sqa server performance tuning
TRANSCRIPT
SQLSERVER PERFORMANCE
TUNING
Presenter: Trịnh Hồng Chương
AGENDA
� About my company – ANS-ASIA, and me
� About SQL Performance
� Indexing
� Rewrite query
� SQLServer tools to improve performance.
ABOUT
� 100% subsidiary of ANS Japan
� Address : 10F CMC Tower, Duy Tan Street
� Foudation : November 2012
� Employees : 30
� Business function :Business function :
� Software development
� Enterprise system (100% Japanese customer up to now)
(Sales management system, Enterprise system for transportation
industry, Tuition management system for university...)
� IT consulting
ABOUT ME
� Name: Trinh Hong Chuong
� Skill: 6 years experience in software development
(.Net (VB.Net, C#), T-SQL, PL-SQL, VBA)
� Beginner in PHP, PostgreSQL
� Interesting in: Reading book, listening music,
walking alone, travelling, ….walking alone, travelling, ….
SQL PERFORMANCE
� Assess the problem and establish numeric values that categorize acceptable behavior.
� Measure the performance of the system before modification.
� Identify the part of the system that is critical for improving the performance. This is called the bottleneck.improving the performance. This is called the bottleneck.
� Modify that part of the system to remove the bottleneck.
� Measure the performance of the system after modification.
� If the modification makes the performance better, adopt it. If the modification makes the performance worse, put it back the way it was.
WHAT’S INDEXING
� Index is shortcuts to real data
� Data type structure: B-Tree
� Types of indexes: Clustered, Non-Clustered, XML
index, Fulltext index
WHY’S INDEXING
� An index is used to speed up searching in the
database.
� Indexes can be helpful for a variety of queries
that contain SELECT, UPDATE, DELETE, or
MERGE statements.
� Less items in primary key� Less items in primary key
CLUSTERED INDEX
� Clustered indexes sort and store the data rows in
the table or view based on their key values.
root
Id(from 1 to 4) Id(from 5 to 7)Id(from 1 to 4) Id(from 5 to 7)
Id 1
Name Bill
Dept Dev
Id 2
Name Jobs
Dept HR
Id 7
Name Gate
Dept R&D
NON-CLUSTERED INDEX
� A nonclustered index contains the nonclustered
index key values and each key value entry has a
pointer to the data row that contains the key
value.
root
Name(from A to F)
Name Bill
Id 1
Name Gate
Id 7
Name Jobs
Id 2
Name(from G to M) Name(from N to Z)
IMPROVE INDEX
� Create Highly-Selective Indexes
� Indexing on columns used in the WHERE clause of
your critical queries frequently improves
performance.
� Selectivity is the ratio of qualifying rows to total
rows. If the ratio is low, the index is highly selective.
� Create Multiple-Column Indexes� Create Multiple-Column Indexes
REWRITE QUERY
� Use a search argument (SARG)
� SARG operators include =, >, <, >=, <=, IN,
BETWEEN, and sometimes LIKE (in cases of prefix
matching, such as LIKE ‘Bill%')
� Non-SARG operators include NOT, <>, NOT EXISTS,
NOT IN, NOT LIKE, and intrinsic functions
REWRITE QUERY
�Rewrite sub-query into JOIN
Bad sample Good sample
SELECT "Order ID" SELECT DISTINCT O."Order ID"
FROM Orders O
WHERE EXISTS (SELECT "Order ID"
FROM "Order Details"
OD
WHERE O."Order ID" =
OD."Order ID"
AND Discount >= 0.25)
FROM Orders O
INNER JOIN "Order Details" OD
ON
O."Order ID" = OD."Order ID"
WHERE Discount >= 0.25
REWRITE QUERY
� Don’t use intrinsic functions, type conversion on index column
Bad sample Good sample
DECLARE @limitId = 10
SELECT Name FROM
Employees
DECLARE @limitId = 10
SELECT Name FROM
EmployeesEmployees
WHERE Id - 1 = @limitId
Employees
WHERE Id = @limitId + 1
REWRITE QUERY
� Use parameterizied queries
� Query only you must
� About Performance, cursor less than base-query
REWRITE QUERY
� Index the ORDER-BY / GROUP-BYCREATE INDEX Emp_Name ON Employees ("Last Name" ASC, "First Name" ASC)
Can help optimize Will not help optimize
... ORDER BY / GROUP BY "Last
Name" ...
... ORDER BY / GROUP BY
"First Name" ...Name" ...
... ORDER BY / GROUP BY "Last
Name", "First Name" ...
"First Name" ...
... ORDER BY / GROUP BY
"First Name", "Last Name" ...
REWRITE QUERY
� Index the DISTINCTCREATE INDEX Emp_Name ON Employees ("Last Name" ASC, "First Name" ASC)
Can help optimize Will not help optimize
... DISTINCT "Last Name", "First
Name" ...
... DISTINCT "First Name" ...
... DISTINCT "Last Name" ...Name" ...
... DISTINCT "First Name", "Last
Name" ...
... DISTINCT "Last Name" ...
SQLServer tools to improve performance.
� Execution plan
CREATE TABLE Employees
(
Id BIGINT NOT NULL,
Name VARCHAR(20) NOT NULL,
Dept VARCHAR(10),
CONSTRAINT [PK_Employee] PRIMARY KEY
CLUSTERED
(Id ASC)
)
CREATE TABLE Employees_Mid
(
Id BIGINT NOT NULL,
Name VARCHAR(20) NOT NULL,
Dept VARCHAR(10),
CONSTRAINT [PK_Employee_Mid] PRIMARY KEY
CLUSTERED
(Id ASC)
)
Query 01
INSERT INTO Employees(Id, Name, Dept)
SELECT Id, Name, Dept FROM Employees_Mid
WHERE Employees_Mid.Id = 1000
Query 02
INSERT INTO Employees(Id, Name, Dept)
SELECT Id, Name, Dept FROM Employees_Mid
WHERE Employees_Mid.Name = ‘A00001’
EXECUTION PLAN – QUERY 01
EXECUTION PLAN – QUERY 02
SQLServer tools to improve performance.
� SQL Profiler
REFERENCE
� http://technet.microsoft.com
� http://www.sqlviet.com