简体   繁体   English

为什么Sqlite不为此ORDER BY使用索引?

[英]why does Sqlite not use the index for this ORDER BY?

I have this query: 我有这个查询:

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

and I have this index: 我有这个索引:

CREATE INDEX [IX_Events_TelemetryId_TimestampTicks] ON [Events] ([TelemetryId],[TimestampTicks])

However, the index is not used for the ORDER BY clause. 但是,索引不用于ORDER BY子句。 I get this query explanation: 我得到此查询说明:

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

Why the B-TREE? 为什么选择B树? If I reverse the index, I actually get worse performance. 如果我反转索引,则实际上会得到较差的性能。 Here's that query plan: 这是查询计划:

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)

I don't know why that ordering disallows use of the TelemetryId. 我不知道为什么该顺序不允许使用TelemetryId。 I really need this query faster. 我真的需要这个查询更快。 Any help? 有什么帮助吗?

  • The specified index is on ([TelemetryId],[TimestampTicks]), not ([TimestampTicks]), and there is no filter criterai on [TelemetryId]. 指定的索引位于([TelemetryId],[TimestampTicks])上,而不是([TimestampTicks]),并且[TelemetryId]上没有过滤条件。
  • If the test DB does not have a full working data volumne, execution plans in test may not reflect execution plans in production. 如果测试数据库没有完整的工作数据量,则测试中的执行计划可能无法反映生产中的执行计划。
  • DB engines attempt to model whther index usage is more efficient than a table scan before choosing to use the index. 数据库引擎尝试在选择使用索引之前对索引使用情况进行建模,使其比表扫描更有效。 Often an index that looks useful might be ignored if the expected data volume > ~10% of the table. 如果期望的数据量大于表的〜10%,通常会忽略看起来有用的索引。 (Not likely in this case though.) (尽管在这种情况下不太可能。)
  • Chasing down imaginary performance problems is a great time waster. 追逐想象中的性能问题是浪费大量的时间。 Is there a real performance problem in this instance? 在这种情况下是否存在真正的性能问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM