Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Which query to be used if IO Stats are different but actual execution time on production is same. Expand / Collapse
Author
Message
Posted Wednesday, May 15, 2013 6:01 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 4:22 AM
Points: 720, Visits: 552
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.
Post #1453034
Posted Wednesday, May 15, 2013 6:30 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:00 AM
Points: 4,410, Visits: 6,281
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 at GMail
Post #1453051
Posted Wednesday, May 15, 2013 10:34 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 4:22 AM
Points: 720, Visits: 552
Both queries are tested and giving proper results.
Post #1453318
Posted Wednesday, May 15, 2013 10:41 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 4:22 AM
Points: 720, Visits: 552
I have attached both queries for your reference. Execution plan could not be attached.

Do we have some other way to write these queries?


  Post Attachments 
query.doc (8 views, 16.00 KB)
Post #1453319
Posted Thursday, May 16, 2013 2:59 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 6:05 AM
Points: 40,258, Visits: 36,681
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 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1453377
Posted Saturday, May 18, 2013 7:10 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 4:22 AM
Points: 720, Visits: 552
Gail,
Just ignore nolock in any of my posts (client policy).
I still need an answer.
Post #1454253
Posted Saturday, May 18, 2013 7:32 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 6:05 AM
Points: 40,258, Visits: 36,681
GilaMonster (5/16/2013)
Will need to see the exec plan to advise any further.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1454256
Posted Saturday, May 18, 2013 11:37 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 24, 2014 7:06 PM
Points: 4, Visits: 21
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
Post #1454315
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse