sql - improvement guide

8
Improvement Guide Improve Stored Procedure Performance Improvement Guide Revision History Author Revision Number Date TCSASSEMBER 1.0 22/06/2014 Improvement Guide TopCoder, Inc. 2014 Page 1 of 8

Upload: reuif

Post on 21-Jul-2016

8 views

Category:

Documents


1 download

DESCRIPTION

SQL - Improvement Guide

TRANSCRIPT

Page 1: SQL - Improvement Guide

Improvement Guide

Improve Stored Procedure PerformanceImprovement Guide

Revision HistoryAuthor Revision Number DateTCSASSEMBER 1.0 22/06/2014

Improvement Guide TopCoder, Inc. 2014 Page 1 of 7

Page 2: SQL - Improvement Guide

Improvement Guide

Improvement Instructions 3

1. Organization of Submission 3

2. Improvement Tips 3

3. Resource Contact List 7

Improvement Guide TopCoder, Inc. 2014 Page 2 of 7

Page 3: SQL - Improvement Guide

Improvement Guide

1. Improvement Instructions

2. Organization of Submission

The submission follows directory structure provided below:

!!!README!!!.txtRead first file.

SQL - Improvement Guide.docThis guide that you are reading

SQLExecutionPlanExport.csvSQL Server 2008 Execution Plan export whos estimates are based on small data-file we had.

spSearchAnalysesFixed.sqlStored Procedure with minor fixes unrelated to improvement of performance

3. Improvement Tips

Since there is not a good test-data case to test my assumptions (since data is needed by SQL Execution Planner to make predictions about how long will execution of specific tasks last), here are couple of hints to the client that can help them solve their problem.

0.The provided procedure has mistakes in it when applied to existing code and will not work with what we were provided. I've made some minor corrections to fix this and the script is located:

spSearchAnalysesFixed.sql

1.Indexes are very important - there is possibility that something is not indexed and causes the execution to be slow.

2.This looks slow:

AND (@invoice IS NULL OR EXISTS(SELECT (1) FROM Invoice WHERE [Invoice].[Project] = [Project].[Project] AND [Invoice].[Client] = [Project].[Client] AND [Invoice].[LabID] = [Project].[LabID] AND [Invoice].[Invoice] LIKE '%' + @invoice + '%'))AND (@method IS NULL OR @method = [REPSAMPLEANALYSIS1].[SpecificMethod])

- It probably won't use any index because of the OR and maybe the dynamic @invoice and @method and it may be running the SELECT statement repeatedly internally with the join. It is at least complicated and likely to confuse your logic now or in future modifications. That is, both your own design, and the logic of the query planner.- You are putting this logic of @variable IS NULL into the query itself. I would build the query dynamically, checking whether @variable IS NULL outside of the query.

Improvement Guide TopCoder, Inc. 2014 Page 3 of 7

Page 4: SQL - Improvement Guide

Improvement Guide

3.All likes with prefixed % are slow.

4.When executing the procedure:

exec spSearchAnalyses @pageNumber=1,@pageSize=10,@sortColumn=N'SampleDate',@sortAscending=N'N',@username=N'[email protected]',@isRecent=N'Y',@keyword=NULL,@sampleDescription=NULL,@sampleId=NULL,@workOrder=NULL,@invoice=NULL,@locationId=NULL,@method=NULL,@sampledDateStart=NULL,@sampledDateEnd=NULLGO

You can right click inside the query window of where you are executing the procedure and choseInclude Actual Execution Plan and than Display Estimated Execution Plan:

Improvement Guide TopCoder, Inc. 2014 Page 4 of 7

Page 5: SQL - Improvement Guide

Improvement Guide

When you get the execution plan, you can hover over individual objects. Pay attention to „Estimated Subtree Cost“

5.You can use this query to execute the procedure and to get cost of execution plan in table form:

SET SHOWPLAN_ALL ONGO

-- FMTONLY will not exec stored procSET FMTONLY ONGO

exec spSearchAnalyses @pageNumber=1,@pageSize=10,@sortColumn=N'SampleDate',@sortAscending=N'N',@username=N'[email protected]',@isRecent=N'Y',@keyword=NULL,@sampleDescription=NULL,@sampleId=NULL,@workOrder=NULL,@invoice=NULL,@locationId=NULL,@method=NULL,@sampledDateStart=NULL,@sampledDateEnd=NULLGO

SET FMTONLY OFFGO

SET SHOWPLAN_ALL OFFGO

It will look like this once executed (pay attention to the column TotalSubtreCost):

Improvement Guide TopCoder, Inc. 2014 Page 5 of 7

Page 6: SQL - Improvement Guide

Improvement Guide

And, again, a key thing to call out here is that these costs (estimated or otherwise) are based on SQL Server’s knowledge of the size of your tables as well as the cardinality and distribution of your data. Or, in other words, these costs are based upon statistics about your data. They’re not, therefore, something ‘tangible’ like the number of milliseconds associated with an operation. As such, the best way to think of them is that lower numbers are better – unless you want to try and get into some of the nitty-gritty details about how these costs are calculated (which, again, is proprietary information or part of SQL Server’s ‘secret sauce’).

With that said, there’s still a way to ‘frame’ these costs – to provide an idea of what costs roughly mean in the ‘real’ world.

.003. Costs of .003 are about as optimized as you’re going to get when interacting with the storage engine (executing some functions or operations can/will come in at cheaper costs, but I’m talking here about full-blown data-retrieval operations).

.03. Obviously, costs of .03 are a full order of magnitude greater than something with a cost of .003 – but even these queries are typically going to be VERY efficient and quick – executing in less than a second in the vast majority of cases.

Queries with a cost of 1 aren’t exactly ugly or painfull (necessarily) and will typically take a second or less to execute. They’re not burning up lots of resources, but they’re also typically not as optimized as they could be (or they are optimized – but they’re pulling back huge amounts of data or filtering against very large tables).

5. Queries with a cost greater than 5, by default, will be executed with a parallel plan – meaning that SQL Server sees these queries as being large enough to throw multiple processors/cores/theads-of-execution at – in order to speed up execution. And, if you’ve got a

Improvement Guide TopCoder, Inc. 2014 Page 6 of 7

Page 7: SQL - Improvement Guide

Improvement Guide

web site that’s firing off a query with a cost of 5 or more per every page load, for example, you’ll probably notice that the page ‘feels’ a bit sluggish loading – maybe by a second or two – as compared to a page that would ‘spring up’ if it was running a query with a cost of, say, .2 or lower. So, in other words, queries up in this range start having a noticeable or appreciable ‘cost’.

20. Queries in this range are TYPICALLY going to be something you can notice taking a second or so. (Though, on decent hardware, they can still end up being instantaneous as well – so even at this point, things still depend on a lot of factors).

200. Queries with this kind of cost should really only be for larger reports and infrequently executed operations. Or, they might be serious candidates for the use of additional tuning and tweaking (in terms of code and/or indexes).

1000. Queries up in this range are what DBAs start to lovingly call ‘queries from hell’ – though it’s possible to bump into queries with costs in the 10s of thousands or even more – depending upon the operations being executed and the amount of data being poured over.

4. Resource Contact List

Name Resource EmailTCSASSEMBER

Improvement Guide TopCoder, Inc. 2014 Page 7 of 7