架构:
CREATE TABLE [Groups] ([Group] VARCHAR(50) NULL, [ArtNum] INTEGER NULL, [ID] VARCHAR(50) NULL, [Time] INTEGER);
CREATE INDEX [GroupTime] ON [Groups] ([Group], [Time] DESC);
CREATE INDEX [GroupArtNum] ON [Groups] ([Group], [ArtNum]);
我想优化以下查询:
SELECT `ID` FROM `Groups` WHERE `Group`=? ORDER BY `Time` DESC LIMIT 1
问题是 SQLite 坚持使用GroupArtNum
索引而不是GroupTime
索引。EXPLAIN QUERY PLAN
打印以下内容:
SEARCH TABLE Groups USING INDEX GroupArtNum (Group=?) (~10 rows)
USE TEMP B-TREE FOR ORDER BY
有趣的是,如果我尝试排序ArtNum
而不是Time
,它运行得非常快。
我在列中只有(64 位)整数,Time
与ArtNum
.