13
www.regouniversity. com Clarity Educational Community www.regouniversity. com Clarity Educational Community Creating and Tuning SQL Queries that Engage Users Presented by: Dave Matzdorf | Date Prepared: April 1, 2015

Www.regouniversity.com Clarity Educational Community Clarity Educational Community Creating and Tuning SQL Queries that Engage Users

Embed Size (px)

Citation preview

www.regouniversity.comClarity Educational Community

www.regouniversity.comClarity Educational Community

Creating and Tuning SQL Queries that Engage Users

Presented by: Dave Matzdorf | Date Prepared: April 1, 2015

2 Clarity Educational Community

2 Clarity Educational Community

Agenda• Goals• Reusing Code• Know Your Data• Know Your Database• Bottlenecks• Tips and Tricks• Exercises

3 Clarity Educational Community

3 Clarity Educational Community

Goal• Improve the execution time of SQL queries

• Minimize system resources required to execute queries

• Utilize lesser known built in functions to avoid complexity

• Give the SQL optimizer the best chance of selecting a good execution plan

4 Clarity Educational Community

4 Clarity Educational Community

Reusing code• Copying code

– Over-reliance can lead to bloated and inefficient code– Review every time code is used– Understand the code

• Database views– One size does not fit all– Stacked views

5 Clarity Educational Community

5 Clarity Educational Community

Know your data• What areas of the application are being used and in what

volumes

• Be a partner in the requirements, not just a developer

• Understand the data model– Entity Relationship Diagram– Data dictionary tables– SQL Traces

6 Clarity Educational Community

6 Clarity Educational Community

Know Your Database• SQL vs Procedural SQL (PL/SQL, T-SQL)

– Do not use PL/SQL when SQL can be used– Row by row vs. set based processing– Context switching

• Hard parsing vs. soft parsing– Query caching– Hard parsing– Soft parsing

• Dynamic SQL– Limited query caching benefits– Can often be written as static SQL

7 Clarity Educational Community

7 Clarity Educational Community

Know Your Database – Bind Variables• Bind variables

– Finding queries in the cache without exact text match• SELECT ID FROM SRM_RESOURCES WHERE UNIQUE_NAME =

‘dmatzdorf’• SELECT ID FROM SRM_RESOURCES WHERE UNIQUE_NAME = ?

– Escape characters– Data type handling– Crucial when executing SQL within a programming loop

8 Clarity Educational Community

8 Clarity Educational Community

Common Bottlenecks• IN vs. Exists

– IN is typically better when the inner query contains a small result set– EXISTS is typically better when the inner query contains a large result

set

• EXISTS vs. DISTINCT– DISTINCT produces the entire result set (including duplicates), sorts,

and then filters out duplicates– EXISTS proceeds with fetching rows immediately after the sub-query

condition has been satisfied the first time

• Double dipping– UNION queries– Repeated Inline Views

9 Clarity Educational Community

9 Clarity Educational Community

Subquery Factoring – WITH Clause• Simplify complex queries• Reduce repeated table access by generating temporary

datasets during query execution• Can be used as an inline view or a table

10 Clarity Educational Community

10 Clarity Educational Community

Analytic Functions – Deriving Totals• Replaces complex logic to summarize data

• Can be partitioned to derive summaries at different levels

• Provide simple solutions to things like running totals

11 Clarity Educational Community

11 Clarity Educational Community

Analytic Functions – Selecting Records• Replaces complex logic select certain records

• Select most recent status report

• Fetch prior or next records

12 Clarity Educational Community

12 Clarity Educational Community

Working Examples• Pull a list of users that belong to at least one security group

• Get the first resource assigned to each project team

• What percentage of total project allocation belongs to each team member

13 Clarity Educational Community

13 Clarity Educational Community

Questions

Phone888.813.0444

[email protected]

Websitewww.regouniversity.com

We hope that you found this session informative and worthwhile. Our primary goal was to increase your understanding of the topic and CA PPM in general.

There were many concepts covered during the session, if you would like to contact any presenter with questions, please reach out to us.

Thank you for attending regoUniversity 2015!