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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:24:23+00:00 2026-05-29T06:24:23+00:00

I have a SQL query that contains a derieved table. The derived query looks

  • 0

I have a SQL query that contains a derieved table.

The derived query looks something like this:

SELECT 
   ObjectId, MIN(StatusHistoryId) AS FirstStatusHistoryId
FROM 
   dbo.StatusHistory
WHERE 
   ObjectType = 'SchemeTypeApplication' 
   AND (StatusId = 504 OR StatusId = 501) 
   AND IsDeleted = 0    
GROUP BY 
   ObjectId

This takes around 2 minutes to complete on it’s own, it pulls back nearly 300k rows. The whole query (with this inside) takes around about the same. Without the derived table, it takes less than a second, so I know it’s the derieved query that causing a problem.

My question is, is there anyway to improve the speed of the dereived table query? Maybe adding some indexes to the StatusHistory table (I’m a bit rubbish with indexing…)? Or some other approach other than using a derived table?

Any suggestions are appreciated.

Thanks

  • 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-29T06:24:23+00:00Added an answer on May 29, 2026 at 6:24 am

    As JonH noted, expand an index and have the key elements based on what would be considered the smallest granularity of expected results based on you WHERE criteria… Your “ObjectType” being a string might be better optimized if it were more an an enumerable integer value basis than text too. As for granularity. Which would return the least amount of records… status ID being 501 and 504 combined (via your OR condition) vs the object type of “SchemeTypeApplication”. If 501 has 50,000 records, 504 has 30,000 records, but “SchemeTypeApplication” has 475,000 records, I would have my index on the status ID first, otherwise, vice-versa… Let the index blow through 80,000 records and within that, how many of the 475,000 object types are 501 and 504, maybe 50,000 and you’re done. Yes, your original comment refers to returning about 300k rows, but this is just a sample of determination of an optimizing index technique based on how your queries will commonly be used. So, I would index on something like

    (statusID, ObjectType, IsDeleted, ObjectID)

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

Sidebar

Related Questions

So I have a SQL table that contains time-clock information like this. The number
I have a sql query that performs the type of select I'm after: select
I have a model which contains a link to sql query that fetches information
Basic SQL statement question - I have a table (myUsers) that contains the column
I have a single column table which looks like this: Gaming | Austria |
This is for MySQL and PHP I have a table that contains the following
In a SQL query, that contains ... WHERE MYID = @1 .... I have
I have list of MyObject that looks like this: public class MyObject{ public int
I have this SQL query but its running soo slow, SELECT wr.wr_re_id as ReID,
Friends, I have a dynamic sql query that I would like to execute and

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.