30.05.2013 Views

Tuning SQL for Optimal Performance - Sybase

Tuning SQL for Optimal Performance - Sybase

Tuning SQL for Optimal Performance - Sybase

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Agenda<br />

• What affects application<br />

per<strong>for</strong>mance<br />

• <strong>SQL</strong> Optimization and the<br />

Application Lifecycle<br />

• How to achieve and maintain<br />

maximum per<strong>for</strong>mance<br />

• Interview with Michael<br />

Brundige, Sr. <strong>Sybase</strong> DBA<br />

of Brinks


Gartner Study<br />

• According to industry experts,<br />

60% to 90% of database<br />

application per<strong>for</strong>mance<br />

problems are caused by <strong>SQL</strong><br />

<strong>SQL</strong><br />

Related<br />

60%<br />

“Poorly written OLTP application <strong>SQL</strong> continues to be the<br />

largest contributor to poor database per<strong>for</strong>mance.”<br />

Other<br />

40%<br />

– Gartner Group


A Corporate Per<strong>for</strong>mance Strategy<br />

• Hardware Upgrades 0 — 25%<br />

• Operating System Settings 0 — 50%<br />

• Database Server Parameter Settings 0 — 100%<br />

• Database Design 0 — 1000%<br />

• Indexes on Database Tables 0 — 1000%<br />

• <strong>SQL</strong> Statements 0 — 100,000%<br />

Based on LECCOTECH customer feedback


Sample of Customer Results<br />

Fremont Compensation – resource consumption<br />

Original statement: 2.5 hours <strong>SQL</strong> Expert optimized result: 1.5 minutes<br />

Original statement: 4 hours <strong>SQL</strong> Expert optimized result: 1.3 minutes<br />

Original statement: 8 hours <strong>SQL</strong> Expert optimized result: 2 hours<br />

Metasolv, Plano, TX – productivity<br />

Original statement: 15 minutes <strong>SQL</strong> Expert optimized result: 3 seconds<br />

Kentucky Community Technical Colleges – skill set<br />

Original statement: 1.2 hours <strong>SQL</strong> Expert optimized result: 15 seconds<br />

(complex statement with 8 table joins)


<strong>Sybase</strong> <strong>SQL</strong> Expert: A Per<strong>for</strong>mance Assurance Strategy<br />

Enhancements or<br />

New Requirements<br />

Maintain<br />

<strong>Optimal</strong><br />

Per<strong>for</strong>mance<br />

The Application Life-Cycle<br />

Development<br />

Production<br />

QA & Stress<br />

Best<br />

Per<strong>for</strong>ming<br />

Code<br />

Maximum<br />

Per<strong>for</strong>mance<br />

Be<strong>for</strong>e Rollout


3-Golden Rules <strong>for</strong> Writing <strong>SQL</strong><br />

• Make it work<br />

• Make it work right<br />

• Make it work fast


The Complex Nature of <strong>SQL</strong><br />

• Difficult to write <strong>SQL</strong> <strong>for</strong> per<strong>for</strong>mance<br />

• Many ways to write a <strong>SQL</strong><br />

• Small differences in coding <strong>SQL</strong> can have great per<strong>for</strong>mance<br />

implications<br />

• Need constant tuning due to data and system environment<br />

changes<br />

• Most important factor to system scalability


How to Tune <strong>SQL</strong><br />

• Understand how the database processes <strong>SQL</strong> statements<br />

• Know database structure<br />

• Know data in the database<br />

• Different <strong>SQL</strong> syntax can change per<strong>for</strong>mance


How Does ASE Process <strong>SQL</strong>?<br />

Data<br />

<strong>SQL</strong><br />

Server Receives <strong>SQL</strong><br />

Parse, Normalize &<br />

Pre-Process<br />

ASE Optimizer<br />

Determines the Query Plan<br />

Bind<br />

Execute


How the ASE Optimizer Works*<br />

<strong>SQL</strong><br />

Internally<br />

Rewrites &<br />

Generates<br />

Multiple<br />

Query plans<br />

Plan 1 cost=1000<br />

Plan 2 cost=3000<br />

Plan 3 cost=500<br />

Plan 1<br />

Plan 2<br />

Plan 3<br />

Cost Estimation


How the ASE Optimizer Works*<br />

<strong>SQL</strong><br />

Internally<br />

Rewrites &<br />

Generates<br />

Multiple<br />

Query plans<br />

Plan 1 cost=1000<br />

Plan 2 cost=3000<br />

Plan 3 cost=500<br />

Plan 1<br />

Plan 2<br />

Plan 3<br />

Can it try every possible<br />

way to rewrite your <strong>SQL</strong>?<br />

Cost Estimation


How the ASE Optimizer Works*<br />

