Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlite函数查询优化器_Sql_Sqlite - Fatal编程技术网

sqlite函数查询优化器

sqlite函数查询优化器,sql,sqlite,Sql,Sqlite,表架构为: create table test_table (id int PRIMARY KEY NOT NULL, feature blob) eg数据: id feature 1 blob=bytes[512]=float[128] eg[0.01,111,232....] 2 blob=bytes[512]=float[128] eg[0.02,113,-22....] 3 blob=bytes[512]=float[128] eg[222

表架构为:

create table test_table (id int PRIMARY KEY NOT NULL, feature blob)
eg数据:

id      feature
1       blob=bytes[512]=float[128] eg[0.01,111,232....]
2       blob=bytes[512]=float[128] eg[0.02,113,-22....]
3       blob=bytes[512]=float[128] eg[2222,113,-22....]
UDF余弦距离是带有两个blob参数的自定义函数。 列特性是浮点数组的一个blob(float[128]),然后我想计算余弦距离。 我有一个sql,它具有昂贵的函数余弦距离(udf)和sqlite

select cosine_distance(a,?) as distance 
from test_table 
where distance >=? 
order by distance desc limit ?
在此sql中,余弦距离函数将在sqlite中调用两次。 如何让余弦距离(一个昂贵的函数)只执行一次

当我使用子选择时,例如:

select * from (select cosine_distance(feature,?) d from test_table) nt 
where nt.d>? 
order by nt.d desc limit ?
UDF余弦距离将在每个记录中执行两次


当表test\u表有5记录时,余弦距离将被调用10次。但是余弦距离非常昂贵。

尝试在子查询上添加限制,如下所示:

select * from (select cosine_distance(feature,?) as d from test_table limit -1) nt 
where nt.d>? 
order by nt.d desc limit ?
根据第13条的规定,这可能会禁用

选项2:在子查询上使用偏移量:

select * from (select cosine_distance(feature,?) as d from test_table offset 0) nt 
where nt.d>? 
order by nt.d desc limit ?
选项3:在两个查询中使用ORDER BY:

select * from (select cosine_distance(feature,?) as d from test_table order by d) nt 
where nt.d>? 
order by nt.d desc limit ?

创建函数时是否将其标记为确定性?是否可以添加示例数据?@Shawn未将其标记为确定性。但我只是将其标记为确定性。该函数仍将被调用两次为什么在“昂贵函数”上使用下划线?您还可以创建一列来存储计算值,甚至为该列编制索引。每次插入或更新行上的值时,都要更新新列。此更新也可以通过触发器实现