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

  • SEARCH
  • Home
  • 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 6540759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:01:08+00:00 2026-05-25T11:01:08+00:00

I’m trying to optimise my query. Basically I have an Department table and a

  • 0

I’m trying to optimise my query. Basically I have an Department table and a document table. Each document belongs to a department and each document can be a specific type.

Currently mu query looks like this

SELECT  DepartmentID,
        [Description] = DepartmentNo + ' (' + DepartmentName + ')',
        hasInvoice = CASE WHEN EXISTS(SELECT DocID FROM Document WHERE DepartmentID = Department.DepartmentID AND TypeID = 1) > 0 THEN 1 ELSE 0 END,
        hasCreditNote = CASE WHEN EXISTS(SELECT DocID FROM Document WHERE DepartmentID = Department.DepartmentID AND TypeID = 2) > 0 THEN 1 ELSE 0 END,
        hasQuote = CASE WHEN EXISTS(SELECT DocID FROM Document WHERE DepartmentID = Department.DepartmentID AND TypeID = 3) > 0 THEN 1 ELSE 0 END
FROM  Department
ORDER BY DepartmentName

The problem I have is that for each lookup of the documents relating to the department by their type causes the query to rescan through the document table which makes things really slow. Is there a more optimised way of doing this? I’ve tried using a CTE, Doing a left join for each type. but it seems to make no impact to making the query faster.

We have roughly 200000 documents, so it’s quite an important thing to get this query optimised.

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-25T11:01:08+00:00Added an answer on May 25, 2026 at 11:01 am

    Whatever the indexes, you have too many “touches” on the Document table: 3 times per row in the Department table. So it will scale badly and the optimiser may not change the correlated sub-queries into JOINs.

    You can do with a single “touch” on the Document table like this using a JOIN

    SELECT
        D.DepartmentID,
        [Description] = D.DepartmentNo + ' (' + D.DepartmentName + ')',
        hasInvoice = SIGN(COUNT(CASE WHEN doc.TypeID = 1 THEN 1 END)),
        hasCreditNote = SIGN(COUNT(CASE WHEN doc.TypeID = 2 THEN 1 END)),
        hasQuote = SIGN(COUNT(CASE WHEN doc.TypeID = 3 THEN 1 END))
    FROM 
        Department D
        LEFT JOIN
        Document doc ON D.DepartmentID = doc.DepartmentID
    ORDER BY
        D.DepartmentName, D.DepartmentID, D.DepartmentNo
    

    You can also LEFT JOIN 3 times on an filtered+aggregated Document derived table or use CROSS APPLY 3 times: but these are still 3 uses of the Document table

    You need an index on DepartmentID, TypeID in Documents for any solution. Without this, I’ve only re-arranged the deck chairs so to speak

    I assume Department.DepartmentID is a clustered index too.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a jquery bug and I've been looking for hours now, I can't
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.