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 8721193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:10:51+00:00 2026-06-13T07:10:51+00:00

This query is taking 5 seconds: With cte as ( Select ROW_NUMBER() OVER (Order

  • 0

This query is taking 5 seconds:

With cte as 
(
    Select  ROW_NUMBER() OVER (Order By dbo.GetLatestStatusDateTime(d.DocumentID)) peta_rn, d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
        d.IDate, d.InstrumentID, d.DocumentStatusID, u.Username, j.JDAbbreviation, inf.DocumentName,  dbo.GetLatestStatusDateTime(d.DocumentID) as LatestStatusDatetime,
        it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation 
                From Documents d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
                Left Join ITypes it On d.ITypeID = it.ITypeID 
                Left Join Users u On u.UserID = d.UserID 
                Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
                Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
                Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID Where 1 = 1  And d.JurisdictionID = 1
)
Select  * from cte
                Where 1 = 1 
                And peta_rn>92000 AND peta_rn<=92100

Though this is a static query, it will be dynamic in the sense that Order By in Row_Number() would be based on what user selects. Eg. currently it is dbo.GetLatestStatusDateTime(d.DocumentID) but it can be anything else too. Hence this query would be generated in code using if and then. Anyways, can you spot any mistake in my query? Why would it take 5 seconds for mere 100 records? I have seen SQL server scale upto millions of records and this is just 1/10th of million and I am suffering from bottleneck 🙁

Edit: Massive improvement

Changing the query to this:

With cte as 
(

        Select  ROW_NUMBER() OVER (Order By d.DocumentID) peta_rn, d.DocumentID
                From Documents d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
                Left Join ITypes it On d.ITypeID = it.ITypeID 
                Left Join Users u On u.UserID = d.UserID 
                Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
                Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
                Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID Where 1 = 1  And d.JurisdictionID = 1
)
Select  d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
        d.IDate, d.InstrumentID, d.DocumentStatusID, u.Username, j.JDAbbreviation, inf.DocumentName, 
        it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation 
                From Documents d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
                Left Join ITypes it On d.ITypeID = it.ITypeID 
                Left Join Users u On u.UserID = d.UserID 
                Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
                Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
                Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
                Inner Join cte On cte.DocumentID = d.DocumentID
                And peta_rn>92000 AND peta_rn<=92100

executes the query in 1 second. But I still feel 1 second is too big just for 100 records. Any other optimizations please?

  • 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-06-13T07:10:52+00:00Added an answer on June 13, 2026 at 7:10 am

    try this. It is alot of code to edit without knowing the tables relations and not being able to test it. I hope this helps.

    With d as 
    (
      Select ROW_NUMBER() OVER (Order By d.DocumentID) peta_rn, 
      DocumentID, IsReEfiled, IGroupID, ITypeID, RecordingDateTime,
      IDate, InstrumentID, DocumentStatusID, JurisdictionID, UserID
      from Documents
    )
    Select  d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
    d.IDate, d.InstrumentID, d.DocumentStatusID, u.Username, j.JDAbbreviation, inf.DocumentName, 
    it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation 
    From d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
    Left Join ITypes it On d.ITypeID = it.ITypeID 
    Left Join Users u On u.UserID = d.UserID 
    Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
    Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
    Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
    where
    d.peta_rn>92000 AND d.peta_rn<=92100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query which is taking around 40-50 seconds to load SELECT members.latitude,
I have a query that is taking 48 seconds to execute as follows: SELECT
I have this linq query i'm using and it's taking 50 seconds to run
I have this query which is a dependant query and taking much execution time
I have this query that, due to the number of records, is taking several
I am having this Linq To SQL query which is taking Customer Category from
this query SELECT * FROM tblContracts LEFT JOIN tblPartys ON tblContracts.id = tblPartys.Contract_id INNER
This query works fine for me: $query = SELECT p.topnode_id, p.param_key, p.param_value FROM tbl_params
This query selects all the unique visitor sessions in a certain date range: select
This query works: item = db.GqlQuery(SELECT * FROM Item WHERE CSIN = 13)[0] although

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.