我有这个问题:
SELECT * FROM Events e
INNER JOIN Telemetry ss ON ss.Id = e.TelemetryId
INNER JOIN Services s ON s.Id = ss.ServiceId
WHERE s.AssetId = @AssetId AND e.TimestampTicks >= @StartTime
ORDER BY e.TimestampTicks LIMIT 1000
我有这个索引:
CREATE INDEX [IX_Events_TelemetryId_TimestampTicks] ON [Events] ([TelemetryId],[TimestampTicks])
但是,索引不用于ORDER BY子句。我得到了这个查询解释:
0|0|2|SCAN TABLE Services AS s (~44 rows)
0|1|1|SEARCH TABLE Telemetry AS ss USING AUTOMATIC COVERING INDEX (ServiceId=?) (~5 rows)
0|2|0|SEARCH TABLE Events AS e USING INDEX IX_Events_TelemetryId_TimestampTicks (TelemetryId=? AND TimestampTicks>?) (~1816 rows)
0|0|0|USE TEMP B-TREE FOR ORDER BY
为什么B-TREE?如果我反转索引,我实际上会变得更差。这是查询计划:
0|0|0|SEARCH TABLE Events AS e USING INDEX IX_Events_TimestampTicks_TelemetryId (TimestampTicks>?) (~4031303 rows)
0|1|1|SEARCH TABLE Telemetry AS ss USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
0|2|2|SEARCH TABLE Services AS s USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
我不知道为什么这个命令不允许使用TelemetryId。我真的更需要这个查询。有什么帮助吗?
答案 0 :(得分:0)