12.07.2015 Views

Tuning SQL for Optimal Performance - Sybase

Tuning SQL for Optimal Performance - Sybase

Tuning SQL for Optimal Performance - Sybase

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

Agenda• What affects applicationper<strong>for</strong>mance• <strong>SQL</strong> Optimization and theApplication Lifecycle• How to achieve and maintainmaximum per<strong>for</strong>mance• Interview with MichaelBrundige, Sr. <strong>Sybase</strong> DBAof Brinks


Gartner Study• According to industry experts,60% to 90% of databaseapplication per<strong>for</strong>manceproblems are caused by <strong>SQL</strong><strong>SQL</strong>Related60%Other40%“Poorly written OLTP application <strong>SQL</strong> continues to be thelargest contributor to poor database per<strong>for</strong>mance.”– Gartner Group


A Corporate Per<strong>for</strong>mance Strategy• Hardware Upgrades 0 — 25%• Operating System Settings 0 — 50%• Database Server Parameter Settings 0 — 100%• Database Design 0 — 1000%• Indexes on Database Tables 0 — 1000%• <strong>SQL</strong> Statements 0 — 100,000%Based on LECCOTECH customer feedback


Sample of Customer ResultsFremont Compensation – resource consumptionOriginal statement: 2.5 hours <strong>SQL</strong> Expert optimized result: • 1.5 minutesOriginal statement: 4 hours <strong>SQL</strong> Expert optimized result: • 1.3 minutesOriginal statement: 8 hours <strong>SQL</strong> Expert optimized result: • 2 hoursMetasolv, Plano, TX – productivityOriginal statement: 15 minutes<strong>SQL</strong> Expert optimized result: • 3 secondsKentucky Community Technical Colleges – skill setOriginal statement: 1.2 hours <strong>SQL</strong> Expert optimized result: • 15 seconds(complex statement with 8 table joins)


<strong>Sybase</strong> <strong>SQL</strong> Expert: A Per<strong>for</strong>mance Assurance StrategyThe Application Life-CycleEnhancements orNew RequirementsDevelopmentBestPer<strong>for</strong>mingCodeMaintain<strong>Optimal</strong>Per<strong>for</strong>manceProductionQA & StressMaximumPer<strong>for</strong>manceBe<strong>for</strong>e Rollout


3-Golden Rules <strong>for</strong> Writing <strong>SQL</strong>• Make it work• Make it work right• Make it work fast


The Complex Nature of <strong>SQL</strong>• Difficult to write <strong>SQL</strong> <strong>for</strong> per<strong>for</strong>mance• Many ways to write a <strong>SQL</strong>• Small differences in coding <strong>SQL</strong> can have great per<strong>for</strong>manceimplications• Need constant tuning due to data and system environmentchanges• Most important factor to system scalability


How to Tune <strong>SQL</strong>• Understand how the database processes <strong>SQL</strong> statements• Know database structure• Know data in the database• Different <strong>SQL</strong> syntax can change per<strong>for</strong>mance


How Does ASE Process <strong>SQL</strong>?Server Receives <strong>SQL</strong>Data<strong>SQL</strong>Parse, Normalize &Pre-ProcessASE OptimizerDetermines the Query PlanBindExecute


How the ASE Optimizer Works*<strong>SQL</strong>InternallyRewrites &GeneratesMultipleQuery plansPlan 1Plan 2Plan 3Cost EstimationPlan 1 cost=1000Plan 2 cost=3000Plan 3 cost=500


How the ASE Optimizer Works*<strong>SQL</strong>InternallyRewrites &GeneratesMultipleQuery plansPlan 1Plan 2Plan 3Can it try every possibleway to rewrite your <strong>SQL</strong>?Cost EstimationPlan 1 cost=1000Plan 2 cost=3000Plan 3 cost=500


How the ASE Optimizer Works*<strong>SQL</strong>InternallyRewrites &GeneratesMultipleQuery plansPlan 1 cost=1000Plan 1Plan 2Plan 3Can it try every possibleway to rewrite your <strong>SQL</strong>?Cost EstimationHow Accurate is theCost EstimationPlan 2 cost=3000Plan 3 cost=500Execution


The Perfect Optimizer?“For any real-life optimizer, no matter how sophisticated, there willalways be a query, a database state and a system state where theoptimizer makes the wrong decision.”Mihnea Andrei (<strong>Sybase</strong> France) &Patrick Valduriez (Laboratoire d’In<strong>for</strong>matique de Paris)User-Optimizer Communication, Abstract Plans Tech paper


2 Different <strong>SQL</strong>’s – Same Query PlanElapsed Time: 0.080 sElapsed Time: 0.080 s


