我有两条语句返回相同的结果,但产生不同的执行计划。
- 第一 >>> 索引搜索
- 第二个>>>索引扫描
谁能解释为什么?
例子
CREATE TABLE OrderDetails (intOrderId int, intItemId int, dtOrderDate Datetime, intQty int, intPrice int, intDiscount int)
GO
CREATE CLUSTERED INDEX CI_OrderId ON OrderDetails(intOrderId)
GO
CREATE NONCLUSTERED INDEX NCI_ItemId ON OrderDetails(intItemId)
GO
-- Populate Data
SET NOCOUNT ON
DECLARE @i int
SET @i =10
WHILE @i < 100000
BEGIN
INSERT INTO OrderDetails
VALUES (@i, round(rand()*9999,0)+1, getdate() - round(rand()*999,0), round(rand()*99,0)+1, round(rand()*9999,0)+1, round(rand()*99,0)+1)
SET @i = @i + 1
END
GO
-- Check Execution Plans
-- NCI SEEK
SELECT intOrderId, intItemId
FROM OrderDetails
WHERE intItemId = 600 * 10
-- NCI SCAN
SELECT intOrderId, intItemId
FROM OrderDetails
WHERE intItemId/10 = 600