Your SlideShare is downloading. ×
0
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Sql query performance analysis
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Sql query performance analysis

1,867

Published on

Sql query performance analysis

Sql query performance analysis

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,867
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
72
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. SQL Query Performance Analysis
  • 2. INDEX• Query Optimizer• Addhoc queries• Execution Plan• Statistics Analysis
  • 3. Query OptimizerThe query optimizer in SQL Server is cost-based. It includes:1. Cost for using different resources (CPU and IO)2. Total execution timeIt 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.
  • 4. Addhoc queriesAny non-Parameterized quires are called addhoc queries. Forexample :SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 100In sql server if we execute a sql query it goes through two stepsjust like any other programming languages:• 1. Compilation• 2. Execution
  • 5. Properties of addhoc query• Case sensitive• Space sensitive• Parameter sensitiveSql severs treats two same sql query of different parametersas different sql statements. For example:• SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 1• SELECT MsgID, Severity FROM SqlMessage WHERE MsgID = 2
  • 6. Effect of faulty C# code• Sql server 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.
  • 7. 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.Stored procedure :• It is precompiled sql queries which follow a common execution plan.
  • 8. Execution Plan• What is an index in sql server? Index is a way to organize data in a table to make some operations like searching, sorting, grouping etc faster for particular key. We need indexing when cached query plan has:• WHERE , HAVING, ON clause (That is searching)• ORDER BY clause (That is sorting)• GROUP BY clause (This is grouping) etc.
  • 9. Table scan and Index scan:SELECT * FROM Student WHERE RollNo = 111Time complexity of table scan is : O(n)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
  • 10. Clustered index• When we create a clustered index on any table physical organization of table is changed in B- Tree.• Leaf node of clustered index always keep actual data.• If we create table with primary key, sql server automatically creates clustered index on that table .• If clustered index has more than one partition then each partition has a B-tree.
  • 11. Index seekTime complexity of index seek is: O(log n)
  • 12. Types of scanning• Table scan: It is very slow scanning process and it is used only if table has not any clustered index.• Index scan: It is also slow scanning process. 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.• If a table has only five records which scanning will you follow?
  • 13. Terms of execution plan• Predicate: It is condition in WHERE clause which is either non- key column or column which has not been covered.• Seek Predicate: It is condition in WHERE clause which is either key column or fully covered.• Object: It is name of source from where it getting the data. It can be heap, clustered index or non-clustered index.• Output list: It is name of the columns which are getting from the object.
  • 14. Non-clustered index• It is logical organization of data of table. A non-clustered index can be of two types.1. Based on heap2. Based on clustered index.• If table has clustered index then leaf node of non-clustered index keeps the key columns of clustered index.• In heap leaf node of non-clustered index keeps RID.
  • 15. Based on clustered Index
  • 16. Based on heap
  • 17. 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.
  • 18. Statistics Analysis• The query optimizer uses statistics to create query plans that improve query performance.• A correct or updated statistics will lead to high-quality query plan.• Auto create and updates applies strictly to single-column statistics.• 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.
  • 19. To improve cardinality• If possible, simplify expressions with constants in them.• If there is cross relation between column use computed column.• Rewriting the query to use a parameter instead of a local variable.• Avoid changing the parameter value within the stored procedure before using it in the query.
  • 20. Goal• Should we use sub query or inner join?• Should we use temp table or table variable?Other tools:• Sql query profiler• Database Tuning Advisor• Resource Governor
  • 21. THANK YOU

×