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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:31:16+00:00 2026-06-04T02:31:16+00:00

I have a SQL query that’s fairly complicated and involves multiple subqueries and unions.

  • 0

I have a SQL query that’s fairly complicated and involves multiple subqueries and unions. Despite the complexity, the subquery part executes very quickly (< 1ms once compiled). But when I run the query as a whole, it’s taking upwards of 60ms.

I’ve used “set statistics time on” and “set statistics io on” to track accesses of the base tables, and SQL is performing many more logical reads than necessary. When I run the subquery on it’s own, it only does 10 reads or so, but it’s around 1800 when the entire query is run.

I’d like to encourage SQL to evalute the subquery first – the expected number of rows is 1-5 so the data set is tiny. It should then be inner joined to the same base table, and pull back an estimated 10-20 records.

Is there a way to use hints to ensure SQL optimises for the expected cases? The query logic seems to be too much for the optimiser and it’s choosing an execution plan that would only be ideal for row sets far larger than will ever exist in my situation.

Edit: One of the conditions in the final join will filter out 99.99% of the rows in the base table. If I can force SQL to evaluate that first, it should do the trick. Is there a way to do this?

Query text here: http://pastebin.com/zVR91AP2

With the WHERE clause at the end commented out, the query returns 4 rows and runs in <1ms with 33 logical reads of the audit table.
But if I uncomment the WHERE clause, it returns 3 rows (1 filtered out) and takes 50ms with a whopping 1277 logical reads of the audit table. The io stats indicate that a work table is being used in the first instance, but not in the second. I want to encourage SQL to use this work table.

  • 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-04T02:31:17+00:00Added an answer on June 4, 2026 at 2:31 am

    Worked it out. The solution was to use a join hint, which forces the evaluation of the inner query before joining it to the larger table.

    inner REMOTE join dbo.poAudit… etc

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

Sidebar

Related Questions

I have a SQL query that has a subquery that has joins. I would
I have the following SQL query that runs fairly well, however it seems to
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a sql query that runs super fast, around one second, when not
I have a SQL query that takes a very long time to run on
We have a SQL query that pulls a large number of fields from many
I have a SQL query that I'm trying to debug. It works fine for
I have an SQL query that returns all companies records ignore the ones with
I have a SQL query that I'm trying to write, but I'm not quite
I currently have a SQL query that returns a number of fields. I need

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.