[英]Why SQLite index is not speed up query?
I wrote this query: 我写了这个查询:
INSERT INTO KeysTable (KeyText, Id)
SELECT KeyText as BKT, KeysTable.ID as CID FROM KeysTable
INNER JOIN StatTable ON KeysTable.ID = StatTable.Key_ID
WHERE StatTable.StatCommandCode = 4 AND
EXISTS (SELECT 1 FROM StatTable WHERE StatCommandCode = 4 AND StatTable.Key_ID = CID);
I know that removing the condition 我知道删除条件
AND StatTable.Key_ID = CID
would make the query very fast. 会使查询非常快。 Also if I replace it with
如果我用它替换它
AND StatTable.Key_ID = 444 // (444 - random static number)
the query will be very fast too. 查询也会非常快。 Both the columns in this condition are indexed:
这种情况下的两列都被编入索引:
CREATE INDEX IF NOT EXISTS StatsIndex ON StatTable (Key_ID);
and in KeysTable the ID column is primary key. 在KeysTable中 , ID列是主键。 Why doesn't the index improve perfomance in this case?
为什么索引在这种情况下不能改善性能?
Thanks for answers and sorry for my bad english :(. 谢谢你的回答,抱歉我的英语不好:(。
If there is no CID
column in any of the two tables, then the EXISTS
subquery is useless. 如果两个表中的任何一个表中都没有
CID
列,那么EXISTS
子查询就没用了。 Rewrite the statement as: 将声明重写为:
INSERT INTO KeysTable (KeyText, Id)
SELECT KeyText
, KeysTable.ID
FROM KeysTable
INNER JOIN StatTable
ON KeysTable.ID = StatTable.Key_ID
WHERE StatTable.StatCommandCode = 4
If it is still slow, you can try adding an index on (StatCommandCode, Key_ID)
如果它仍然很慢,您可以尝试添加索引
(StatCommandCode, Key_ID)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.