<strong>SQL</strong><br />

Internally<br />

Rewrites &<br />

Generates<br />

Multiple<br />

Query plans<br />

Plan 1 cost=1000<br />

Plan 2 cost=3000<br />

Plan 3 cost=500<br />

Plan 1<br />

Plan 2<br />

Plan 3<br />

Can it try every possible<br />

way to rewrite your <strong>SQL</strong>?<br />

Cost Estimation<br />

How Accurate is the<br />

Cost Estimation<br />

Execution


The Perfect Optimizer?<br />

“For any real-life optimizer, no matter how sophisticated, there will<br />

always be a query, a database state and a system state where the<br />

optimizer makes the wrong decision.”<br />

Mihnea Andrei (<strong>Sybase</strong> France) &<br />

Patrick Valduriez (Laboratoire d’In<strong>for</strong>matique de Paris)<br />

User-Optimizer Communication, Abstract Plans Tech paper


2 Different <strong>SQL</strong>’s – Same Query Plan<br />

Elapsed Time: 0.080 s Elapsed Time: 0.080 s


2 Different <strong>SQL</strong>’s – 2 Different Plans<br />

Elapsed Time: 0.080 s Elapsed Time: 0.110 s


Find the Best Way to Process Your <strong>SQL</strong>


Find the Best Way to Process Your <strong>SQL</strong>


Manual Approach – <strong>SQL</strong> <strong>Tuning</strong><br />

• Obtain show_plan and analyze output. Look <strong>for</strong> problem<br />

operations such as full table scans<br />

i<strong>SQL</strong><br />

SET SHOWPLAN ON<br />

GO<br />

SELECT *<br />

FROM EMPLOYEE<br />

WHERE EM P_ID > 73712<br />

GO<br />

SET SHOWPLAN OFF<br />

GO<br />

QUERY PLAN FOR STATEMENT 1 (at line 1).<br />

STEP 1<br />

The type of query is SELECT.<br />

FROM TABLE<br />

EMPLOYEE<br />

Nested iteration.<br />

Table Scan.<br />

Forward scan.<br />

Positioning at start of table.<br />

Using I/O Size 2 Kbytes <strong>for</strong> data pages.<br />

With LRU Buffer Replacement Strategy <strong>for</strong> data pages.<br />

Total estimated I/O cost <strong>for</strong> statement 1 (at line 1): 20600.


Manual Approach – <strong>SQL</strong> <strong>Tuning</strong><br />

• Try different ways to write the <strong>SQL</strong> statement<br />

• Techniques to write <strong>SQL</strong>:<br />

– IN, EXISTS, or JOIN<br />

– Full table scan, or index scan<br />

– JOIN access path<br />

– NOT IN, or MINUS<br />

– Outer Join, or UNION ALL<br />

– IN or OR<br />

– Forces?<br />

– Temp tables?<br />

– ….


Article – Fifty Ways to Quote Your Query<br />

BY C. J. Date, July 1998<br />

Database Programming & Design<br />

1. SELECT DISTINCT S.SNAME<br />

FROM S, SP<br />

WHERE S.S# = SP.S#<br />

AND SP.P# = 'P2'<br />

“if there are so many ways to<br />

<strong>for</strong>mulate such a simple query,<br />

how many ways are there going<br />

to be <strong>for</strong> a complicated one?”<br />

2. SELECT DISTINCT S.SNAME<br />

FROM S<br />

WHERE S.S# IN<br />

