【发布时间】:2012-10-14 00:09:14
【问题描述】:
当我在查询中添加 ORDER BY 语句时,它变得非常慢。
这是我没有 ORDER BY 的查询:
SELECT ClientIpAddress, Agentstring, Count(ClientIpAddress) AS Count FROM LogEntries
WHERE SiteIisId = 3 AND DateTime >= '13-09-2012 00:00:00'
GROUP BY ClientIpAddress, Agentstring
LIMIT 5
ET:1 毫秒
现在使用 ORDER BY:
SELECT ClientIpAddress, Agentstring, Count(ClientIpAddress) AS Count FROM LogEntries
WHERE SiteIisId = 3 AND DateTime >= '13-09-2012 00:00:00'
GROUP BY ClientIpAddress, Agentstring
ORDER BY Count DESC
LIMIT 5
ET:294 毫秒
我正在查询的表包含 1.380.855 行。
这是我正在使用的索引:
CREATE INDEX "LogEntries_MostActiveClients" ON "LogEntries" ("ClientIpAddress" ASC, "Agentstring" ASC, "SiteIisId" ASC, "DateTime" DESC)
使用EXPLAIN QUERY PLAN Sqlite 告诉我它正在使用我的索引扫描表,并且正在使用TEMB B-TREE作为我的Order By。
我该如何克服这个问题?显然我无法索引Count,那该怎么办?
谢谢一百万!
【问题讨论】:
-
请注意,
DateTime上的比较不能正常工作,因为它没有从最重要的字段开始,即年份。您应该使用supported date formats 之一。 -
对不起,我已经添加了正在使用的索引。感谢您指出 DateTime 问题。
标签: sqlite group-by sql-order-by