﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / index is not working while using ROW_NUMBER / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 16:14:40 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>Please post table definitions, index definitions and execution plan, as per [url]http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Pictures of the plan are pretty useless.</description><pubDate>Mon, 25 Feb 2013 01:56:18 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>If I use any other column which is in NCI (&amp; not PK), then this is a seek.NCI is same in both situations. Difference is only is Row_Number order by column. Please check the attached two execution plans. One is Row_Number with PK &amp; second one is Row_Number with any other column.one shows SCAN &amp; another one shows SEEK. Difference is only in Row_Number order by column.</description><pubDate>Sun, 24 Feb 2013 21:19:28 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>thanks for this valuable advice.</description><pubDate>Sun, 24 Feb 2013 10:15:51 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>You're not filtering by AdID. If you were, that would be SARGable. You're filtering by a function on AdID, and that, like all other functions, is not SARGable, hence no index seek. As to why the cluster, probably because the query is not covered by any other indexes and hence the clustered index is the more efficient one to use.</description><pubDate>Sun, 24 Feb 2013 10:07:00 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>thanks Gail !This link is really very helpful.But I am not able to understand that if I use AdID (which is primary key &amp; Clustered Index also) in Row_Number order by clause then why this is a SCAN rather than SEEK &amp; even if I have used non clustered index too.I have googled several topics but I haven't found why It is a SCAN ?</description><pubDate>Sun, 24 Feb 2013 09:25:55 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>Well the row number can't be a seek predicate, because there's no index on the row_number.As for the other columns, incorrect index order?[url]http://sqlinthewild.co.za/index.php/2009/02/06/index-columns-selectivity-and-inequality-predicates/[/url]Also there's a chapter in Pro SQL 2012 Practices about predicates and seek predicates</description><pubDate>Sun, 24 Feb 2013 07:33:06 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>Both are using same execution plan.but I am still concerned about "CreatedDate" filter.because all the filters which comes after date, are not coming under seek predicate.</description><pubDate>Sun, 24 Feb 2013 04:09:20 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>[quote][b]Anuj Rathi (2/23/2013)[/b][hr]Which one is efficient?[/quote]Test them both and see?</description><pubDate>Sun, 24 Feb 2013 03:38:23 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>another version is:DECLARE @intPageNumber INT = 1, @intPageSize INT = 20;SELECT A.AdID, A.AdTitle, A.AdFor, A.AdCondition, A.AdExpPrice, A.CreatedDate, A.ModifiedDate, A.AdUIDFROM(	SELECT TOP(@intPageNumber * @intPageSize) ROW_NUMBER() OVER (ORDER BY vaa.CreatedDate) AS RowNum,		vaa.AdID, vaa.AdTitle, vaa.CityID, vaa.AdFor, vaa.AdCondition, 		vaa.AdExpPrice, vaa.CreatedDate, vaa.ModifiedDate, vaa.AdUID	FROM Catalogue.vwAvailableActiveAds vaa	WHERE vaa.CategoryID = 1 AND vaa.CountryCode = 'GB' AND vaa.CreatedDate &amp;gt; DATEADD(dd, -90, GETUTCDATE())		AND vaa.StateID = 1737 AND vaa.CityID = 86) AWHERE A.RowNum &amp;gt; (@intPageSize * (@intPageNumber - 1));Which one is efficient?</description><pubDate>Sat, 23 Feb 2013 23:25:34 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>I found a bit better situation.DECLARE @intPageNumber INT = 1, @intPageSize INT = 20;WITH SQLPagingAS(	SELECT TOP(@intPageNumber * @intPageSize) ROW_NUMBER() OVER (ORDER BY vaa.CreatedDate) AS RowNum,		vaa.AdID, vaa.AdTitle, vaa.CityID, vaa.AdFor, vaa.AdCondition, 		vaa.AdExpPrice, vaa.CreatedDate, vaa.ModifiedDate, vaa.AdUID	FROM Catalogue.vwAvailableActiveAds vaa	WHERE vaa.CategoryID = 1 AND vaa.CountryCode = 'GB' AND vaa.CreatedDate &amp;gt; DATEADD(dd, -90, GETUTCDATE()) 		AND vaa.StateID = 1737 AND vaa.CityID = 86 	)SELECT * FROM SQLPagingWHERE RowNum &amp;gt; (@intPageSize * (@intPageNumber - 1))If I use CreatedDate in place of AdID in Row_Number order by clause, then it uses the index seek till Created date. After that it looks for the predicate StateID &amp; CityID.I can not move the createddate before stateid &amp; cityid becaue stateid &amp; cityid are optional parameters in dynamic query &amp; categoryid, country &amp; created date are fixed conditions.</description><pubDate>Sat, 23 Feb 2013 23:09:40 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>Gail,I have attached the required details.&amp; please ignore the data rows count because I have generated in local server not in production server.</description><pubDate>Sat, 23 Feb 2013 21:14:25 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item><item><title>RE: index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>Please post table definitions, index definitions and execution plan, as per [url]http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]</description><pubDate>Sat, 23 Feb 2013 10:28:23 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>index is not working while using ROW_NUMBER</title><link>http://www.sqlservercentral.com/Forums/Topic1423352-392-1.aspx</link><description>Hi,I have used Row_Number() to implement the paging in my stored procedure. Paging is working fine. But problem is, after implementing the Row_Number(), indexes does not work &amp; a Clustered index SCAN happens even if I use the primary key column in order by section.below is the sample query:SELECT TOP (@insPageSize) A.RowNum, A.AdID, A.AdTitle, A.AdFor, A.AdCondition,     A.AdExpPrice, A.CreatedDate, A.ModifiedDate, A.AdUID    FROM    (	SELECT ROW_NUMBER() OVER (ORDER BY vaa.AdID DESC) AS RowNum,		vaa.AdID, vaa.AdTitle, vaa.CityID, vaa.AdFor, vaa.AdCondition, 		vaa.AdExpPrice, vaa.CreatedDate, vaa.ModifiedDate, vaa.AdUID	FROM Catalogue.vwAvailableActiveAds vaa	WHERE vaa.CategoryID = @intCategoryID AND vaa.CountryCode = @chrCountryCode             AND vaa.CreatedDate &amp;gt; DATEADD(dd, -90, GETUTCDATE())             AND vaa.StateID = @inbStateID AND vaa.CityID = @inbCityID    ) A    WHERE A.RowNum &amp;gt; (@insPageSize * (@insPageNo - 1))if I try to execute only inner query:     SELECT ROW_NUMBER() OVER (ORDER BY vaa.AdID DESC) AS RowNum,		vaa.AdID, vaa.AdTitle, vaa.CityID, vaa.AdFor, vaa.AdCondition, 		vaa.AdExpPrice, vaa.CreatedDate, vaa.ModifiedDate, vaa.AdUID	FROM Catalogue.vwAvailableActiveAds vaa	WHERE vaa.CategoryID = @intCategoryID AND vaa.CountryCode = @chrCountryCode             AND vaa.CreatedDate &amp;gt; DATEADD(dd, -90, GETUTCDATE())             AND vaa.StateID = @inbStateID AND vaa.CityID = @inbCityIDIt does not use any index. AdID is primary key &amp; there is another non clustered index which covers all where clause. But index scan occurs. If I remove the Row_Number() from inner query &amp; check its execution plan, now index works but again StateID &amp; CityID display as "predicate" while they are in non clustered index.I have read that "&amp;gt;" comes under SARGable, then why StateID &amp; City is not coming in seek predicate list.Please give me some guidance to solve my both  problems.</description><pubDate>Sat, 23 Feb 2013 09:07:56 GMT</pubDate><dc:creator>Anuj Rathi</dc:creator></item></channel></rss>