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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:46:25+00:00 2026-05-27T10:46:25+00:00

I have to optimize this query can some help me fine tune it so

  • 0

I have to optimize this query can some help me fine tune it so it will return data faster?

Currently the output is taking somewhere around 26 to 35 seconds. I also created index based on attachment table following is my query and index:

SELECT DISTINCT o.organizationlevel, o.organizationid, o.organizationname, o.organizationcode,
      o.organizationcode + ' - ' + o.organizationname AS 'codeplusname'
 FROM Organization o
 JOIN Correspondence c ON c.organizationid = o.organizationid
 JOIN UserProfile up ON up.userprofileid = c.operatorid
WHERE c.status = '4'
  --AND c.correspondence > 0
  AND o.organizationlevel = 1
  AND (up.site = 'ALL' OR
                  up.site = up.site)
  --AND (@Dept = 'ALL' OR @Dept = up.department)
  AND EXISTS (SELECT 1 FROM Attachment a
               WHERE a.contextid = c.correspondenceid
                 AND a.context = 'correspondence'
                 AND ( a.attachmentname like '%.rtf' or  a.attachmentname like '%.doc'))
ORDER BY o.organizationcode

I can’t just change anything in db due to permission issues, any help would be much appreciated.

  • 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-27T10:46:26+00:00Added an answer on May 27, 2026 at 10:46 am

    I believe your headache is coming from this part in specific…like in a where exists can be your performance bottleneck.

     AND EXISTS (SELECT 1 FROM Attachment a
               WHERE a.contextid = c.correspondenceid
                 AND a.context = 'correspondence'
                 AND ( a.attachmentname like '%.rtf' or  a.attachmentname like '%.doc'))
    

    This can be written as a join instead.

    SELECT DISTINCT o.organizationlevel, o.organizationid, o.organizationname, o.organizationcode,
      o.organizationcode + ' - ' + o.organizationname AS 'codeplusname'
    FROM Organization o
    JOIN Correspondence c ON c.organizationid = o.organizationid
    JOIN UserProfile up ON up.userprofileid = c.operatorid
    left join article a on a.contextid = c.correspondenceid
                 AND a.context = 'correspondence'
                 and right(attachmentname,4) in ('.doc','.rtf')
    
    ....
    

    This eliminates both the like and the where exists. put your where clause at the bottom.it’s a left join, so a.anycolumn is null means the record does not exist and a.anycolumn is not null means a record was found. Where a.anycolumn is not null will be the equivalent of a true in the where exists logic.

    Edit to add:
    Another thought for you…I’m unsure what you are trying to do here…

    AND (up.site = ‘ALL’ OR
    up.site = up.site)

    so where up.site = ‘All’ or 1=1? is the or really needed?

    and quickly on right…Right(column,integer) gives you the characters from the right of the string (I used a 4, so it’ll take the 4 right chars of the column specified). I’ve found it far faster than a like statement runs.

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

Sidebar

Related Questions

I'm trying to optimize this mysql query using EXPLAIN. Can somebody please help me
I just can't figure out how to optimize this into one query in HQL.
I have a Django query and some Python code that I'm trying to optimize
I have been working on this for 24 hours now, trying to optimize it.
I have written this code to join ArrayList elements: Can it be optimized more?
I have been tasked to optimize some sql queries at work. Everything I have
I have a search query that I'm inheriting and attempting to optimize. I am
I'm trying to optimize query performance and have had to resort to using optimizer
I currently have a website that I am trying to optimize in terms of
I have a (C#) genetic program that uses financial time-series data and it's currently

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.