UNIONを含むクエリからページングされたデータを返すSPがあります。これは私のDBを殺していると時々実行するために30秒かかる、私はここに何かを見逃している?パフォーマンスを改善するために私は何ができますか?関与SELECT UNIONでページングを超過し、サーバーを強制終了する
テーブル:製品、カテゴリー、CategoryProducts
目標: カテゴリーに含まれていないか、Webサービスのためのそれらの上のカテゴリやページ内のすべての製品現在のカテゴリUNIONから削除されたすべての製品。
私が参加しているすべての列にインデックスがあり、427,996個の製品、6148個のカテゴリ、409,691個のカテゴリ製品がデータベースにあります。ここで
を実行するために6、および30秒の間取っている私のクエリです:
SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY Products.ItemID, Products.ManufacturerID) AS RowNum, *
FROM
(
SELECT Products.*,
CategoryID = NULL, CategoryName = NULL,
CategoryProductID = NULL,
ContainerMinimumQuantity =
CASE COALESCE(Products.ContainerMinQty, 0)
WHEN 0 THEN Products.OrderMinimumQuantity
ELSE Products.ContainerMinQty
END
Products.IsDeleted,
SortOrder = NULL
FROM CategoryProducts RIGHT OUTER JOIN Products
ON CategoryProducts.ManufacturerID = Products.ManufacturerID
AND CategoryProducts.ItemID = Products.ItemID
WHERE (Products.ManufacturerID = @ManufacturerID)
AND (Products.ModifiedOn > @tStamp)
AND ((CategoryProducts.IsDeleted = 1) OR (CategoryProducts.IsDeleted IS NULL))
UNION
SELECT Products.*,
CategoryProducts.CategoryID , CategoryProducts.CategoryName,
CategoryProducts.CategoryProductID ,
ContainerMinimumQuantity =
CASE COALESCE(Products.ContainerMinQty, 0)
WHEN 0 THEN Products.OrderMinimumQuantity
ELSE Products.ContainerMinQty
END
CategoryProducts.IsDeleted,
CategoryProducts.SortOrder
FROM Categories INNER JOIN
CategoryProducts ON Categories.CategoryID = CategoryProducts.CategoryID INNER JOIN
Products ON CategoryProducts.ManufacturerID = Products.ManufacturerID
AND CategoryProducts.ItemID = Products.ItemID
WHERE (Products.ManufacturerID = @ManufacturerID)
AND (Products.ModifiedOn > @tStamp OR CategoryProducts.ModifiedOn > @tStamp))
AS Products) AS C
WHERE RowNum >= @StartRow AND RowNum <= @EndRow
任意の洞察力をいただければ幸いです。
クエリを調べる前に、実行計画の外観を教えてください。 (バルク、つまりインデックススキャンを実行していますか?) – McGarnagle
ユニオンは必要ですか、またはすべてのユニオンが動作しますか? –
@TimLehner私はそれが私に違いをもたらすとは思わない - それはパフォーマンスに大きく影響するのだろうか? – Slee