我有一個where子句,像這樣一個SQL查詢:3個索引或單個索引與2列在SQL Server 2008中?
Where ManufacturerID = @ManufacturerID
AND ItemID IN (SELECT ItemID FROM @T)
AND RelatedItemID IN (SELECT RelatedItemID FROM @T)
什麼會給我最好的表現還是這樣做的正確方法? 3個索引 - 每列一個或包含所有3個索引的單個索引?
這裏是SP被運行的一個更完整的VIWE:
DECLARE @T TABLE (
[CategoryID] [int] NOT NULL,
[ManufacturerID] [int] NULL,
[ItemID] [varchar](100) NOT NULL,
[ItemName] [varchar](100) NULL,
[PhotoName] [varchar](150) NULL,
[ModifiedOn] [datetime] NULL,
[ModifiedBy] [varchar](50) NULL,
[IsDeleted] [bit] NOT NULL)
;WITH T As
(SELECT CategoryID, ManufacturerID, ItemID, ItemName, PhotoName, ModifiedOn, ModifiedBy, IsDeleted
FROM StagingCategoryItems
WHERE (ManufacturerID = @ManufacturerID)
EXCEPT
SELECT CategoryID, ManufacturerID, ItemID, ItemName, PhotoName, ModifiedOn, ModifiedBy, IsDeleted
FROM CategoryProducts
WHERE (ManufacturerID = @ManufacturerID)
)
INSERT INTO @T
SELECT *
FROM T
DELETE FROM CategoryProducts WHERE ManufacturerID = @ManufacturerID
AND ItemID IN (SELECT ItemID FROM @T)
AND CategoryID IN(SELECT CategoryID FROM @T)
INSERT INTO [CategoryProducts]
([CategoryID]
,[ManufacturerID]
,[ItemID]
,[ItemName]
,[PhotoName]
,[CreatedOn]
,[CreatedBy]
,[ModifiedOn]
,[ModifiedBy]
,[DeletedOn]
,[DeletedBy]
,[IsDeleted])
SELECT [CategoryID]
,[ManufacturerID]
,[ItemID]
,[ItemName]
,[PhotoName]
,[CreatedOn]
,[CreatedBy]
,[ModifiedOn]
,[ModifiedBy]
,[DeletedOn]
,[DeletedBy]
,[IsDeleted]
FROM [StagingCategoryItems]
WHERE ManufacturerID = @ManufacturerID
AND ItemID IN (SELECT ItemID FROM @T)
AND CategoryID IN(SELECT CategoryID FROM @T)
提供關於什麼是數據,您試圖查詢的內容以及完整查詢SQL的更多細節。它很難理解你在這裏試圖達到什麼。 – 2011-03-28 23:55:21
這取決於此查詢的選擇列表中的內容以及總體查詢工作負載和現有索引。 – 2011-03-29 00:07:02
另外,您是否通過測量確定您有性能問題? – 2011-03-29 00:24:55