0% found this document useful (0 votes)
2 views12 pages

Unit 7: Case 2 - Applying The Solution

The document discusses a case study on optimizing SQL queries by reducing intermediate results and incorporating INNER JOINs to enhance performance. It includes various query structures and execution times, demonstrating the effectiveness of these optimizations. The final rewritten query successfully executes in under 100ms, showcasing significant improvements in processing efficiency.

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)
2 views12 pages

Unit 7: Case 2 - Applying The Solution

The document discusses a case study on optimizing SQL queries by reducing intermediate results and incorporating INNER JOINs to enhance performance. It includes various query structures and execution times, demonstrating the effectiveness of these optimizations. The final rewritten query successfully executes in under 100ms, showcasing significant improvements in processing efficiency.

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/ 12

Week 4: Case Studies

Unit 7: Case 2 – Applying the Solution


Case 2 – Applying the Solution
Reduce intermediate results
20 (20) rows

Inclusive Time : 1,653.2 ms


Exclusive Time : 13.4 ms

Column Search #3

ORDER BY A b
B 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


Matched records
LIMIT GROUP BY
b 1
d 1

Order By INNER JOIN “What if we make matched records to be


processed in Column Search #2 in order
to reduce the number of records to be
T1 T2 T3 materialized in column search 2?"

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


Case 2 – Applying the Solution
Reduce intermediate results
20 (20) rows

Inclusive Time : Fast


Exclusive Time : Fast

Column Search #3
Matched records
ORDER BY b 1
A b
B d d 1
C z N U L L V A L U E
LEFT OUTER
JOIN Not-Matched records

20 (20) rows Small (10,000,000) rows Column Search #2 would return a small
Inclusive Time : 2.2 ms Inclusive Time : Fast
set of records.
Exclusive Time : 2.2 ms Exclusive Time : Fast

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN “What if we make matched records to be


processed in Column Search #2 in order
to reduce the number of records to be
T1 T2 T3 materialized in column search 2?"

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


Case 2 – Applying the Solution
Add INNER JOIN T1.A = T4.C
INNER JOIN

T1

Column Search #3
Matched records We can get matched records
ORDER BY A b b 1 using INNER JOIN
B d d 1
C z N U L L V A L U E
LEFT OUTER
JOIN Not-Matched records

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 4


Case 2 – Applying the Solution
Add INNER JOIN

Column Search #3

ORDER BY

LEFT OUTER
JOIN

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN T1.A = T4.C

T1 T2 T3 T1

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


Case 2 – Applying the Solution
Rewrite query

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


FROM T1
LEFT OUTER JOIN (
SELECT T2.C
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)
T4.C
ORDER BY T1.A
LIMIT 20 WITH HINT (IGNORE_PLAN_CACHE);

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


Case 2 – Applying the Solution
Rewrite query

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)
INNER JOIN T1 ON (T1.A=T2.C)
GROUP BY T2.C ) T4
ON (T1.A=T4.C)
ORDER BY T1.A
LIMIT 20 WITH HINT (IGNORE_PLAN_CACHE);

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


Case 2 – Applying the Solution
Rewrite query – Verification

Column Search #3

ORDER BY

LEFT OUTER
JOIN

20 (100,000) rows 9,951 (100,000) rows

Column Search #1 Column Search #2

LIMIT GROUP BY

Order By INNER JOIN T1.A = T4.C

T1 T2 T3 T1

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


Case 2 – Applying the Solution
Rewrite query – Add INNER JOIN into column search #2

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)
INNER JOIN T1 ON (T2.C = T1.A)
GROUP BY T2.C ) T4 ON (T1.A=T4.C)
ORDER BY T1.A Column Search #1 Column Search #2
LIMIT 20 WITH HINT (IGNORE_PLAN_CACHE);

Statement 'SELECT T1.A, T1.B, T4.C, T4.E, T4.H FROM T1 LEFT OUTER JOIN ( SELECT T2.C,...'
successfully executed in 98 ms 184 µs (server processing time: 11 ms 26 µs)
Fetched 20 row(s) in 0 ms 185 µs (server processing time: 0 ms 0 µs)

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


Case 2 – Applying the Solution
Rewrite query – Add INNER JOIN into column search #2

Column Search #3

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


Statement
FROM T1 'SELECT T1.A, T1.B, T4.C, T4.E, T4.H FROM T1 LEFT OUTER JOIN ( SELECT T2.C,...'
LEFT OUTER JOIN (
successfully executed in 98 ms 184 µs (server processing time:
SELECT T2.C,
SUM(CASE WHEN T2.E>10 THEN 10 ELSE 0 END) AS E,
11 ms 26 µs)
Fetched 20 row(s) in 0 ms 185 µs (server processing time: 0 ms 0 µs)
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)
INNER JOIN T1 ON (T2.C = T1.A)
GROUP BY T2.C ) T4 ON (T1.A=T4.C)
ORDER BY T1.A
Adding INNER JOIN reduces intermediate
Column Search #1results and
Column Search #2
LIMIT 20 WITH HINT (IGNORE_PLAN_CACHE);
execution time to under 100ms

Statement 'SELECT T1.A, T1.B, T4.C, T4.E, T4.H FROM T1 LEFT OUTER JOIN ( SELECT T2.C,...'
successfully executed in 98 ms 184 µs (server processing time: 11 ms 26 µs)
Fetched 20 row(s) in 0 ms 185 µs (server processing time: 0 ms 0 µs)

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


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