为什么不使用第二个 WHERE 条件上的索引?
此查询需要 2 秒,仅返回 16 条记录:
SELECT * FROM cells WHERE cellbookguid='1' AND cellpageguid='1';
表 cells
包含大约 20.000 条记录。它有 42 列,例如 cellwidth
、cellheight
、cellcolor
、cellbackground
、cellpageguid
、 cellbookguid
等。EXPLAIN QUERY PLAN
说:
order: 0
from: 0
detail: TABLE cells WITH INDEX idx_cells_cellbookguid
因此,因为它不使用索引 idx_cells_cellpageguid
,所以需要很长时间。但是,SELECT name FROM sqlite_master WHERE type = 'index';
显示它确实包含 idx_cells_cellpageguid
。外部 SQLite 查看器还显示该列上存在索引。为什么查询不使用这个索引?
idx_cells_cellpageguid
的定义:
SQL 命令:
CREATE INDEX idx_cells_cellpageguid
ON cells
(CellPageGUID COLLATE BINARY)
This query takes 2 seconds to only return 16 records:
SELECT * FROM cells WHERE cellbookguid='1' AND cellpageguid='1';
Table cells
holds around 20.000 records. It has 42 columns such as cellwidth
, cellheight
, cellcolor
, cellbackground
, cellpageguid
, cellbookguid
, etc. EXPLAIN QUERY PLAN
says:
order: 0
from: 0
detail: TABLE cells WITH INDEX idx_cells_cellbookguid
So because it does not use the index idx_cells_cellpageguid
it takes so long. However, SELECT name FROM sqlite_master WHERE type = 'index';
shows it does contain idx_cells_cellpageguid
. An external SQLite viewer also shows that index is present on the column. Why does the query not use this index?
The definition of idx_cells_cellpageguid
:
The SQL command:
CREATE INDEX idx_cells_cellpageguid
ON cells
(CellPageGUID COLLATE BINARY)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论