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.
All constructs like this:
Can be re-factored to
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:
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.