2 Different <strong>SQL</strong>’s – 2 Different PlansElapsed Time: 0.080 sElapsed 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>• Obtain show_plan and analyze output. Look <strong>for</strong> problemoperations such as full table scansi<strong>SQL</strong>SET SHOWPLAN ONGOSELECT *FROM EMPLOYEEWHERE EM P_ID > 73712GOSET SHOWPLAN OFFGOQUERY PLAN FOR STATEMENT 1 (at line 1).STEP 1The type of query is SELECT.FROM TABLEEMPLOYEENested iteration.Table Scan.Forward scan.Positioning at start of table.Using I/O Size 2 Kbytes <strong>for</strong> data pages.With LRU Buffer Replacement Strategy <strong>for</strong> data pages.Total estimated I/O cost <strong>for</strong> statement 1 (at line 1): 20600.


Manual Approach – <strong>SQL</strong> <strong>Tuning</strong>• Try different ways to write the <strong>SQL</strong> statement• Techniques to write <strong>SQL</strong>:– IN, EXISTS, or JOIN– Full table scan, or index scan– JOIN access path– NOT IN, or MINUS– Outer Join, or UNION ALL– IN or OR– Forces?– Temp tables?– ….


Article – Fifty Ways to Quote Your QueryBY C. J. Date, July 1998Database Programming & Design1. SELECT DISTINCT S.SNAMEFROM S, SPWHERE S.S# = SP.S#AND SP.P# = 'P2'“if there are so many ways to<strong>for</strong>mulate such a simple query,how many ways are there goingto be <strong>for</strong> a complicated one?”2. SELECT DISTINCT S.SNAMEFROM SWHERE S.S# IN( SELECT SP.S#FROM SPWHERE SP.P# = 'P2' )3. SELECT DISTINCT S.SNAMEFROM SWHERE S.S# =ANY( SELECT SP.S#FROM SPWHERE SP.P# = 'P2' )4. SELECT DISTINCT S.SNAMEFROM SWHERE S.S# MATCH( SELECT SP.S#FROM SPWHERE SP.P# = 'P2' )5. SELECT DISTINCT S.SNAMEFROM SWHERE S.S# MATCHUNIQUE( SELECT SP.S#FROM SPWHERE SP.P# = 'P2' )6. SELECT DISTINCT S.SNAMEFROM SWHERE S.S# MATCHPARTIAL( SELECT SP.S#FROM SPWHERE SP.P# = 'P2' )


Manual Approach – <strong>SQL</strong> <strong>Tuning</strong>• Time the alternatives in I<strong>SQL</strong>i<strong>SQL</strong>SET STATISTICS IO ONGOSET STATISTICS TIME ONGOSELECT *FROM EMPLOYEEWHERE EMP_ID > 73712GOSET STATISTICS IO OFFGOSET STATISTICS TIME OFFGOServer Message: Number 3631, Severity 10Line 1:Total actual I/O cost <strong>for</strong> this command: 0.Total writes <strong>for</strong> this command: 0Server Message: Number 3631, Severity 10Line 1:Total actual I/O cost <strong>for</strong> this command: 0.Total writes <strong>for</strong> this command: 0Execution Time 0.<strong>SQL</strong> Server cpu time: 0 ms. <strong>SQL</strong> Server elapsed time: 3614 ms.Parse and Compile Time 0.<strong>SQL</strong> Server cpu time: 0 ms.Table: EMPLOYEE scan count 1, logical reads: (regular=1030 apf=0 total=1030),physical reads: (regular=0 apf=0 total=0), apf IOs used=0Server Message: Number 3631, Severity 10Line 1:Total actual I/O cost <strong>for</strong> this command: 2060.Total writes <strong>for</strong> this command: 0Execution Time 16.<strong>SQL</strong> Server cpu time: 1600 ms. <strong>SQL</strong> Server elapsed time: 7730 ms.(18371 rows affected)Parse and Compile Time 0.<strong>SQL</strong> Server cpu time: 0 ms.Execution Time 0.<strong>SQL</strong> Server cpu time: 0 ms. <strong>SQL</strong> Server elapsed time: 0 ms.Parse and Compile Time 0.<strong>SQL</strong> Server cpu time: 0 ms.


<strong>SQL</strong> Expert• Streamlines the <strong>SQL</strong> optimization process• Proactively identifies problematic <strong>SQL</strong> without running applications• AI-based <strong>SQL</strong> trans<strong>for</strong>mation generates every possible alternativeand unique query plan• Benchmarks <strong>SQL</strong> to identify the most efficient alternative <strong>for</strong> a dbenvironment• Provide the “OPTIMUM” <strong>SQL</strong> alternative


<strong>SQL</strong> Optimization MethodologyAnalyzeCapture/ExtractOptimizeMaximizePer<strong>for</strong>mance


