sql server query parameterization

29
Query Tuning SQL Server Query Parameterization Ritesh Kumar Skype: mfs_ritesh Blog: http://exacthelp.blogspot.com/

Upload: mindfire-solutions

Post on 24-May-2015

553 views

Category:

Technology


4 download

DESCRIPTION

This session talks all about SQL Server Query Parameterization. Covers various topics like Query Tuning, Adhoc Query, Execution Plan, Query Optimizer, Parameter Sniffing, Indexes etc. etc.

TRANSCRIPT

Page 1: Sql Server Query Parameterization

Query Tuning

SQL Server Query Parameterization

Ritesh KumarSkype: mfs_riteshBlog: http://exacthelp.blogspot.com/

Page 2: Sql Server Query Parameterization

INDEX

Adhoc Query

Predicates Order

Execution Plan

Query Optimizer

Parameter Sniffing

Indexes

Statistics

Database Engine Tuning Advisor

Page 3: Sql Server Query Parameterization

Adhoc Query

q Any non-parameterized queries are called addhoc queries. For example :

q SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 100

q In sql server if we execute a sql query it goes through two steps just like any other programming languages:

q Compilation

q Execution

Page 4: Sql Server Query Parameterization

Properties Of Addhoc Queries

q Case sensitive

q Space sensitive

q Parameter sensitive 

q Sql server treats two same sql queries of different parameters as a two different sql statements. For example:

q SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 1q SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 2

Page 5: Sql Server Query Parameterization

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 applications since this faulty code will force to delete cached query plans of other sql statements.

Page 6: Sql Server Query Parameterization

Predicates Order

q Does order of predicates matters in WHERE clause?

q WHERE vcLanguage = 'English' AND ntAge = 12

q WHERE ntAge = 12 AND vcLanguage = 'English'

Page 7: Sql Server Query Parameterization

Execution Plan

Page 8: Sql Server Query Parameterization

Query Optimizer

Page 9: Sql Server Query Parameterization

Query Optimizer

The query optimizer in SQL Server is cost-based. It includes:

q Cost for using different resources (CPU and IO)

q 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 10: Sql Server Query Parameterization

Parameter Sniffing

Sql server generates execution paln according to the first parameter

This execution plan may bad for other parameters.

 

Solution:

Create multiples stored procedures.

Use optimizer for query hints.

Page 11: Sql Server Query Parameterization

What Is An Index ?

q Index is a way to organize data to make searching, sorting and grouping faster.

q we need indexing when :

q WHERE, ON, HAVING clause (Searching)

q ORDER BY clause (Sorting)

q GROUP BY clause (Grouping) etc.

Page 12: Sql Server Query Parameterization

Table Scan

RollNo Name Country Age101 Greg UK 23102 Sachin India 21103 Akaram Pakistan 22107 Miyabi China 18108 Marry Russia 27109 Scott USA 31110 Benazir Banglades 17111 Miyabi Japan 24112 Rahul India 27113 Nicolus France 19

SELECT * FROM Student WHERE RollNo = 111

Time complexity of table scan is : O(n)

Page 13: Sql Server Query Parameterization

Types Of Index

q Table without any index is called Heap

q There are two type of index:

q Clustered index

q Non-Clustered index

Page 14: Sql Server Query Parameterization

Clustered Index

When we create a clustered index on any table physical organization of table is changed.

Now data of table is stored as a balanced tree(B tree).

CREATE UNIQUE [CLUSTERED] INDEX <Name>

ON <ObjectName>(

<ColumnName>  [ASC | DESC ] [ ,...n ]

)

Page 15: Sql Server Query Parameterization
Page 16: Sql Server Query Parameterization

Types Of Scanning

Table scan: It is very slow can and it is used only if table has not any clustered index.

Index scan: It is also slow scan. It is used when table has clustered index and either in WHERE clause non-key columns are present or query has not been covered (will discuss later) or both.

Index Seek: It is very fast. Our goal is to achieve this.

Page 17: Sql Server Query Parameterization

Clustered Index

If we create table with primary key, sql server automatically creates clustered index on that table

A table can have only one clustered index .

Physical order of rows of table is same as logical order of key columns of clustered index.

Page 18: Sql Server Query Parameterization

Terms Of Execution Plan

Predicate: It is condition in WHERE clause which is either non-key column or column which has not been covered.

Object: It is name of source from where it getting the data. It can be name of table, Clustered index or non-clustered index

Output list: It is name of the columns which is getting from object.

Seek Predicate: It is condition in WHERE clause which is either key column or fully covered.

Page 19: Sql Server Query Parameterization

Non-Clustered Index

It is logical organization of data of table. A non-clustered index can be of two types.

q Heap

q Based on clustered index.

If table has clustered index then leaf node of non-clustered index keeps the key columns of clustered index.

If the table has not any clustered index then leaf node of non-clustered index keeps RID which unique of each row of table.

Page 20: Sql Server Query Parameterization

Based On Clustered Index

Page 21: Sql Server Query Parameterization

Based On Heap

Page 22: Sql Server Query Parameterization

Covering Of Queries

We can specify maximum 16 column names.

Sum of size of the columns cannot be more than 900 bytes.

All columns must belong to same table.

Data type of columns cannot be ntext, text, varchar (max), nvarchar (max), varbinary (max), xml, or image

It cannot be non-deterministic computed column.

Page 23: Sql Server Query Parameterization

Statistics Analysis

The query optimizer uses statistics to create query plans that improve query performance

A correct statistics will lead to high-quality query plan.

The query optimizer determines when statistics might be out-of-date by counting the number of data modifications since the last statistics update and comparing the number of modifications to a threshold.

Page 24: Sql Server Query Parameterization

Auto Create Statistics

Default setting of auto create statistics is ON.

It creates when: Clustered and non clustered Index is created

Select query is executed.

Auto create and updates applies strictly to single-column statistics.

Page 25: Sql Server Query Parameterization

Why Query 2 Is Performing Better

q If we perform following operations on field of any table in query predicate:

q Using any system function or user defined function

q Scalar operation like addition, multiplication etc.

q Type casting

q In this situation sql server query optimizer is not able to estimate correct cardinality using statistics.

Page 26: Sql Server Query Parameterization

To Improve Cardinality

If possible, simplify expressions with constants in them.

If possible, don't perform any operation on the any field of a table in WHERE Clause, ON Clause, HAVING Clause

Don't use local variables in WHERE Clause, ON Clause, HAVING Clause. 

If there is any cross relationship among fields or there is a complex expression in a field in a query predicates, it is better to create a computed column and then create a non-clustered index on it.

Page 27: Sql Server Query Parameterization

To Improve Cardinality

If possible, don't update the value of parameters of a function or stored procedure before using in sql statement

Use OPTIMIZE FOR clause when you want to optimize a sql query on the basis of specific parameter value.

If you want to update the value parameter of a stored procedure or a function create a similar procedure or function and execute it form base procedure or function by passing the updated value as a parameter. 

Create user defined multi column statistics if query predicates have more than one fields of a table.

Page 28: Sql Server Query Parameterization

SQL Server Tools

Sql query profiler

Database Tuning Advisor Client Statistics

Resource Governor Data Collections

Page 29: Sql Server Query Parameterization

THANK YOU