我在c#中使用以下sqlite查询。查询将针对100K项进行迭代。我觉得执行速度很慢,通过优化查询可以提高速度
insert into salesmetrics(salesid, salesrankcount, volumerankcount, countsales,Avgsales)
select @salesid,sum(salesrank), sum(volumerank), Count(salesrank), avg(salesrank)
from (select salesrank, volumerank
from salesindex
Join SalesData on salesindex.salesindexID = salesData.salesindexID
where SourceID = @Sourceid
and Content like @content
group by salesindex.salesindexID)
表:
CREATE TABLE `salesindex` (
salesindexID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
salesindex TEXT,
SourceID INTEGER,
salesrank INTEGER,
volumerank INTEGER,
dateAdded DATETIME,
UNIQUE(SourceID,dateAdded)
);
CREATE TABLE `SalesData` (
SalesDataID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
salesindexID INTEGER,
Content TEXT,
dateAdded DATETIME,
UNIQUE(salesindexID,content)
);
CREATE TABLE `salesmetrics` (
salesmetricsID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
salesID INTEGER,
SalesRankCount INTEGER,
VolumeRankCount INTEGER,
countsales INTEGER,
SalesRank FLOAT
);
salesdata表上的“content”和salesindex表上的sourceid的索引
答案 0 :(得分:0)
主要问题是内部查询中的GROUP BY,显然需要压缩多个匹配的SalesData
行。
执行Content
检查子查询:
SELECT @salesid,
SUM(salesrank),
SUM(volumerank),
COUNT(salesrank),
AVG(salesrank)
FROM salesindex
WHERE salesindexID IN (SELECT salesindexID
FROM SalesData
WHERE Content LIKE @content)
AND SourceID = @Sourceid;
或者,根据Content
查找的选择性,将查找作为相关子查询进行查询可能更好:
SELECT @salesid,
SUM(salesrank),
SUM(volumerank),
COUNT(salesrank),
AVG(salesrank)
FROM salesindex
WHERE EXISTS (SELECT 1
FROM SalesData
WHERE salesindexID = salesindex.salesindexID
AND Content LIKE @content)
AND SourceID = @Sourceid;
答案 1 :(得分:-1)
除非我遗漏了某些内容,否则我认为您可以直接插入选择,而不是选择选择:
insert into salesmetrics(salesid, salesrankcount, volumerankcount, countsales,Avgsales)
(select @salesid,sum(salesrank), sum(volumerank), Count(salesrank), avg(salesrank)
from salesindex
Join SalesData on salesindex.salesindexID = salesData.salesindexID
where SourceID = @Sourceid
and Content like @content
group by salesindex.salesindexID)