Query kill CPU | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Query kill CPU

This query is killing my CPU.<br />I can´t find any index to improve it.<br />May be there is a better way to write it.<br />Any clue?<br /><br />select it.coditm, tc.descripcion<br />from itemsmedcol it <br />join tabcoloresdet tcd on it.codcol = tcd.codcol <br />join tabcolores tc on tcd.codtabcol = tc.codtabcol<br />group by tc.descripcion, it.coditm<br />order by coditm <br /><br /><br /><br /> |–Sort(DISTINCT ORDER BY<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[it].[CODITM] ASC, [tc].[DESCRIPCION] ASC))<br /> |–Hash Match(Inner Join, HASH<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODCOL])=([it].[CODCOL]), RESIDUAL<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[it].[CODCOL]=[tcd].[CODCOL]))<br /> |–Merge Join(Inner Join, MERGE<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tc].[CODTABCOL])=([tcd].[CODTABCOL]), RESIDUAL<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODTABCOL]=[tc].[CODTABCOL]))<br /> | |–Clustered Index Scan(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[TABCOLORES].[PK_TABCOLORES] AS [tc]), ORDERED FORWARD)<br /> | |–Clustered Index Scan(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[TABCOLORESDET].[pk_tabcoloresdet] AS [tcd]), ORDERED FORWARD)<br /> |–Index Scan(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[ITEMSMEDCOL].[ITEMSMEDCOL_CODITM] AS [it]))<br /><br />Luis Martin<br />Moderator<br />SQL-Server-Performance.com<br /><br /><font size="1">One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important<br />Bertrand Russell<br /></font id="size1"><br /><br /><font size="1">All postings are provided “AS IS” with no warranties for accuracy.</font id="size1"><br /><br /><br /><br />
1. Try to force nonclustered index (if you have any, if not make it nonclustered and test) on tabColoresDet.codcol
2. Do you have an composite index on itemsMedCol columns codCol and coditm? If so try to force that index to be used.
3. Try forcing different join alghorithms and maybe force specific join order. It is hard to give you more precise suggestions without knowlege of table structure, data distribution and the purpose of each table. Is table tabColores lookup table containing colors? What can be color detail if it is the meaning of tabColoresDet? What is in itemsMedCol table? How many rows do you have in each?

Mmarovic: 1) Ok.
2) Yes.
3) TabColores CodTabCol Descripcion
000 Marron-Negro
E17 Azul-Amarillo
… …………. 2940 Rows TabColoresDet CodTabcol CodCol Secuencia
000 MAR 1
000 NGO 2
E17 AMA 2
E17 AZL 1
… … . 7568 Rows ItemsMedcol CodItm CodItemAlternativo Codtal CodCol
ABE00014 NULL CAN NGO
…… …. … … 13596 Rows. Now, I rewrite the query in the following way: select it.coditm,
tc.descripcion
from itemsmedcol it with (nolock),
tabcoloresdet tcd,
tabcolores tc
WHERE COALESCE(tcd.codtabcol, tcd.codtabcol) = tc.codtabcol
AND it.codcol = tcd.codcol
group by tc.descripcion,
it.coditm
order by coditm
OPTION (LOOP JOIN) And now, after create noncluster Tabcoleresdet on CodCol, Codtabcol
and noncluster ItemsMedcol on Codcol, Coditm It run 40% faster and don’t kill my CPU. But I’m not happy yet. Luis Martin
Moderator
SQL-Server-Performance.com One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important
Bertrand Russell
All postings are provided “AS IS” with no warranties for accuracy.
I forgot: I can’t change any database table (3rd software) and I’m helping people who are designing web using that database, so any help with index, code is welcome.
Luis Martin
Moderator
SQL-Server-Performance.com One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important
Bertrand Russell
All postings are provided “AS IS” with no warranties for accuracy.
What is performance of original query with new indexes? Edited: I think i missunderstood table structure, ignore my previous comment.
CodItm is not unique, right? Otherwise query wouldn’t make sense.
I would try reverse column order too: nonclustered ItemsMedcol on Coditm, Codcol in that order, that should eliminate sorting. I would rather force original query, something like:
select it.coditm, tc.descripcion
from itemsmedcol it (index = idx_coditm_codcol)
inner loop join tabcoloresdet tcd on it.codcol = tcd.codcol
inner loop join tabcolores tc on tcd.codtabcol = tc.codtabcol
group by tc.descripcion, it.coditm
order by coditm
option (force order)

