Upload
riteshkiit
View
1.801
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
SQL Query Performance Analysis
Level 1
Addhoc QueriesAny non-Parameterized queries are called addhoc queries. For example :
SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 100
In sql server if we execute a sql query it goes through two steps just like any other programming languages:
• 1. Compilation • 2. Execution
Properties of addhoc query • Case sensitive• Space sensitive• Parameter sensitive
Sql severs treats two same sql query but of different parameters as different sql statements. For example:
• SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 1• SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 2
Effect of faulty C# code• Sql server has took extra n * (Compilation time) ms to display
records
• Extra time to insert records in cached plans.
• Sql server has to frequently fire a job to delete the cached plan since it will reach the max limit very soon.
• It will not only decrease the performance of this sql query but all sql queries of other application since this faulty code will force to delete cached query plans of other sql statement.
Prepared queriesExample:
(@Msgid int)SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = @Msgid
• It is not case, space and parameter sensitive and it is our goal.
Query Processing
• Non-Procedural: . SELECT, DELETE, UPDATE statements etc.
• a. Analyze the query.• b. Get execution plan.• b. Process the query.
• Procedural: Stored procedure, sql functions etc.
Query Optimizer
Query Optimizer
The query optimizer in SQL Server is cost-based. It includes:
1. Cost for using different resources (CPU and IO)2. Total execution time It determines the cost by using: • Cardinality: The total number of rows processed at each level of a
query plan with the help of histograms , predicates and constraint
• Cost model of the algorithm: To perform various operations like sorting, searching, comparisons etc.
Execution Plan and Caching
• Query Plan• Execution Context
• Database Engine uses a cost-based approach to determine which execution plans to remove from the procedure cache.
Assignments
• 1. Give any example in which join performs better than sub query.
• 2. Give me any example in which order matters in WHERE clause.
• 3. Give me any three examples which query returns same output but have different execution plan
http://msdn.microsoft.com/en-us/library/ms175913(v=sql.105).aspx
THANK YOU