«make it faster»: myths about sql performance - data warehousing with oracle · 2020. 5. 6. ·...

25
BASEL | BERN | BRUGG | BUCHAREST | DÜSSELDORF | FRANKFURT A.M. | FREIBURG I. BR. | GENEVA HAMBURG | COPENHAGEN | LAUSANNE | MANNHEIM | MUNICH | STUTTGART | VIENNA | ZURICH BASEL | BERN | BRUGG | BUCHAREST | DÜSSELDORF | FRANKFURT A.M. | FREIBURG I. BR. | GENEVA HAMBURG | COPENHAGEN | LAUSANNE | MANNHEIM | MUNICH | STUTTGART | VIENNA | ZURICH danischnider.wordpress.com @dani_schnider «Make it faster»: Myths about SQL Performance Dani Schnider, Trivadis AG

Upload: others

Post on 20-Mar-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

BASEL | BERN | BRUGG | BUCHAREST | DÜSSELDORF | FRANKFURT A.M. | FREIBURG I. BR. | GENEVA HAMBURG | COPENHAGEN | LAUSANNE | MANNHEIM | MUNICH | STUTTGART | VIENNA | ZURICHBASEL | BERN | BRUGG | BUCHAREST | DÜSSELDORF | FRANKFURT A.M. | FREIBURG I. BR. | GENEVA HAMBURG | COPENHAGEN | LAUSANNE | MANNHEIM | MUNICH | STUTTGART | VIENNA | ZURICH

danischnider.wordpress.com@dani_schnider

«Make it faster»: Myths about SQL PerformanceDani Schnider, Trivadis AG

Page 2: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

BASEL | BERN | BRUGG | BUCHAREST | COPENHAGEN | DÜSSELDORF | FRANKFURT A.M. | FREIBURG I.BR. GENEVA | HAMBURG | LAUSANNE | MANNHEIM | MUNICH | STUTTGART | VIENNA | ZURICH

Dani Schnider• Senior Principal Consultant at Trivadis AG in

Glattbrugg/Zurich

• Trainer of several Trivadis courses

• Co-Author of Books “Data Warehousing mitOracle” and “Data Warehouse Blueprints”

• Oracle ACE Director

@dani_schnider danischnider.wordpress.com

| HOMEOFFICE

Page 3: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

www.trivadis-training.com/o-opt

Page 4: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

«Die Another Day»Try

Performance tuning can be done at the end of the project

Page 5: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Performance Tuning Tasks

Performance Planning

• Scalability

• System Architecture

• Application Design

• Data Model

• Testing

• etc.

Instance Tuning

• Memory Allocation

• I/O Balancing

• Database Configuration

• System Configuration

• etc.

SQL Tuning

• Indexing

• Gathering Statistics

• Partitioning

• Rewrite SQL Statements

• Hints

• etc.

See Oracle® Database Performance Tuning Guide, Chapter 1, Performance Tuning Overview

One of my first Performance Reviews

Page 6: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

select SNR ,SNRBEZ ,LAND ,KNZNR ,LINR ,SWIFTID ,BRAN ,ORT ,CLID1 ,KGR ,KUBER1 ,BEZ1 ,BEZ2 ,CLID2 ,CLID3 ,KNZNRMU ,SNRMU ,TELEX ,REGION ,ABRKZ ,KGR_SW ,SPKZHS ,INFO ,SPERR ,KUBER2 ,NATION ,ABWME ,TELBEST ,KNZART ,DEPTCO ,KTYP ,RWHG ,NETTING ,TEXT ,KOST ,GEBDAT ,RESDA ,SCHL1 ,SCHL2 ,SCHL3 ,SCHL4 ,SCHL5 ,ORGAN ,BBSNR ,BBKNZNR ,BBSNRG ,REPOFF ,PROVTYP ,ZSPER ,SNRTREU ,STKZ ,RATING1 ,RATING2 ,RATING3 ,PRZ ,MARKTO ,LOESCHUNG ,TESTKEY ,ABWKONTO ,ZAVEIN ,VWCO ,AUFTRAG ,BEREICH ,MANDK ,AEND ,ADAT ,SACHBE ,PRUEFER ,STLAND ,STNR ,KTE ,KTL ,VVTYP ,VVSNR ,PFMAN into :b1,:b2,:b3,:b4,:b5,:b6,:b7,:b8,:b9,:b10,:b11,:b12,:b13,:b14,:b15,:b16,:b17,:b18,:b19,:b20,:b21,:b22,:b23,:b24,:b25,:b26,:b27,:b28,:b29,:b30,:b31,:b32,:b33,:b34,:b35,:b36,:b37,:b38,:b39,:b40,:b41,:b42,:b43,:b44,:b45,:b46,:b47,:b48,:b49,:b50,:b51,:b52,:b53,:b54,:b55,:b56,:b57,:b58,:b59,:b60,:b61,:b62,:b63,:b64,:b65,:b66,:b67,:b68,:b69,:b70,:b71,:b72,:b73,:b74,:b75

