"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;
Showing posts with label SQL Performance Tuning. Show all posts
Showing posts with label SQL Performance Tuning. Show all posts

August 12, 2012

SQL Server - Index Tuning Basics

SQL Server Query Tuning Session. - One more session to add to SQL Tuning list.


Useful pointers from the session. Couple of pointers are covered in previous posts as well.

Optimizer's choice of Join
Nested Loop Join
  • Outer input is small
  • Inner input has an index on the join key
Merge Join
  • Medium to large inputs
  • Sorted inputs and equality operator required
Hash Join
  • Large inputs
  • Requires equality operator, inputs need not be sorted

MAXDOP Option - 1 (Ensure single processor for execution)
Density - How many distinct values available in a particular column
Multi Column Index
  • Index can be used to seek on second column if there is an equality operator on first column
Guildeline - Most Selective column should be first column where all other column predicates use the equality operator
Happy Learning!!!

March 15, 2010

How many times my query has been executed ?

Thanks to Balmukund for his help. I wanted to know how many times a query has been executed from its query plan. Here is quick steps for this.
Step 1 - Create necessary tables for demo
use tempdb
CREATE TABLE DBO.TESTTable
(
    Id INT Identity(10,10),
    Name VARCHAR(20)
)

INSERT INTO DBO.TESTTable(Name)
Values ('Testvalue')
GO 50
Step 2 - Run below query
SELECT * FROM TESTTable WHERE Id = 20

Step 3 - I would like to know the plan for this query - Below DMV query would help

SELECT *
FROM sys.dm_exec_query_stats
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
CROSS APPLY sys.dm_exec_query_plan(plan_handle)
WHERE TEXT LIKE '%TestTable%'

Step 4 - Fetch the Plan Handle from above step. Use the DMV sys.dm_exec_query_stats to find execution count of that plan based on plan handle
SELECT execution_count,* FROM sys.dm_exec_query_stats where plan_handle = 0x0600020078374D0040213585000000000000000000000000
 
This execution count would tell you how many times this plan has been executed. To check plan is cached  query cached_plans DMV can be used
SELECT * FROM sys.dm_exec_cached_plans where plan_handle = 0x0600020078374D0040213585000000000000000000000000
 
Hope it was useful. Have a Good Day.

January 10, 2010

SQL Perf Tuning - Performance Counters and Values

I have come across lot of blogs with queries to find Top IO, CPU queries.
  • Learn to differentiate IO Issue, CPU Issue, Network Issue
  • IO Issue - Excessive IO can be due to a SCAN on a big table. There are ways to convert SCAN to SEEK. In a OLTP system query are expected to be highly selective, fetch few records
  • CPU Issue - During processing we might have temp tables, Sort a Temp tables. Sort Operator is usually CPU intensive. Wait Stats sys.dm_os_wait_stats DMV provides more info. Please check Joe Sack wait stats link mentioned below
  • Disk Issue - PhysicalDisk/LogicalDisk counter in below bookmarked excel and values defined for it for interpreting Disk Issues
  • Network Latency - Network Interface counter in below excel provides values for counters and interpreting them
List of Counters and Optimal values for them


From Perfmon counters interpret from values of perfmon counters. Attached XL is very very good.
Without writing any query how do we find expensive queries
  • Activity monitor check recently expensive queries
  • Goto Particular Database that you want to check. List Reports for Reports->Standard Reports->Top Transactions by Age, Locks
  • Check Parallelism Enabled/Not Link
Saleem has written a good article on step-by-step Analysis for query troubleshooting. Link is


Happy Learning!!!

September 14, 2009

Learning's on perfomance troubleshooting

Enables DBCC Traceon (1222) to Capture Deadlocks.
Run below trace to capture deadlock process. Link here

I also learnt I need to do a good learning on locking concepts, I found below links useful
Range locks
Geek City: What do you intend with that lock?
SQL Server DBA Concurrency and Locking Interview Questions
Presentation Links: SQL Server Performance Tuning (Quest)


Happy Learning!!!

August 01, 2009

Using Hints and Plan Guides

In this post we would see examples for using hints and plan guides

DBCC FREEPROCCACHE - Removes all plans in cache

--STEP 1
DROP TABLE TestTable
DROP TABLE TestTable2

--STEP 2
CREATE Table TestTable (
a int NOT NULL PRIMARY KEY,
name varchar(50))

CREATE Table TestTable2 (
a int NOT NULL PRIMARY KEY,
name varchar(50))

--STEP 3
Declare @i int
set @i = 1
While 1 =1
begin
insert into TestTable(a, name)
select @i, convert(varchar(2),@i)+ 'name'
insert into TestTable2(a, name)
select @i+5, convert(varchar(2),@i)+ 'name'
set @i = @i+1
if @i > 100
break;
end

sp_help TestTable
--PK__TestTable__627A25C7 clustered, unique, primary key located on PRIMARY

--STEP 4
CREATE INDEX IX_Name ON TestTable(name)

--Index Hint
We can specify the query to use a specific index as mentioned below.

SELECT a,name FROM TestTable WITH (INDEX(IX_Name)) WHERE a = 10
SELECT a,name FROM TestTable WITH (INDEX(PK__TestTable__627A25C7)) WHERE a = 10

--JOINT HINT
If I want my query to use a specific join you can provide it like mentioned below.

SELECT TT1.a,TT.name FROM TestTable TT JOIN TestTable2 TT1
ON TT.a = TT1.a
Option(MERGE JOIN)

--STEP 5
I need my query to stick to a plan, you can create a plan guide for your query as provided in below example.

--CREATE A PLAN GUIDE For the Same
sp_create_plan_guide @name = N'PlanGuidetest',
@stmt = N'SELECT TT1.a,TT.name FROM TestTable TT JOIN TestTable2 TT1
ON TT.a = TT1.a',
@type = N'SQL',
@module_or_batch = NULL,
@params = NULL,
@hints = N'OPTION (Loop JOIN)'
GO

SELECT * FROM sys.plan_guides
GO

SELECT TT1.a,TT.name FROM TestTable TT JOIN TestTable2 TT1
ON TT.a = TT1.a

--Drop the plan guide.
EXEC sp_control_plan_guide N'DROP', N'PlanGuidetest'

References –
http://blogs.technet.com/andrew/archive/2008/11/14/sql-server-2008-plan-guides.aspx

Special thanks to balmukund for correcting me plan guide is case-sensitive, Even a little space adjusted in the query, it would not match with plan guide.