Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:56:02+00:00 2026-05-25T18:56:02+00:00

I inherited this hellish query designed for pagination in SQL Server. It’s only getting

  • 0

I inherited this hellish query designed for pagination in SQL Server.

It’s only getting 25 records, but according to SQL Profiler, it does 8091 reads, 208 writes and takes 74 milliseconds. Would prefer it to be a bit faster. There is an index on the ORDER BY column deployDate.

Anyone have any ideas on how to optimise it?

SELECT TOP 25 
    textObjectPK, textObjectID, title, articleCredit, mediaCredit,
    commentingAllowed,deployDate, 
    container, mediaID, mediaAlign, fileName AS fileName, fileName_wide AS fileName_wide, 
    width AS width, height AS height,title AS mediaTitle, extension AS extension, 
    embedCode AS embedCode, jsArgs as jsArgs, description as description, commentThreadID,
    totalRows = Count(*) OVER()
 FROM
    (SELECT 
        ROW_NUMBER() OVER (ORDER BY textObjects.deployDate DESC) AS RowNumber,
        textObjects.textObjectPK, textObjects.textObjectID, textObjects.title,  
        textObjects.commentingAllowed, textObjects.credit AS articleCredit, 
        textObjects.deployDate, 
        containers.container, containers.mediaID, containers.mediaAlign, 
        media.fileName AS fileName, media.fileName_wide AS fileName_wide, 
        media.width AS width, media.height AS height, media.credit AS mediaCredit, 
        media.title AS mediaTitle, media.extension AS extension, 
        mediaTypes.embedCode AS embedCode, media.jsArgs as jsArgs, 
        media.description as description, commentThreadID,
        TotalRows = COUNT(*) OVER ()
     FROM textObjects WITH (NOLOCK) 
     INNER JOIN containers WITH (NOLOCK) 
                ON containers.textObjectPK = textObjects.textObjectPK 
                AND (containers.containerOrder = 0 or containers.containerOrder = 1)
     INNER JOIN LUTextObjectTextObjectGroup tog WITH (NOLOCK)
                ON textObjects.textObjectPK = tog.textObjectPK
                AND tog.textObjectGroupID in (3)
     LEFT OUTER JOIN media WITH (NOLOCK) 
                ON containers.mediaID = media.mediaID 
     LEFT OUTER JOIN mediaTypes WITH (NOLOCK) 
                ON media.mediaTypeID = mediaTypes.mediaTypeID
     WHERE (((version = 1)   
              AND (textObjects.textObjectTypeID in (6))  
       AND (DATEDIFF(minute, deployDate, GETDATE()) >= 0) 
       AND (DATEDIFF(minute, expireDate, GETDATE()) <= 0))
       OR  ( (version = 1) AND (textObjects.textObjectTypeID in (6))  
       AND (DATEDIFF(minute, deployDate, GETDATE()) >= 0) 
       AND (expireDate IS NULL))) 
       AND deployEnglish = 1
       ) tmpInlineView
  WHERE RowNumber >= 51
  ORDER BY deployDate DESC 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:56:03+00:00Added an answer on May 25, 2026 at 6:56 pm

    I am in a similar position to with the same sort of queries. Here are some tips:

    • Look at the query plans to make sure you have the right indexes.
    • I’m not sure if MSSQL optimizes around DATEDIFF(), but if it doesn’t you can precompute threshold dates and turn it into a BETWEEN clause.
    • If you don’t need to order by all those columns in your ROW_NUMBER() clause, get rid of them. That may allow you to do the pagination on a much simpler query, then just grab the extra data you need for the 25 rows you are returning.

    Also, rewrite the two LEFT OUTER JOINs like this:

    LEFT OUTER JOIN 
    (
        media WITH (NOLOCK) 
            LEFT OUTER JOIN mediaTypes WITH (NOLOCK) 
                 ON media.mediaTypeID = mediaTypes.mediaTypeID
    )
        ON containers.mediaID = media.mediaID 
    

    which should make the query optimizer behave a little better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had inherited this SQL Server where we put data in a table (Call
I know this is a broad question, but I've inherited several poor performers and
This is a C# question, but really could be language-agnostic I have inherited a
I am using ASP classic with ADO, connecting to SQL Server 2008. I inherited
This is running on SQL Server 2000 Background: when a new server is brought
The full .Net framework has an inherited UserControl but this option isn't offered by
I inherited this huge application consisting of client and server code, and I'm trying
I've been working on this Solution for a while now, but suddenly I'm getting
i inherited this code from some-one and this only returns the first row it
I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.