我正在使用视图来检索和显示产品,目前这个列表只有几百个条目,但它可能会达到数百万。
我还有一张包含产品清单的表格。
哪个会更快执行?以下查询:
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....)
作为旁注,是的我知道我可以加入这两个表然后执行连接的位置,但在这种情况下它将无法工作。
提前感谢您提供的任何帮助
答案 0 :(得分:0)
我一直试图测试上面的内容,花了我几个小时:)但结果如下:
我设法用271,226,592项填充product_view,子查询表(产品)用13,272项填充。
我用sqlitestudio测量了读取每个文件的时间。 结果如下:
从子查询表中检索项目时 使用IN:
select * from product_view where code in (
select code from products limit y,x)
使用FIXED:
Select code from products limit y,x;
select * from product_view where code in ('values'....)
其中y是随机数,x是以下每个标题中的数字
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
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
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
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秒