0% found this document useful (0 votes)
14 views21 pages

Unit 5: Case 2 - Finding SQL Hint

The document presents a case study focused on optimizing SQL queries by analyzing data flow and execution times for various SQL operations. It highlights the performance bottlenecks caused by large intermediate results during joins and suggests using SQL hints like JOIN_THRU_AGGR to improve execution time. The final SQL query demonstrates a successful execution with improved performance metrics after applying the hint.

Uploaded by

vv2jsrrym8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views21 pages

Unit 5: Case 2 - Finding SQL Hint

The document presents a case study focused on optimizing SQL queries by analyzing data flow and execution times for various SQL operations. It highlights the performance bottlenecks caused by large intermediate results during joins and suggests using SQL hints like JOIN_THRU_AGGR to improve execution time. The final SQL query demonstrates a successful execution with improved performance metrics after applying the hint.

Uploaded by

vv2jsrrym8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Week 4: Case Studies

Unit 5: Case 2 – Finding SQL Hint


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN


E
B
A
D
T1 T2 T3
E
100,000 rows from T1
© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2
Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

A
B
C Order By INNER JOIN
D
E
Sort records
T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 3


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

A Column Search #1 Column Search #2


B
C LIMIT GROUP BY
Return only 20 rows

Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 4


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

A Column Search #1 Column Search #2


B
C LIMIT GROUP BY

Order By INNER JOIN


a 1
b 2
c 3
T1 T2 T3 d 4
e 5

T2 T3
© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 5
Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

A Column Search #1 Column Search #2


B
C LIMIT GROUP BY
a 1
b 2
c 3
Order By d 4
INNER JOIN e 5

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 6


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

A Column Search #1 Column Search #2


B a 1
C LIMIT GROUP BY a 2
b 1
c 3
c 4
T2.C=T3.F AND T2.D=T3.G
Order By d 1
INNER JOIN e 5
e 6

The join produces huge intermediate result


T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 7


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

A Column Search #1 Column Search #2


B a 1
C LIMIT GROUP BY a 2
b 1
c 3
c 4
Order By d 1
INNER JOIN e 5
e 6

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 8


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms
Grouping Column

A Column Search #1 Column Search #2


B a 1
C LIMIT GROUP BY a 2
b 1
c 3
c 4
Order By d 1
INNER JOIN e 5
e 6

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 9


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows The Group By still returns a lot of records.
Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms
This is the bottleneck.
Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms
Grouping Column

A Column Search #1 Column Search #2


B a
C LIMIT GROUP BY b
c
d
e
Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 10


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3
a
b
ORDER BY
A c
B d
C e

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 11


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3 Join keys


a
b
ORDER BY
A b c
B d d
C z e

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 12


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3 Join keys


a
b
ORDER BY
A b c
B d d
C z e

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 13


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3
Matched records
ORDER BY
A b b
B d d
C z

LEFT OUTER JOIN

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 14


Case 2 – Finding SQL Hint
Understand data flow
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms The intermediate result can be reduced by this
join because the left child has only 20 rows
Column Search #3
Matched records
ORDER BY
A b b
B d d
C z N U L L V A L U E

LEFT OUTER JOIN Non-matched records

20 (20) rows 1,000,535 (10,000,000) rows

Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms


Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 15


Case 2 – Finding SQL Hint
Push down key operators
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3
Matched records
ORDER BY
A b b
Possible Key Reducer B d d
C z N U L L V A L U E
A part which improves LEFT OUTER JOIN Non-matched records
performance

20 (20) rows 1,000,535 (10,000,000) rows


JOIN_THRU_AGGR?
Inclusive Time : 2.2 ms Inclusive Time : 1,637.3 ms
Exclusive Time : 2.2 ms Exclusive Time : 1,637.3 ms

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN

T1 T2 T3

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 16


Case 2 – Finding SQL Hint
Try SQL hint JOIN_THRU_AGGR

SELECT T1.A, T1.B, T4.C, T4.E, T4.H


