Presentation is loading. Please wait.

Presentation is loading. Please wait.

JULIE McLAIN-HARPER LINKEDIN: JM HARPER

Similar presentations


Presentation on theme: "JULIE McLAIN-HARPER LINKEDIN: JM HARPER"— Presentation transcript:

1 JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Your host: JULIE McLAIN-HARPER LINKEDIN: JM HARPER

2 Optimizer, Statistics, TUNING SQL QUERIES
and TUNING SQL QUERIES WHERE DO I START??

3 Cost Based Optimizer aka CBO
the basic purpose of the Query Optimizer is to find an efficient execution plan for your query. The Query Optimizer must strike a balance between optimization time and plan quality.

4 Cost Based Optimizer – cont.
To estimate the cost of a plan, it estimates the cost of each physical operator in that plan using costing formulas that consider the use of resources such as I/O, CPU, and memory. cost estimation depends mostly on the algorithm used by the physical operator, as well as the estimated number of records that will need to be processed, aka cardinality estimation.

5 Cost Based Optimizer – cont.
while the SQL Server Query Optimizer can typically consider a large number of candidate execution plans, it cannot perform an exhaustive search of all the possible plans for every query. If it did, then the time taken to assess all of the plans would be unacceptably long, and could start to have a major impact on the overall query execution time. DID YOU KNOW? The optimizer can reorganize, merge, and process the query in any order, as SQL is a non procedural language.

6 Cost Based Optimizer Uses Statistics
HOW DOES CARDINALITY ESTIMATION ASSIST THE OPTIMIZER IN FIGURING OUT THE MOST EFFICIENT PATH TO GETTING YOUR DATA??? CE affects the join strategy, memory grant requirement, worker thread selection as well as choice of indexes when accessing data. To help with this cardinality estimation, SQL Server uses and maintains optimizer statistics, which contain statistical information describing the distribution of values in one or more columns of a table.

7 Where are the Statistics? How do they get there?
Index statistics: Created automatically when an index (clustered and non-clustered) is created. These will have the same name as the index and will exist as long as the index exists.

8 Where are the Statistics? How do they get there?
Column statistics: Ways they get there: can be created manually by the DBA using the ‘CREATE STATISTICS’ command Automatically if the “Auto Create Statistics” option is set to “True”. Column statistics can be created, modified and dropped at will.

9 How do I know what the Statistics Are. When do Statistics Change
How do I know what the Statistics Are? When do Statistics Change? What happens when Data causes Statistics to change?

10 Julie’s Tool Time Key information
dbcc show_statistics('clinic', 'PK_CLINIC') Key information Updated: Last time Statistics were updated Density: high density --> less unique data Ex: Avg CountryID=100 vals, Avg RegionID=1000 vals the highest possible density, all same val. 1/# of rows – Primary Key Density

11 sp_help ‘table name’ sp_help ‘function name’
Julie’s Tool Time – cont. sp_help ‘table name’ sp_help ‘function name’

12 sp_helpindex ‘table name’
Julie’s Tool Time – cont. sp_helpindex ‘table name’

13 Sys.indexes and OBJECT_ID
Julie’s Tool Time – cont. Sys.indexes and OBJECT_ID USE JuliesTestDB GO SELECT name AS index_name, STATS_DATE(OBJECT_ID, index_id) AS StatsUpdated FROM sys.indexes WHERE OBJECT_ID = OBJECT_ID(‘dbo.JuliesTestTable1')

14 Now, Back to Statistics…
Statistics help to determine the SELECTIVITY of the data. High selectivity implies high uniqueness, or a low number of matching values. Low selectivity implies a low uniqueness, or a high percent of matches. 

15 Execution Plans Once the optimizer arrives at an execution plan, the actual plan is created and stored in a memory space known as the plan cache UNLESS: It is expensive for the Server to generate execution plans so SQL Server will keep and reuse plans wherever possible. While gen-ing plans, if CBO finds a matching plan in the cache, it will use that plan.

16 Julie’s Tool Time DBCC FREEPROCCACHE
Consider that this removes ALL ROWS from the PLAN CACHE on the entire INSTANCE you are connected to; it is not a command bound only by your own session or database you are connected to! What does this mean????? (Test Twice Always, Be kind to your co- workers if shared instance) INSTEAD…

17 Julie’s Tool Time - cont
-- Remove one plan from the cache Step 1-- Get the plan handle for a cached plan SELECT cp.plan_handle, st.[text] FROM sys.dm_exec_cached_plans AS cp CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st WHERE [text] LIKE N'%/* GetOnlineSearchResultsMonday %'; Step 2-- Remove the specific plan from the cache using the plan handle DBCC FREEPROCCACHE (0x F7BA926C40C );

18 More on Selectivity The estimated selectivity of a predicate is essential when the optimizer is evaluating possible plans. (What is a predicate??)

19 Selectivity Can be seen…
Affects things like join order (all other things equal, the most selective join operation is generally performed first) Whether SQL chooses to do a table scan or an index seek followed by a bookmark lookup

20 How To: Estimated Execution Plan
Several Methods to Launch it! Click on the “Display Estimated Execution Plan icon on the tool bar. Right-click the query window and select the same option from the menu. Click on the Query option in the menu bar and select the same choice. Simply hit CTRL-L on the keyboard. Pick your favorite method!

21 -VS- ACTUAL Execution Plan
An Actual Estimation Plan does not represent the calculations of the optimizer like ESTIMATED does. ACTUAL Execution Plan methods function as an “on” switch So, Execution plan will be created for all queries run from that query window until you (remember to) turn it off again.

22 How To: ACTUAL Execution Plan
Several Methods to Launch it! Click on the icon on the tool bar called “Include Actual Execution Plan Right-click within the query window and choose the “Include Actual Execution Plan menu item. Choose the same option in the Query menu choice. Type Control-M. Pick your favorite method!

23 SQL Performance Checklist
SORTs are generally expensive; ensure that they are NEEDED and sort the LEAST amount of rows (ie Order By) Table Scans are not necessarily a bad thing Avoid function in WHERE clause (ie UPPER, DATEDIFF, LEFT, SUBSTRING)

24 SQL Performance Checklist
If EXISTS stops SQL processing if 1 result is found, COUNT(*) keeps processing to end of set UNLESS comparing COUNT(*) > 0 COUNT(*) includes NULL-able rows, while COUNT(Column1) doesn't if Column1 value IS NULL

25 SQL Performance Checklist
NULLS do not equal anything, but… Oracle –vs- SQL Server, AI value –vs- Null Index Seek (PINPOINT) is more efficient than Index Scan (ie Where Name Like AB% or including the columns in the INDEX order, like scanning a RANGE)

26 A Final JULIE’S TOOL TIME:
SET STATISTICS IO ON/OFF --pay attention to Physical Reads SET STATISTICS TIME ON/OFF --difference between ELAPSED time and CPU; Consider Load on the system!

27


Download ppt "JULIE McLAIN-HARPER LINKEDIN: JM HARPER"

Similar presentations


Ads by Google