我有一个SQLite数据库C#应用程序,我在内存中运行(:memory :)。我的表有大约650k行,这并不多。我希望在以下查询上获得快速响应时间(从LINQ到SQL动态LINQ查询获取语法)
SELECT
SUM([t0].[Value1]) AS [Sum],
[t0].[Dim1] AS [Primary],
[t0].[Dim2] AS [Secondary]
FROM [BudgetLine] AS [t0]
WHERE [t0].[BudgetID] = 4
GROUP BY [t0].[Dim1], [t0].[Dim2]
哪个是此查询的最佳索引?除了主键之外,我唯一的索引如下所示...
create index IX_0 on budgetLine (BudgetID, Dim1, Dim2)
create index IX_1 on budgetLine (BudgetId)
create index IX_2 on budgetLine (Dim1, Dim2)
create index IX_3 on budgetLine (BudgetID, Dim1, Dim2,Value1)
目前执行时间各不相同,但当前平均值约为1秒。我希望这个查询至少花费少于0.5秒。
该表有大约50列。
请协助
更新:查看我上面的4个索引,这些我得到了0.8秒的响应...
答案 0 :(得分:0)
我会尝试这些索引。
create index IX_2 on budgetLine(BudgetId)
create index IX_3 on budgetLine(Dim1, Dim2)
答案 1 :(得分:0)
删除索引
create index IX_3 on budgetLine (BudgetID, Dim1, Dim2,Value1)
create index IX_0 on budgetLine (BudgetID, Dim1, Dim2)
并且只使用bartosz提供的budgetid索引(但不是dim1,dim2)
内存数据库将受到系统内存的限制 - 您是在运行不足还是过载的机器上运行?您有没有理由使用:内存:而不是实际的磁盘文件? (见Stack overflow question here)
还要考虑阅读有关性能调优的SQLite文档, SQLite performance for windows
还尝试按如下方式更改SQL查询:
select
SUM([Value1]) AS [Sum]
,[Dim1] AS [Primary],
[Dim2] AS [Secondary]
from
(
SELECT
value1, dim1, dim2
FROM [BudgetLine]
WHERE [BudgetID] = 4
) as a
GROUP BY [Dim1], [Dim2]