FROM T1
LEFT OUTER JOIN (
SELECT T2.C,
SUM(CASE WHEN T2.E>10 THEN 10 ELSE 0 END) AS E,
SUM(CASE WHEN T3.H<=50 THEN 0 ELSE T3.H END) AS H
FROM T2 JOIN T3 ON (T2.C=T3.F AND T2.D=T3.G)
GROUP BY T2.C ) T4 ON (T1.A=T4.C)
ORDER BY T1.A
LIMIT 20 WITH HINT (IGNORE_PLAN_CACHE, JOIN_THRU_AGGR);

Statement 'SELECT T1.A, T1.B, T4.C, T4.E, T4.H FROM T1 LEFT OUTER JOIN ( SELECT T2.C,...'
successfully executed in 1.824 seconds (server processing time: 1.686 seconds)
Fetched 20 row(s) in 0 ms 312 µs (server processing time: 0 ms 0 µs)

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 17


Case 2 – Finding SQL Hint
Try SQL hint JOIN_THRU_AGGR

Column Search #3

SELECT T1.A, T1.B, T4.C, T4.E, T4.H


FROM T1
LEFT OUTER JOIN (
SELECT T2.C,
SUM(CASE WHEN T2.E>10 THEN 10 ELSE 0 END) AS E,
SUM(CASE WHEN T3.H<=50 THEN 0 ELSE T3.H END) AS H
FROM T2 JOIN T3 ON (T2.C=T3.F AND T2.D=T3.G)
GROUP BY T2.C ) T4 ON (T1.A=T4.C)
ORDER BY T1.A
LIMIT 20 WITH HINT (IGNORE_PLAN_CACHE, JOIN_THRU_AGGR); Column Search #1 Column Search #2

Statement 'SELECT T1.A, T1.B, T4.C, T4.E, T4.H FROM T1 LEFT OUTER JOIN ( SELECT T2.C,...'
successfully executed in 1.824 seconds (server processing time: 1.686 seconds)
Fetched 20 row(s) in 0 ms 312 µs (server processing time: 0 ms 0 µs)

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 18


Case 2 – Finding SQL Hint
Try SQL hint JOIN_THRU_AGGR

Column Search #3

SELECT T1.A, T1.B, T4.C, T4.E, T4.H


FROM T1
LEFT OUTER JOIN (
SELECT T2.C,
SUM(CASE WHEN T2.E>10 THEN 10 ELSE 0 END) AS E,
SUM(CASE WHEN T3.H<=50 THEN 0 ELSE T3.H END) AS H
FROM T2 JOIN T3 ON (T2.C=T3.F AND T2.D=T3.G) JOIN_THRU_AGGR does not change the plan
GROUP BY T2.C ) T4 ON (T1.A=T4.C)
ORDER BY T1.A
LIMIT 20 WITH HINT (IGNORE_PLAN_CACHE, JOIN_THRU_AGGR); Column Search #1 Column Search #2

Statement 'SELECT T1.A, T1.B, T4.C, T4.E, T4.H FROM T1 LEFT OUTER JOIN ( SELECT T2.C,...'
successfully executed in 1.824 seconds (server processing time: 1.686 seconds)
Fetched 20 row(s) in 0 ms 312 µs (server processing time: 0 ms 0 µs)

© 2020 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 19


Thank you.
Contact information:

[email protected]
Follow all of SAP

www.sap.com/contactsap

© 2020 SAP SE or an SAP affiliate company. All rights reserved.


No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of
SAP SE or an SAP affiliate company.
The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its
distributors contain proprietary software components of other software vendors. National product specifications may vary.
These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or
warranty of any kind, and SAP or its affiliated companies shall not be liable for errors or omissions with respect to the materials.
The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty
statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional
warranty.
In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or
any related presentation, or to develop or release any functionality mentioned therein. This document, or any related presentation,
and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platforms, directions, and
functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason
without notice. The information in this document is not a commitment, promise, or legal obligation to deliver any material, code, or
functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ
materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements, and they
should not be relied upon in making purchasing decisions.
SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered
trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. All other product and service names
mentioned are the trademarks of their respective companies.
See www.sap.com/copyright for additional trademark information and notices.

You might also like