( SELECT SP.S#<br />

FROM SP<br />

WHERE SP.P# = 'P2' )<br />

3. SELECT DISTINCT S.SNAME<br />

FROM S<br />

WHERE S.S# =ANY<br />

( SELECT SP.S#<br />

FROM SP<br />

WHERE SP.P# = 'P2' )<br />

4. SELECT DISTINCT S.SNAME<br />

FROM S<br />

WHERE S.S# MATCH<br />

( SELECT SP.S#<br />

FROM SP<br />

WHERE SP.P# = 'P2' )<br />

5. SELECT DISTINCT S.SNAME<br />

FROM S<br />

WHERE S.S# MATCH<br />

UNIQUE<br />

( SELECT SP.S#<br />

FROM SP<br />

WHERE SP.P# = 'P2' )<br />

6. SELECT DISTINCT S.SNAME<br />

FROM S<br />

WHERE S.S# MATCH<br />

PARTIAL<br />

( SELECT SP.S#<br />

FROM SP<br />

WHERE SP.P# = 'P2' )


Manual Approach – <strong>SQL</strong> <strong>Tuning</strong><br />

• Time the alternatives in I<strong>SQL</strong><br />

i<strong>SQL</strong><br />

SET STATISTICS IO ON<br />

GO<br />

SET STATISTICS TIME ON<br />

GO<br />

SELECT *<br />

FROM EMPLOYEE<br />

WHERE EMP_ID > 73712<br />

GO<br />

SET STATISTICS IO OFF<br />

GO<br />

SET STATISTICS TIME OFF<br />

GO<br />

Server Message: Number 3631, Severity 10<br />

Line 1:<br />

Total actual I/O cost <strong>for</strong> this command: 0.<br />

Total writes <strong>for</strong> this command: 0<br />

Server Message: Number 3631, Severity 10<br />

Line 1:<br />

Total actual I/O cost <strong>for</strong> this command: 0.<br />

Total writes <strong>for</strong> this command: 0<br />

Execution Time 0.<br />

<strong>SQL</strong> Server cpu time: 0 ms. <strong>SQL</strong> Server elapsed time: 3614 ms.<br />

Parse and Compile Time 0.<br />

<strong>SQL</strong> Server cpu time: 0 ms.<br />

Table: EMPLOYEE scan count 1, logical reads: (regular=1030 apf=0 total=1030),<br />

physical reads: (regular=0 apf=0 total=0), apf IOs used=0<br />

Server Message: Number 3631, Severity 10<br />

Line 1:<br />

Total actual I/O cost <strong>for</strong> this command: 2060.<br />

Total writes <strong>for</strong> this command: 0<br />

Execution Time 16.<br />

<strong>SQL</strong> Server cpu time: 1600 ms. <strong>SQL</strong> Server elapsed time: 7730 ms.<br />

(18371 rows affected)<br />

Parse and Compile Time 0.<br />

<strong>SQL</strong> Server cpu time: 0 ms.<br />

Execution Time 0.<br />

<strong>SQL</strong> Server cpu time: 0 ms. <strong>SQL</strong> Server elapsed time: 0 ms.<br />

Parse and Compile Time 0.<br />

<strong>SQL</strong> Server cpu time: 0 ms.


<strong>SQL</strong> Expert<br />

• Streamlines the <strong>SQL</strong> optimization process<br />

• Proactively identifies problematic <strong>SQL</strong> without running applications<br />

• AI-based <strong>SQL</strong> trans<strong>for</strong>mation generates every possible alternative<br />

and unique query plan<br />

• Benchmarks <strong>SQL</strong> to identify the most efficient alternative <strong>for</strong> a db<br />

environment<br />

• Provide the “OPTIMUM” <strong>SQL</strong> alternative


<strong>SQL</strong> Optimization Methodology<br />

Capture/Extract<br />

Analyze<br />

Maximize<br />

Per<strong>for</strong>mance<br />

Optimize


<strong>SQL</strong> Optimization Methodology<br />

Capture/Extract<br />

Analyze<br />

Maximize<br />

Per<strong>for</strong>mance<br />

• Dynamic <strong>SQL</strong>:<br />

Captures <strong>SQL</strong> through<br />

the <strong>Sybase</strong> Monitor<br />

Server<br />

— <strong>SQL</strong> Monitor<br />

Optimize<br />

• Embedded <strong>SQL</strong>:<br />

Extracts <strong>SQL</strong> from ASE<br />

database objects (sp’s,<br />

views, etc), files,<br />

source code<br />

(PowerBuilder, etc)<br />

— <strong>SQL</strong> Scanner


<strong>SQL</strong> Monitor – Capture currently running <strong>SQL</strong>


<strong>SQL</strong> Scanner – Extract embedded <strong>SQL</strong> from db objects & source code


<strong>SQL</strong> Optimization Methodology<br />

Capture/Extract<br />

Analyze<br />

• Analyze in batch the query plan <strong>for</strong> multiple <strong>SQL</strong><br />

• Identify potentially problematic operations: Full<br />

tables scans-large tables, many table scans,<br />

worktables,…<br />

—<strong>SQL</strong> Scanner<br />

Maximize<br />

Per<strong>for</strong>mance<br />

Optimize


<strong>SQL</strong> Scanner – Analyze and identify problematic query plans


Problematic <strong>SQL</strong> identified, without running it!


<strong>SQL</strong> Optimization Methodology<br />

• AI based recursive <strong>SQL</strong><br />

trans<strong>for</strong>mation<br />

• Provides unique query<br />

plans<br />

• Guarantees semantically<br />

equivalence<br />

• •Capture/Extract Rewrites SELECT,<br />

SELECT…INTO,<br />

INSERT, DELETE and<br />

UPDATE<br />

• Benchmarks test <strong>SQL</strong><br />

—Syntactical <strong>SQL</strong> Optimizer<br />

Analyze<br />

Maximize<br />

Per<strong>for</strong>mance<br />

Optimize


What is Recursive <strong>SQL</strong> Trans<strong>for</strong>mation<br />

Using 2 trans<strong>for</strong>mation rules<br />

SELECT * FROM A<br />

WHERE EXISTS (SELECT ‘x’ FROM B<br />

WHERE EXISTS (SELECT ‘x’ FROM C<br />

WHERE B.C2=C.C2)<br />

WHERE A.C1=B.C1)<br />

IN to<br />

EXISTS<br />

SELECT * FROM A<br />

WHERE A.C1 IN (SELECT B.C1 FROM B<br />

WHERE EXISTS (SELECT ‘x’ FROM C<br />

WHERE B.C2=C.C2 ))<br />

EXISTS<br />

to IN<br />

SELECT * FROM A<br />

WHERE A.C1 IN (SELECT B.C1 FROM B<br />

WHERE B.C2 IN<br />

(SELECT C.C2 FROM C))<br />

EXISTS<br />

to IN<br />

IN to<br />

EXISTS<br />

SELECT * FROM A<br />

WHERE A.C1 IN (SELECT B.C1 FROM B<br />

WHERE EXISTS (SELECT ‘x’ FROM C<br />

WHERE B.C2=C.C2 ))<br />

SELECT * FROM A<br />

WHERE EXISTS (SELECT ‘x’ FROM B<br />

WHERE B.C2 IN (SELECT C.C2 FROM C)<br />

AND A.C1=B.C1)<br />

SELECT * FROM A<br />

WHERE A.C1 IN (SELECT B.C1 FROM B<br />

WHERE B.C2 IN (SELECT C.C2 FROM C))<br />

SELECT * FROM A<br />

WHERE EXISTS (SELECT ‘x’ FROM B<br />

WHERE B.C2 IN (SELECT C.C2 FROM C)<br />

AND A.C1=B.C1)<br />

SELECT * FROM A<br />

WHERE A.C1 IN (SELECT B.C1 FROM B<br />

WHERE EXISTS (SELECT ‘x’ FROM C<br />

WHERE B.C2=C.C2))<br />

SELECT * FROM A<br />

WHERE EXISTS (SELECT ‘x’ FROM B<br />

WHERE EXISTS (SELECT ‘x’ FROM C<br />

WHERE B.C2=C.C2)<br />

AND A.C1=B.C1)


<strong>SQL</strong> Optimization – In 2 minutes, <strong>SQL</strong> Expert generated 279 alternatives


Benchmark testing the <strong>SQL</strong> alternatives against the database –<br />

Locate the most–efficient <strong>SQL</strong> <strong>for</strong> the ASE environment


Original <strong>SQL</strong><br />

Elapsed Time: 0.763 s<br />

Fastest <strong>SQL</strong> Alternative<br />

Elapsed time: 0.060 s


Polling Question<br />

Yes or No<br />

Have you used ASE’s<br />

Abstract Plans?


Abstract Plan<br />

<strong>SQL</strong><br />

Optimizer<br />

ASE<br />

Based on the<br />

database statistics<br />

to generate a query<br />

plan<br />

Based on the saved<br />

abstract plan to<br />

generate a query<br />

plan<br />

Execution


<strong>Sybase</strong> <strong>SQL</strong> Expert Facts<br />

• <strong>Sybase</strong> <strong>SQL</strong> Expert (SSE) is sold as an option to ASE<br />

• Windows-based product, Client-based product, no server-side<br />

installation<br />

• Databases supported:<br />

– From version 11.0 to ASE 12.5.0.3<br />

– To capture <strong>SQL</strong> from the <strong>Sybase</strong> Monitor Server, ASE 11.5 or above required<br />

– To use Abstract Plans, ASE 12.0 or above required<br />

• Open Client/CT-Lib version 11 or above<br />

• <strong>Sybase</strong> Monitor Server is only needed to capture currently running<br />

<strong>SQL</strong>; you can use <strong>SQL</strong> Expert without the <strong>Sybase</strong> Monitor Server<br />

• Non-intrusive<br />

– Scanning and optimization process occurs in the PC; no over-head to the<br />

database


Key <strong>SQL</strong> Expert Benefits<br />

• Automates current manual tuning ef<strong>for</strong>ts<br />

• Solves the problem consuming 60–90% of IT resources<br />

• Delivers immediate and quantifiable application<br />

per<strong>for</strong>mance gains<br />

• Heightens staff expertise<br />

• Increases staff productivity<br />

• Establishes benchmarks and per<strong>for</strong>mance standards<br />

Use <strong>Sybase</strong> <strong>SQL</strong> Expert to ensure only<br />

the best per<strong>for</strong>ming statements are used.


Interview with Michael<br />

Brundige, Sr. <strong>Sybase</strong><br />

DBA of Brinks

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!