fromAKS where rowid =:b76

call count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 9275 2.63 4.30 0 0 0 0Execute 195234 16.46 17.07 0 0 0 0Fetch 195230 40.85 165.61 63452 195230 0 195230------- ------ -------- ---------- ---------- ---------- ---------- ----------total 399739 59.94 186.98 63452 195230 0 195230

Rows Execution Plan------- ---------------------------------------------------

0 SELECT STATEMENT GOAL: CHOOSE1 TABLE ACCESS (BY USER ROWID) OF 'AKS'

One of my first Performance Reviews

The slowest SQL statement

Page 7: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

One of my first Performance Reviews

The data model

Page 8: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

One of my first Performance Reviews

The best solution* for the application

(* I wrote it a little more diplomatically in the review document)

Page 9: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Performance Tuning Tasks

Performance Planning

• Scalability

• System Architecture

• Application Design

• Data Model

• Testing

• etc.

Instance Tuning

• Memory Allocation

• I/O Balancing

• Database Configuration

• System Configuration

• etc.

SQL Tuning

• Indexing

• Gathering Statistics

• Partitioning

• Rewrite SQL Statements

• Hints

• etc.

See Oracle® Database Performance Tuning Guide, Chapter 1, Performance Tuning Overview

Performance must be considered in every phase of the project

Page 10: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

«From Russia With Love»Index

Index is good, full table scan is bad

Page 11: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Which Execution Plan is Better?

A

B

A

Page 12: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Is this a Good Execution Plan?

No

YesYes

Page 13: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Index Scan or Full Table Scan?

It depends...

... on the selectivity of the query

Index Scan is efficent for a strong selectivity (i.e. small percentage of data)

Full Table Scan is efficent for a weakselectivity (i.e. high percentage of data)

Page 14: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

When is an Index better than a Full Table Scan?

Selectivity < 80 %A

Selectivity < 50%B

Selectivity < 20 %C

Selectivity < 5%DD

Page 15: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

LIVE DEMO

«For Your Eyes Only»

Indexes

Page 16: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

«Live and Let Die»Buy

Buy more hardware will give us good performance

Page 17: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Add More Hardware…

KIWI«Kill It With Iron»

Page 18: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

ORA-01652: unable to extend temp

segment by 128 in tablespace TEMP

, c AS (SELECT … )

, d AS (SELECT … )

, e AS (SELECT … )

, f AS (SELECT … )

SELECT DISTINCT …FROM aJOIN bJOIN cJOIN dJOIN eJOIN f

WITH a AS (SELECT … )

, b AS (SELECT … )

Page 19: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

, c AS (SELECT … )

, d AS (SELECT … )

SELECT DISTINCT …FROM aJOIN bJOIN cJOIN d

WITH a AS (SELECT … )

, b AS (SELECT … )Join condition was missing

Increase TEMP tablespaceA

Add missing join conditionBB

Page 20: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

«The Man with the Golden Gun»Hint

We can add hints to solve performance issues

Page 21: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Concept of Hints

Hints “cut” branches of the optimizer decision tree

49 51 52 68 56 71 82 7547

Start with table T1 Start with table T2

IndexIDX1

IndexIDX2

NL Hash Merge

Join with T2 Join with T1

HashNL Merge

/*+ index(T1 IDX2) *//*+ leading(T2) index(T1 IDX2) */

Page 22: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

LIVE DEMO

«For Your Eyes Only»

Hints

Page 23: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Start with EMPLOYEES Start with DEPARTMENTS

NL Hash Merge

Join withDEPARTMENTS

Join withEMPLOYEES

HashNL Merge

/*+ use_nl(e) */

Start with EMPLOYEES Start with DEPARTMENTS

NL Hash Merge

Join withDEPARTMENTS

Join withEMPLOYEES

HashNL Merge

Start with EMPLOYEES Start with DEPARTMENTS

NL Hash Merge

Join withDEPARTMENTS

Join withEMPLOYEES

HashNL Merge

Start with EMPLOYEES Start with DEPARTMENTS

NL Hash Merge

Join withDEPARTMENTS

Join withEMPLOYEES

HashNL Merge

/*+ use_nl(d) */

/*+ leading(d) use_nl(e) */

Page 24: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

«No Time to Die»Lie

Which myths about SQL performance are true?

Page 25: «Make it faster»: Myths about SQL Performance - Data Warehousing with Oracle · 2020. 5. 6. · Instance Tuning •Memory Allocation •I/O Balancing •Database Configuration •System

Which Myths are true?

Performance should be considered after development ✘More memory helps to improve SQL performance ( )

Adding more indexes always improve SQL performance ✘A full table scan is slower than an index scan ✘COUNT(1) is faster than COUNT(*) ✘Hints force the optimizer to use a specific execution plan ( )

Hints reduce the costs of an execution plan ✘The hint /*+ golden_hint */ makes every query faster ✘James Bond always wins