<strong>SQL</strong> Optimization MethodologyAnalyze• Dynamic <strong>SQL</strong>:Captures <strong>SQL</strong> throughthe <strong>Sybase</strong> MonitorServer— <strong>SQL</strong> MonitorCapture/ExtractMaximizePer<strong>for</strong>manceOptimize• Embedded <strong>SQL</strong>:Extracts <strong>SQL</strong> from ASEdatabase objects (sp’s,views, etc), files,source code(PowerBuilder, etc)— <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 MethodologyAnalyzeCapture/ExtractOptimize• Analyze in batch the query plan <strong>for</strong> multiple <strong>SQL</strong>• Identify potentially problematic operations: Fulltables scans-large tables, many table scans,worktables,…—<strong>SQL</strong> ScannerMaximizePer<strong>for</strong>mance


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


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


<strong>SQL</strong> Optimization Methodology• AI based recursive <strong>SQL</strong>trans<strong>for</strong>mation• Provides unique queryplans• Guarantees semanticallyequivalence• Capture/ExtractRewrites SELECT,SELECT…INTO,INSERT, DELETE andUPDATE• Benchmarks test <strong>SQL</strong>—Syntactical <strong>SQL</strong> OptimizerAnalyzeMaximizePer<strong>for</strong>manceOptimize


What is Recursive <strong>SQL</strong> Trans<strong>for</strong>mationUsing 2 trans<strong>for</strong>mation rulesSELECT * FROM AWHERE EXISTS (SELECT ‘x’ FROM BWHERE EXISTS (SELECT ‘x’ FROM CWHERE B.C2=C.C2)WHERE A.C1=B.C1)IN toEXISTSSELECT * FROM AWHERE A.C1 IN (SELECT B.C1 FROM BWHERE EXISTS (SELECT ‘x’ FROM CWHERE B.C2=C.C2 ))EXISTSto INSELECT * FROM AWHERE A.C1 IN (SELECT B.C1 FROM BWHERE B.C2 IN(SELECT C.C2 FROM C))EXISTSto ININ toEXISTSSELECT * FROM AWHERE A.C1 IN (SELECT B.C1 FROM BWHERE EXISTS (SELECT ‘x’ FROM CWHERE B.C2=C.C2 ))SELECT * FROM AWHERE EXISTS (SELECT ‘x’ FROM BWHERE B.C2 IN (SELECT C.C2 FROM C)AND A.C1=B.C1)SELECT * FROM AWHERE A.C1 IN (SELECT B.C1 FROM BWHERE B.C2 IN (SELECT C.C2 FROM C))SELECT * FROM AWHERE EXISTS (SELECT ‘x’ FROM BWHERE B.C2 IN (SELECT C.C2 FROM C)AND A.C1=B.C1)SELECT * FROM AWHERE A.C1 IN (SELECT B.C1 FROM BWHERE EXISTS (SELECT ‘x’ FROM CWHERE B.C2=C.C2))SELECT * FROM AWHERE EXISTS (SELECT ‘x’ FROM BWHERE EXISTS (SELECT ‘x’ FROM CWHERE B.C2=C.C2)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 –Locate the most–efficient <strong>SQL</strong> <strong>for</strong> the ASE environment


Original <strong>SQL</strong>Elapsed Time: 0.763 sFastest <strong>SQL</strong> AlternativeElapsed time: 0.060 s


Polling QuestionYes or NoHave you used ASE’sAbstract Plans?


Abstract PlanASE<strong>SQL</strong>OptimizerBased on thedatabase statisticsto generate a queryplanExecutionBased on the savedabstract plan togenerate a queryplan


<strong>Sybase</strong> <strong>SQL</strong> Expert Facts• <strong>Sybase</strong> <strong>SQL</strong> Expert (SSE) is sold as an option to ASE• Windows-based product, Client-based product, no server-sideinstallation• Databases supported:– From version 11.0 to ASE 12.5.0.3– To capture <strong>SQL</strong> from the <strong>Sybase</strong> Monitor Server, ASE 11.5 or above required– To use Abstract Plans, ASE 12.0 or above required• Open Client/CT-Lib version 11 or above• <strong>Sybase</strong> Monitor Server is only needed to capture currently running<strong>SQL</strong>; you can use <strong>SQL</strong> Expert without the <strong>Sybase</strong> Monitor Server• Non-intrusive– Scanning and optimization process occurs in the PC; no over-head to thedatabase


Key <strong>SQL</strong> Expert Benefits• Automates current manual tuning ef<strong>for</strong>ts• Solves the problem consuming 60–90% of IT resources• Delivers immediate and quantifiable applicationper<strong>for</strong>mance gains• Heightens staff expertise• Increases staff productivity• Establishes benchmarks and per<strong>for</strong>mance standardsUse <strong>Sybase</strong> <strong>SQL</strong> Expert to ensure onlythe best per<strong>for</strong>ming statements are used.


Interview with MichaelBrundige, Sr. <strong>Sybase</strong>DBA of Brinks

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

Saved successfully!

Ooh no, something went wrong!