Ich habe ein Problem mit SQL-Ausführungszeit mit Order By. Das Problem ist, wenn ich Order By mache, dauert es eine lange Zeit und ich habe einen Peak auf meinem Server. Wenn es eine Möglichkeit, ein Max-Werte ohne Auftrag von durchMS SQL Order By Lange
Beispiel für Code mit Auftrag zu nehmen:
SELECT DISTINCT TOP 3 s.title, s.imageUrl, count(sv.saleid) as mostViewPeriod14Days, s.guid
FROM dbo.Sales s
INNER JOIN dbo.KEY_BrandcategoryToSale b_key ON s.id = b_key.saleId
INNER JOIN dbo.SaleView sv ON s.id = sv.saleId
WHERE (sv.date <= GETDATE())
AND (sv.date >= GETDATE() - 14)
AND s.isActive = 1 AND s.isHotsale = 1
AND b_key.brandCategoryId = 28 AND s.id NOT IN (SELECT sv.saleId FROM dbo.SaleView sv WHERE sv.userId = 99114)
GROUP BY s.title, s.imageUrl, s.guid
ORDER BY mostViewPeriod14Days Desc
Und was ich, was ist eine gute Arbeit, aber ohne Auftrag:
SELECT DISTINCT TOP 3 s.title, s.imageUrl, count(sv.saleid) as mostViewPeriod14Days, s.guid
FROM dbo.Sales s
INNER JOIN dbo.KEY_BrandcategoryToSale b_key ON s.id = b_key.saleId
INNER JOIN dbo.SaleView sv ON s.id = sv.saleId
WHERE (sv.date <= GETDATE())
AND (sv.date >= GETDATE() - 14)
AND s.isActive = 1 AND s.isHotsale = 1
AND b_key.brandCategoryId = 28 AND s.id NOT IN (SELECT sv.saleId FROM dbo.SaleView sv WHERE sv.userId = 99114)
GROUP BY s.title, s.imageUrl, s.guid
Fügen Sie einfach einen Index auf mostViewPeriod14Days – Hogan
Randbemerkung: Verlieren die 'DISTINCT'. Es macht hier nichts. – GurV
Randnotiz: Haben Sie einen Index auf SaleView mit Datum, ID - Datum ist das erste Feld. Hat die Markenkategorie (b_key) einen Index für salesid? Ich bringe diese nur auf, wenn Sie es nicht als Mangel des Indexproblems angesehen haben. –