May 15, 2013 at 6:01 am
Below are the IO Statistics for a query before and after optimization.
IO Stats are different but actual execution time on production is same for both the queries.
Now I want to go with optimized query but my manager want to revert the changes as there is no time gain.
What will be your choice in this case!!!
QUERY 1 I/O STATISTICS after Optimization
(20998 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'company'. Scan count 1, logical reads 1027, physical reads 0, read-ahead reads 1, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'person'. Scan count 1, logical reads 646153, physical reads 266, read-ahead reads 430590, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'worker'. Scan count 1, logical reads 24215, physical reads 0, read-ahead reads 50, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'buyer_supplier_contract'. Scan count 1, logical reads 140, physical reads 0, read-ahead reads 47, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
QUERY 2 I/O STATISTICS before Optimization
(20998 row(s) affected)
Table 'buyer_supplier_contract'. Scan count 20998, logical reads 44642, physical reads 0, read-ahead reads 1, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'company'. Scan count 1, logical reads 1026, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'person'. Scan count 3, logical reads 1338694, physical reads 399, read-ahead reads 450197, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'worker'. Scan count 1, logical reads 24215, physical reads 0, read-ahead reads 16, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
May 15, 2013 at 6:30 am
There are quite a few different things you can "tune" a query to "optimize/minimize": duration, IO, CPU, Memory, locking, etc. In your case it would seem that you have reduced IO (likely from eliminating nested loop activities) but kept duration roughly the same (likely from an increase in CPU usage to do HASH joins, but that is just an educated guess since you didn't provide the actual query plans).
If the CPU usage is higher on the new query then you can make a secondary decision that you want to minimize that, and thus revert back to the original.
Another consideration is concurrency/locking. One plan could cause many more blocks to occur than another and that could be the thing you need to minimize in a busy environment.
Good tuning takes a LOT of knowledge, experience, some art and even sometimes just plain dumb luck. I have 40000+ hours dealing with just the SQL Server relational engine and sometimes I would still rather be lucky than good!! 😎
Oh, another potential reason for reverting back to the original query: you hopefully have some reasonable certainty that it produces the correct results EVERY time. That may not be a true statement for the refactor - even if it is a "trivial" change. There are bugs and side-effects that present a tiny but non-zero chance of a regression.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
May 15, 2013 at 10:34 pm
Both queries are tested and giving proper results.
May 15, 2013 at 10:41 pm
I have attached both queries for your reference. Execution plan could not be attached.
Do we have some other way to write these queries?
May 16, 2013 at 2:59 am
You can start by taking the nolock hints out.
See - http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx
Unless, of course, all the users know and accept that the data returned by the query can be wrong and are fine with that.
Will need to see the exec plan to advise any further.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 18, 2013 at 7:10 am
Gail,
Just ignore nolock in any of my posts (client policy).
I still need an answer.
May 18, 2013 at 7:32 am
GilaMonster (5/16/2013)
Will need to see the exec plan to advise any further.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 18, 2013 at 11:37 pm
Replace
LEFT JOIN (
SELECT
msp_flag,
bc_code
FROM
dbo.bs_contract(nolock)
GROUP BY
msp_flag,
bc_code
) F ON F.bc_code = C.comp_code
AND F.msp_flag = 1
with
LEFT JOIN dbo.bs_contract F (nolock)
ON F.bc_code = C.comp_code AND F.msp_flag = 1
Probably wont make any performance difference
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply