0

I have a DB with about 2 million rows and I need to fix my current paging and have decided to go with the following:

SET @startRowIndex = ((@Page-1) * @PageSize) + 1; 

    SET ROWCOUNT @startRowIndex
    SELECT @first_id = ProductID FROM LiveProducts (nolock) WHERE ManufacturerID=@ManufacturerID AND ModifiedOn >= @tStamp ORDER BY ProductID

    SET ROWCOUNT @PageSize
    SELECT * FROM LiveProducts (nolock) WHERE ManufacturerID=@ManufacturerID AND ProductID >= @first_id ORDER BY ProductID

I am no where near a DBA and I want this to be as fast as possible. What index(s) shoud i set on this thing. From my reading and my basic understanding I gathered I should create a no-clustered index on ManufacturerID, ProductID, and ModifiedOn.

But should they all be Index key columns, or just one there and the others in Included Columns?

4

1 回答 1

0

第一个查询使用以下列: ProductIdManufacturerIdModifiedOn

因为您在日期上有一个不等式,所以索引可用于优化where子句,但不能用于优化order by. 但是,通过ProductId在索引中包含 ,引擎可以使用以下索引来满足整个查询:) LiveProducts(ManufacturerId, ModifiedOn, ProductId。请注意,这些列的顺序很重要。而且,查询仍然需要对order by.

第二个查询是选择所有列,所以它需要去原始数据。因此,优化仅针对where子句。为此,使用LiveProducts(ManufacturerId, ProductId). 在这种情况下,它应该能够使用索引进行排序。

于 2013-07-05T14:47:38.783 回答