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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:16:47+00:00 2026-05-28T17:16:47+00:00

I have a rather large SQL query which the majority of which has come

  • 0

I have a rather large SQL query which the majority of which has come from a Business Objects SQL export which must be replicated on SQL Server for reporting purposes.

The view is now complete and runs however due to the mass amount of clauses is filling up the tempdb very quickly and falling over.

As far as I kno i need everything in the WHERE clauses however I feel this could be optimised to not fill the tempdb.

Where clauses as follows:

WHERE

dbo.VIEW1.ABSENCE_TYPE  IN  ('leavetype1', 'leavetype2', 'leavetype3', 'leavetype4', 'leavetype5')

AND
  ( 
  (ALIAS1.link_type_id='PN_RP_UNIT' or ALIAS1.link_type_id IS NULL)  )
  AND  ( ALIAS2.OBJECT_TYPE='POSITION'  )
  AND  ( ALIAS3.object_type='UNIT'  )
  AND  ( CONTRACT_NAME.object_type='CONT'  )
  AND  ( (POST_LNK.link_type_id='POSITION' OR POST_LNK.link_type_id IS NULL)  )
  AND  ( dbo.table1.link_type_id = 'CONT'  )
  AND  ( dbo.table1.team_party_id IS NULL  )
  AND  ( EMP_TPERSON.non_emp_s='0'  )
  --AND  (( ( dbo.tleaver.starting_d ) <= @Prompt('Report To Date','D',,Mono,Free) AND (( dbo.tleaver.leaving_d ) >= @Prompt('Report From Date','D',,Mono,Free) OR ( dbo.tleaver.leaving_d ) is null)  )
  AND  dbo.VIEW4.UNIT_NM_01  =  'Orgainisation Name'

  AND  ALIAS2.PARTY_NM  NOT LIKE  '%occ1%'
  AND  ALIAS2.PARTY_NM  NOT LIKE  '%occ2%'
  AND  dbo.VIEW2.CONTRACTUAL_HOURS  !=  0
  AND  dbo.VIEW6.FTE_HOURS  !=  0
 AND  ( (ALIAS1.PARTY_LNK_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR ALIAS1.PARTY_LNK_D IS NULL) AND (ALIAS1.PARTY_LNK_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR ALIAS1.PARTY_LNK_ED IS NULL)  )
  AND  ( (( dbo.table1.PARTY_LNK_D ) <= ( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.table1.PARTY_LNK_D ) is null) AND (( dbo.table1.PARTY_LNK_ED ) >= ( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.table1.PARTY_LNK_ED ) is null)  )
  --AND  dbo.VIEW1.ABSENCE_START_DATE  <=  @variable('report to date')
  --AND  (dbo.VIEW1.ABSENCE_END_DATE  >=  @variable('report from date')
 -- OR   dbo.VIEW1.ABSENCE_END_DATE  IS NULL  )
  --AND  dbo.VIEW3.ABS_DAY_DATE  >=  @variable('report from date')
  --AND  dbo.VIEW3.ABS_DAY_DATE  <=  @variable('report to date')
  AND  ( (POST_LNK.PARTY_LNK_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR POST_LNK.PARTY_LNK_D is null) AND (POST_LNK.PARTY_LNK_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR POST_LNK.PARTY_LNK_ED is null)  )
  AND  ( (dbo.VIEW2.INHERITED_FROM_DATE<=( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW2.INHERITED_FROM_DATE IS NULL) AND (dbo.VIEW2.INHERITED_TO_DATE>=( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW2.INHERITED_TO_DATE IS NULL) AND (dbo.VIEW2.HOURS_START_DATE<=( dbo.VIEW3.ABS_DAY_DATE )  OR dbo.VIEW2.HOURS_START_DATE IS NULL) AND (dbo.VIEW2.HOURS_END_DATE>=( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW2.HOURS_END_DATE IS NULL)  )
  AND  ( (dbo.VIEW6.INHERITED_FROM_DATE<=( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW6.INHERITED_FROM_DATE IS NULL) AND (dbo.VIEW6.INHERITED_TO_DATE>=( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW6.INHERITED_TO_DATE IS NULL) AND (dbo.VIEW6.HOURS_START_DATE<=( dbo.VIEW3.ABS_DAY_DATE )  OR dbo.VIEW6.HOURS_START_DATE IS NULL) AND (dbo.VIEW6.HOURS_END_DATE>=( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW6.HOURS_END_DATE IS NULL)  )
  AND  ( (dbo.VIEW4.LNK_01_02_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_01_02_D IS NULL)  AND (dbo.VIEW4.LNK_02_03_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_02_03_D IS NULL)  AND (dbo.VIEW4.LNK_03_04_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_03_04_D IS NULL)  AND (dbo.VIEW4.LNK_04_05_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_04_05_D IS NULL)  AND (dbo.VIEW4.LNK_05_06_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_05_06_D IS NULL)  AND (dbo.VIEW4.LNK_06_07_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_06_07_D IS NULL)  AND (dbo.VIEW4.LNK_07_08_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_07_08_D IS NULL)  AND (dbo.VIEW4.LNK_08_09_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_08_09_D IS NULL)  AND (dbo.VIEW4.LNK_09_10_D <= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_09_10_D IS NULL)  AND (dbo.VIEW4.LNK_01_02_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_01_02_ED IS NULL)  AND (dbo.VIEW4.LNK_02_03_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_02_03_ED IS NULL)  AND (dbo.VIEW4.LNK_03_04_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_03_04_ED IS NULL)  AND (dbo.VIEW4.LNK_04_05_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_04_05_ED IS NULL)  AND (dbo.VIEW4.LNK_05_06_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_05_06_ED IS NULL)  AND (dbo.VIEW4.LNK_06_07_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_06_07_ED IS NULL)  AND (dbo.VIEW4.LNK_07_08_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_07_08_ED IS NULL)  AND (dbo.VIEW4.LNK_08_09_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_08_09_ED IS NULL)  AND (dbo.VIEW4.LNK_09_10_ED >= ( dbo.VIEW3.ABS_DAY_DATE ) OR dbo.VIEW4.LNK_09_10_ED IS NULL)  )

  AND  ALIAS2.PARTY_NM  NOT LIKE  'pos1'
  AND  dbo.VIEW5.contract_category  IN  ('pos2', 'pos3', 'pos4', 'pos4')
  AND  ( (( dbo.VIEW5.inherited_from_date )<=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.inherited_from_date ) IS NULL) AND (( dbo.VIEW5.inherited_to_date )>=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.inherited_to_date ) IS NULL) AND (( dbo.VIEW5.contract_category_d )<=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.contract_category_d ) IS NULL) AND (( dbo.VIEW5.contract_category_ed )>=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.contract_category_ed ) IS NULL)  )
  AND  ALIAS2.PARTY_NM  NOT LIKE  'role1'
  AND  ALIAS2.PARTY_NM  NOT LIKE  'role2'
  AND  ALIAS2.PARTY_NM  NOT LIKE  '%role3%'

  AND  ( (( dbo.VIEW4.inherited_from_date )<=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW4.inherited_from_date ) IS NULL) AND (( dbo.VIEW4.inherited_to_date )>=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW4.inherited_to_date ) IS NULL) AND (( dbo.VIEW4.contract_basis_d )<=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW4.contract_basis_d ) IS NULL) AND (( dbo.VIEW4.contract_basis_ed )>=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW4.contract_basis_ed ) IS NULL)  )
  AND  dbo.VIEW5.contract_category  IN  ('pos1', 'pos2', 'pos3', 'pos4')
  AND  ( (( dbo.VIEW5.inherited_from_date )<=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.inherited_from_date ) IS NULL) AND (( dbo.VIEW5.inherited_to_date )>=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.inherited_to_date ) IS NULL) AND (( dbo.VIEW5.contract_category_d )<=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.contract_category_d ) IS NULL) AND (( dbo.VIEW5.contract_category_ed )>=( dbo.VIEW3.ABS_DAY_DATE ) OR ( dbo.VIEW5.contract_category_ed ) IS NULL)

