[Solved] How do I speed up this Row_Number query?

EverSQL Database Performance Knowledge Base

How do I speed up this Row_Number query?

Database type:

This is my query:

SELECT * 
FROM 
    (SELECT 
        ROW_NUMBER() OVER (ORDER BY NAME asc) peta_rn, 
        peta_query.* 
     FROM 
         (SELECT 
              BOOK, PAGETRIMMED, NAME, TYPE, PDF 
         FROM 
              CCWiseDocumentNames2 cdn
         INNER JOIN 
              CCWiseInstr2 cwi ON cwi.ID = cdn.ID) as peta_query) peta_paged 
WHERE 
    peta_rn > 1331900 AND peta_rn <= 1331950

Currently this query takes about 4 seconds to get the results. Is there any way to bring it under 1 second?

Index is already created on cwi.ID and cdn.ID. Below is the actual execution plan from sql server:

enter image description here

Any help would be useful.

This is the table structure:

    /****** Object:  Table [dbo].[CCWiseInstr2]    Script Date: 9/17/2013 3:54:27 AM ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    SET ANSI_PADDING ON
    GO

    CREATE TABLE [dbo].[CCWiseInstr2](
        [ID] [int] NULL,
        [BK_PG] [varchar](50) NULL,
        [DATE] [datetime] NULL,
        [ITYPE] [varchar](50) NULL,
        [BOOK] [int] NULL,
        [PAGE] [varchar](50) NULL,
        [NOBP] [varchar](50) NULL,
        [DESC] [varchar](240) NULL,
        [TIF] [varchar](50) NULL,
        [INDEXNAME] [varchar](50) NULL,
        [CONFIRM] [varchar](50) NULL,
        [PDF] [varchar](50) NULL,
        [PAGETRIMMED] [varchar](10) NULL
    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF
    GO

    /****** Object:  Index [IX_CCWiseInstr2_ID]    Script Date: 9/17/2013 3:54:27 AM ******/
    CREATE NONCLUSTERED INDEX [IX_CCWiseInstr2_ID] ON [dbo].[CCWiseInstr2]
    (
        [ID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO



/****** Object:  Table [dbo].[CCWiseDocumentNames2]    Script Date: 9/17/2013 3:54:18 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[CCWiseDocumentNames2](
    [ID] [int] NULL,
    [BK_PG] [varchar](50) NULL,
    [NAME] [varchar](100) NULL,
    [OTHERNAM] [varchar](100) NULL,
    [TYPE] [varchar](50) NULL,
    [INDEXNAME] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

/****** Object:  Index [IX_CCWiseDocumentNames2_ID]    Script Date: 9/17/2013 3:54:18 AM ******/
CREATE NONCLUSTERED INDEX [IX_CCWiseDocumentNames2_ID] ON [dbo].[CCWiseDocumentNames2]
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_CCWiseDocumentNames2_NAME]    Script Date: 9/17/2013 3:54:18 AM ******/
CREATE NONCLUSTERED INDEX [IX_CCWiseDocumentNames2_NAME] ON [dbo].[CCWiseDocumentNames2]
(
    [NAME] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Subqueries (query line: 10): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
CREATE INDEX ccwiseinstr2_idx_id ON CCWiseInstr2 (ID);
The optimized query:
SELECT
        * 
    FROM
        (SELECT
            ROW_NUMBER() OVER (ORDER 
        BY
            peta_query.NAME ASC) peta_rn,
            peta_query.* 
        FROM
            (SELECT
                BOOK,
                PAGETRIMMED,
                NAME,
                TYPE,
                PDF 
            FROM
                CCWiseDocumentNames2 cdn 
            INNER JOIN
                CCWiseInstr2 cwi 
                    ON cwi.ID = cdn.ID) AS peta_query) peta_paged 
        WHERE
            peta_rn > 1331900 
            AND peta_rn <= 1331950

Related Articles



* original question posted on StackOverflow here.