在SQLite中,子查询的速度与使用固定值的速度一样快



我正在使用一个视图来检索和显示产品,目前这个列表只有几百个条目,但可能会达到数百万。

我还有一张表,里面有一张产品清单。

哪个执行起来更快?以下查询:

SELECT * FROM Products_view where code IN (SELECT code from Products Where [clause])

或者首先检索子查询中的数据:

SELECT code from Products Where [clause]

然后执行这样的操作:

SELECT * FROM Products_view where code IN (val1,val2,val3....)

顺便说一句,是的,我知道我可以连接这两个表,然后在连接上执行where,但在这种情况下,它不起作用。

提前感谢您为提供的任何帮助

我一直在尝试测试以上内容,花了几个小时:)但结果如下:

我设法用271226592个项目填充product_view,用13272个项目填充子查询表(products)。

我用sqlitestudio测量了阅读每本书所需的时间。结果如下:

从子查询表检索项目时使用IN:

select * from product_view where code in (
select code from products limit y,x)

使用固定:

Select code from products limit y,x; 
select * from product_view where code in ('values'....)

其中y是随机数,x是下面每个头中的数字

产品中的10行从视图中检索194142行

       1st try   2nd try   3rd try   4th try   5th try   |  Average    |  Total
IN     0.006458  0.006182  0.005339  0.005902  0.005523  |  0.0058808  |  0.0058808
FIXED  0.000277  0.000295  0.000217  0.000215  0.000303  |  0.0002614  |
    +  0.005887  0.006221  0.004748  0.004511  0.005724  |  0.0054182  |  0.0056796

产品中的20行从视图中检索367848行

       1st try   2nd try   3rd try   4th try   5th try   |  Average    |  Total
IN     0.004724  0.004552  0.007573  0.005836  0.005913  |  0.0057196  |  0.0057196
FIXED  0.000260  0.000430  0.000267  0.000509  0.000646  |  0.0004224  |
    +  0.004682  0.006082  0.006079  0.004760  0.006242  |  0.0055690  |  0.0059914

产品中的50行从视图中检索848094行

       1st try   2nd try   3rd try   4th try   5th try   |  Average    |  Total
IN     0.004729  0.004773  0.004701  0.004916  0.006257  |  0.0050752  |  0.0050752
FIXED  0.000463  0.000383  0.000418  0.000348  0.000342  |  0.0003908  |
    +  0.005873  0.004759  0.005034  0.004911  0.004796  |  0.0050746  |  0.0054654

产品中的100行从视图中检索1496937行

       1st try   2nd try   3rd try   4th try   5th try   |  Average    |  Total
IN     0.004861  0.004720  0.004819  0.004874  0.005934  |  0.0050416  |  0.0050416
FIXED  0.001217  0.000361  0.000327  0.000337  0.000338  |  0.0005160  |
    +  0.004855  0.005020  0.004896  0.005891  0.004994  |  0.0051312  |  0.0056472

所以实际上,差异似乎没有那么大,不超过+-0.0005秒

相关内容

最新更新