Onece ran this should bring back around 50,000 rows of data, can anyone make any suggestions as to how I can stop putting so much pressure on the tempdb and run this query.

  • 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-28T17:16:49+00:00Added an answer on May 28, 2026 at 5:16 pm

    All constructs like this:

    (POST_LNK.link_type_id='POSITION' OR POST_LNK.link_type_id IS NULL)
    

    Can be re-factored to

    ISNULL(POST_LNK.link_type_id,'POSITION') = 'POSITION'
    

    Just because the where clause is “the slow down” does not mean there are not re-factoring that can happen with the rest of the query. Often times joins and sub-queries can be modified to have constraints and give huge speed boosts because there are less records being inspected by the outer query.


    Also if I were to take WAG I’d say your problem is with the where elements like this:

    AND  ALIAS2.PARTY_NM  NOT LIKE  '%occ1%'
    AND  ALIAS2.PARTY_NM  NOT LIKE  '%occ2%'
    AND  ALIAS2.PARTY_NM  NOT LIKE  '%role3%'
    

    You might consider re-factoring to remove this part if it is the problem (for example adding an occ boolean column and updating it on insert and modify.)


    I also noticed you are joining to a number of views, that could be the problem if the views are doing something dumb like making function calls. If a column is not used SQL might optimize these slow downs out, but re-add them if you are using the element in the where.

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

Sidebar

Related Questions

I have a rather large SQL file which starts with the byte order marker
This one has me rather confused. I've written a query which runs fine from
I have a rather large (many gigabytes) table of data in SQL Server that
I have rather large input XML files that must be converted to a given
I have a rather large (freeware) project written with Delphi 2007 which is using
I have a rather large project which contains a number of third-party dependencies which
I have a rather large database exposed to my application using LINQ-to-SQL. I need
I have a rather large linq-to-nhibernate query. I now need to add a filter
I have a rather large file (150 million lines of 10 chars). I need
We have a rather large SVN repository. Doing SVN updates are taking longer 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.