sqa server performance tuning

22
SQLSERVER PERFORMANCE TUNING Presenter: Trnh Hng Chương

Upload: duy-tan-geek

Post on 11-Jun-2015

74 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: SQA server performance tuning

SQLSERVER PERFORMANCE

TUNING

Presenter: Trịnh Hồng Chương

Page 2: SQA server performance tuning

AGENDA

� About my company – ANS-ASIA, and me

� About SQL Performance

� Indexing

� Rewrite query

� SQLServer tools to improve performance.

Page 3: SQA server performance tuning

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

Page 4: SQA server performance tuning

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, ….

Page 5: SQA server performance tuning

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.

Page 6: SQA server performance tuning

WHAT’S INDEXING

� Index is shortcuts to real data

� Data type structure: B-Tree

� Types of indexes: Clustered, Non-Clustered, XML

index, Fulltext index

Page 7: SQA server performance tuning

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

Page 8: SQA server performance tuning

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

Page 9: SQA server performance tuning

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)

Page 10: SQA server performance tuning

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

Page 11: SQA server performance tuning

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

Page 12: SQA server performance tuning

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

Page 13: SQA server performance tuning

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

Page 14: SQA server performance tuning

REWRITE QUERY

� Use parameterizied queries

� Query only you must

� About Performance, cursor less than base-query

Page 15: SQA server performance tuning

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" ...

Page 16: SQA server performance tuning

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" ...

Page 17: SQA server performance tuning

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’

Page 18: SQA server performance tuning

EXECUTION PLAN – QUERY 01

Page 19: SQA server performance tuning

EXECUTION PLAN – QUERY 02

Page 20: SQA server performance tuning

SQLServer tools to improve performance.

� SQL Profiler

Page 21: SQA server performance tuning

REFERENCE

� http://technet.microsoft.com

� http://www.sqlviet.com

Page 22: SQA server performance tuning