Will try, tomorrow I back with news.
Thanks. Luis Martin
Moderator
SQL-Server-Performance.com One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important
Bertrand Russell
All postings are provided “AS IS” with no warranties for accuracy.
Your query run in 1m 42s and: (1516516 row(s) affected) Table ‘TABCOLORES’. Scan count 4547210, logical reads 9110659, physical reads 0, read-ahead reads 0.
Table ‘TABCOLORESDET’. Scan count 13586, logical reads 35360, physical reads 0, read-ahead reads 0.
Table ‘ITEMSMEDCOL’. Scan count 1, logical reads 101, physical reads 0, read-ahead reads 0.
My query run in 35s and: (1516516 row(s) affected) Table ‘ITEMSMEDCOL’. Scan count 5530, logical reads 29587, physical reads 0, read-ahead reads 0.
Table ‘TABCOLORES’. Scan count 7568, logical reads 15176, physical reads 0, read-ahead reads 0.
Table ‘TABCOLORESDET’. Scan count 1, logical reads 11, physical reads 0, read-ahead reads 0. Thanks again.
Luis Martin
Moderator
SQL-Server-Performance.com One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important
Bertrand Russell
All postings are provided “AS IS” with no warranties for accuracy.
I forgot to put noLock hint too. Anyway, can you please post the execution plan of both queries? I must admit I’m confused right now. The data model is strange and data distribution is not what I expected. Joining 3 tables up to 14000 rows and grouping by query returns more then 1.5 M rows. Also I don’t understand the need for coalesce function.


select it.coditm, tc.descripcion
from itemsmedcol it
inner join tabcoloresdet tcd
inner join tabcolores tc
on tcd.codtabcol = tc.codtabcol
on it.codcol = tcd.codcol
group by tc.descripcion, it.coditm
order by coditm
Mmarovic:<br /><br />Your plan:<br /><br />|–Sort(DISTINCT ORDER BY<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[it].[CODITM] ASC, [tc].[DESCRIPCION] ASC))<br /> |–Nested Loops(Inner Join, OUTER REFERENCES<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODTABCOL]))<br /> |–Nested Loops(Inner Join, OUTER REFERENCES<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[it].[CODCOL]) WITH PREFETCH)<br /> | |–Index Scan(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[ITEMSMEDCOL].[IXCCTP050719_ITEMSMEDCOL_CodItm_CodCol_Power01] AS [it]))<br /> | |–Index Seek(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[TABCOLORESDET].[IXCCTP050719_TABCOLORESDET_CodCol_CodTabcol_Power01] AS [tcd]), SEEK<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODCOL]=[it].[CODCOL]) ORDERED FORWARD)<br /> |–Clustered Index Seek(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[TABCOLORES].[PK_TABCOLORES] AS [tc]), SEEK<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tc].[CODTABCOL]=[tcd].[CODTABCOL]) ORDERED FORWARD)<br /><br /><br />My plan:<br /><br />|–Stream Aggregate(GROUP BY<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[it].[CODITM], [tc].[DESCRIPCION]))<br /> |–Sort(ORDER BY<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[it].[CODITM] ASC, [tc].[DESCRIPCION] ASC))<br /> |–Nested Loops(Inner Join, OUTER REFERENCES<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODCOL]) WITH PREFETCH)<br /> |–Hash Match(Aggregate, HASH<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODCOL], [tc].[DESCRIPCION]), RESIDUAL<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODCOL]=[tcd].[CODCOL] AND [tc].[DESCRIPCION]=[tc].[DESCRIPCION]))<br /> | |–Nested Loops(Inner Join, OUTER REFERENCES<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tcd].[CODTABCOL]))<br /> | |–Index Scan(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[TABCOLORESDET].[IXCCTP050719_TABCOLORESDET_CodCol_CodTabcol_Power01] AS [tcd]))<br /> | |–Clustered Index Seek(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[TABCOLORES].[PK_TABCOLORES] AS [tc]), SEEK<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[tc].[CODTABCOL]=If 1 then [tcd].[CODTABCOL] else [tcd].[CODTABCOL]) ORDERED FORWARD)<br /> |–Index Seek(OBJECT<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[JHR].[dbo].[ITEMSMEDCOL].[IXCCTP050719_ITEMSMEDCOL_CodItm_CodCol_Power01] AS [it]), SEEK<img src=’/community/emoticons/emotion-6.gif’ alt=’:(‘ />[it].[CODCOL]=[tcd].[CODCOL]) ORDERED FORWARD)<br /><br /><br /><br />Luis Martin<br />Moderator<br />SQL-Server-Performance.com<br /><br /><font size="1">One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important<br />Bertrand Russell<br /></font id="size1"><br /><br /><font size="1">All postings are provided “AS IS” with no warranties for accuracy.</font id="size1"><br /><br /><br /><br />
]]>