11
SQL Query Performance Analysis Level 1

Addhoc query

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Addhoc query

SQL Query Performance Analysis

Level 1

Page 2: Addhoc query

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

Page 3: Addhoc query

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

Page 4: Addhoc query

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.

Page 5: Addhoc query

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.

Page 6: Addhoc query

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.

Page 7: Addhoc query

Query Optimizer

Page 8: Addhoc query

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.

Page 9: Addhoc query

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.

Page 10: Addhoc query

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

Page 11: Addhoc query

THANK YOU