Top 5 Issues that Cannot be Resolved by DBAs (other than missed bind variables)

Similar documents
Do-It-Yourself Data Migration

Oracle 9i Application Development and Tuning

Data Tracking: On the Hunt for Information About Your System

Ultra-High Performance SQL and PL/SQL in Batch Processing

Oracle DB-Tuning Essentials

Interpreting Explain Plan Output. John Mullins

Top 7 Plan Stability Pitfalls & How to Avoid Them. Neil Chandler Chandler Systems Ltd UK

IT-Tage Dezember 2016 Frankfurt am Main Maritim Hotel

Advanced Oracle SQL Tuning v3.0 by Tanel Poder

Tuning SQL without the Tuning Pack. John Larkin JP Morgan Chase

<Insert Picture Here> DBA s New Best Friend: Advanced SQL Tuning Features of Oracle Database 11g

Oracle Database Performance Tuning, Benchmarks & Replication

Full Throttle: SQL Tuning & Ressource Consumption Appr.

Oracle Performance Tuning. Overview of performance tuning strategies

SQL Performance Hero and OMG Method Play the Anti-patterns Greatest Hits. Jeff Jacobs Jeffrey Jacobs & Associates

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Answer: Reduce the amount of work Oracle needs to do to return the desired result.

Querying Data with Transact SQL

Oracle Database 11g: SQL Tuning Workshop

20 Essential Oracle SQL and PL/SQL Tuning Tips. John Mullins

Advanced Database Systems

Oracle Database 11g: SQL and PL/SQL Fundamentals

Advanced Oracle Performance Troubleshooting. Query Transformations Randolf Geist

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Ensuring Optimal Performance. Vivek Sharma. 3 rd November 2012 Sangam 2012

Query Optimization, part 2: query plans in practice

Oracle Database: Introduction to SQL/PLSQL Accelerated

Now, we can refer to a sequence without having to use any SELECT command as follows:

Practical MySQL indexing guidelines

Does Exadata Need Performance Tuning? Jože Senegačnik, Oracle ACE Director, Member of OakTable DbProf d.o.o. Ljubljana, Slovenia

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

Introduction to Computer Science and Business

Server-Side Development for the Cloud

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL Gone Wild: Taming Bad SQL the Easy Way (or the Hard Way) Sergey Koltakov Product Manager, Database Manageability

PASSWORDS TREES AND HIERARCHIES. CS121: Relational Databases Fall 2017 Lecture 24

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

SQL. Dean Williamson, Ph.D. Assistant Vice President Institutional Research, Effectiveness, Analysis & Accreditation Prairie View A&M University

Architettura Database Oracle

Oracle Database: Introduction to SQL Ed 2

5. Single-row function

Independent consultant. Oracle ACE Director. Member of OakTable Network. Available for consulting In-house workshops. Performance Troubleshooting

Introduction to SQL/PLSQL Accelerated Ed 2

20461: Querying Microsoft SQL Server 2014 Databases

6232B: Implementing a Microsoft SQL Server 2008 R2 Database

Exploring Best Practices and Guidelines for Tuning SQL Statements

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Join Methods. Franck Pachot CERN

Independent consultant. Oracle ACE Director. Member of OakTable Network. Available for consulting In-house workshops. Performance Troubleshooting

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Scaling To Infinity: Making Star Transformations Sing. Thursday 15-November 2012 Tim Gorman

Creating and Managing Tables Schedule: Timing Topic

QUERY TRANSFORMATION Part 1

Oracle BI 11g R1: Build Repositories Course OR102; 5 Days, Instructor-led

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Advanced indexing methods Usage and Abusage. Riyaj Shamsudeen Ora!nternals

7. Query Processing and Optimization

REPORT ON SQL TUNING USING INDEXING

New Oracle 12c Features for Developers

ORACLE DATABASE 12C INTRODUCTION

Oracle Syllabus Course code-r10605 SQL

Topics Fundamentals of PL/SQL, Integration with PROIV SuperLayer and use within Glovia

TEN QUERY TUNING TECHNIQUES

COURSE OUTLINE: Querying Microsoft SQL Server

Appendix: Application of the Formal Principle for the Analysis of Performance Problems After an Oracle Migration

Quo Vadis SQLTXPLAIN

Table of Contents. Oracle SQL PL/SQL Training Courses

The Oracle Optimizer Explain the Explain Plan O R A C L E W H I T E P A P E R A P R I L

Adaptive Optimization. Presented by: Kerry Osborne Red Gate Webinar, Nov. 2013

SQL Coding Guidelines

Optimizing and Simplifying Complex SQL with Advanced Grouping. Presented by: Jared Still

Presentation Abstract

UCB CS61C : Machine Structures

ORACLE: PL/SQL Programming

TECHNOLOGY: Ask Tom. As Published In. May/June 2011

Arrays are a very commonly used programming language construct, but have limited support within relational databases. Although an XML document or

ColdFusion Summit 2016

1 Prepared By Heena Patel (Asst. Prof)

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

INFSCI 2711 Database Analysis and Design Example I for Final Exam: Solutions

PART I Core Ideas and Elements of PL/SQL Performance Tuning

New York Oracle Users Group. September 26, 2002 New York, NY

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

Manipulating CBO in an OLTP Environment

Optimized Analytical Processing New Features with 11g R2

CS317 File and Database Systems

Introduction to Computer Science and Business

Slides by: Ms. Shree Jaswal

Oracle DB-Tuning Essentials

Find All Tables Containing Column With Specified Name Oracle

Querying Microsoft SQL Server 2012/2014

Oracle Sql Tuning- A Framework

Oracle SQL Internals Handbook. Donald K. Burleson Joe Celko Dave Ensor Jonathan Lewis Dave Moore Vadim Tropashko John Weeg

Oracle Database: SQL and PL/SQL Fundamentals

Tuna Helper Proven Process for SQL Tuning. Dean Richards Senior DBA, Confio Software

Oracle Database 12c SQL Fundamentals

Vijay Mahawar

Introduction to Database Systems CSE 344

Querying Microsoft SQL Server 2008/2012

Session id: The Self-Managing Database: Guided Application and SQL Tuning

Transcription:

Top 5 Issues that Cannot be Resolved by DBAs (other than missed bind variables) March 12, 2013 Michael Rosenblum Dulcian, Inc. www.dulcian.com 1 of 43

Who Am I? Misha Oracle ACE Co-author of 2 books PL/SQL for Dummies Expert PL/SQL Practices Won ODTUG 2009 Speaker of the Year Known for: SQL and PL/SQL tuning Complex functionality Code generators Repository-based development 2 of 43

Houston, we have a problem! Common thought process: Our IT system has an new issue OMG! Production code should not be touched (scary!) DBAs should be able to do something. Reasoning: Configuration of the database is NOT considered production code. DBAs are usually on staff, while the majority of developers are contractors. In the Oracle universe, DBAs are considered to be the most knowledgeable. 3 of 43

Results: Black Hawk Down Significant system architectural problems are covered up using tactical bug-fixes. Systems become even less maintainable and more fragile (I ve seen 11g systems with RBO still enabled!) Architects and developers become lazy. They expect DBAs to adjust everything afterwards. DBAs become frustrated and remove all privileges from developers. 4 of 43

So what? Yes, there are problems that DBAs cannot fix. No, I will NOT talk about bind variables But I will discuss: Problems usually passed to DBAs Correct solutions of those problems Potential workarounds in cases when a real fix is indeed impossible 5 of 43

Personal Top 5 Non-DBA issues Architect s mistakes: 1. Smart columns 2. STUFF table 3. Insufficient hierarchical structures Developer s mistakes: 4. Datatype misuse 5. Misuse of user-defined functions 6 of 43

Issue 1: Smart Columns 7 of 43

Column Column vs. Attribute Represents a single logical attribute Does not make sense if split Smart column Has internal structure May even change meaning depending upon the data Reasons for use: Save time when querying closely related data elements Avoid changes to table structures 8 of 43

Organization rollup Example of Smart Columns (1) Pipe-delimited combination of Region/State/City/Zip Why is it a problem? Adding extra level to rollup is an extremely challenging task. Search is very expensive. What should be done: Split smart columns Aggregate them back using either virtual columns or views 9 of 43

Example of Smart Columns (2) Answers on questionnaires: Single text line where number of characters = number of questions: YYYNNNNYYNY Why is it a problem? Versioning of question sets could cause data corruption. What should be done: High-quality version control Function-based indexes for the most frequently referenced questions 10 of 43

Issue 2: STUFF Table 11 of 43

Reasons for generic solutions: Changes are costly. Over-Generalization Trap We feel protected against the future. Generic models are cool (especially now with the Big Data movement) BUT Generic solutions often mask incomplete understanding of subject area. Generic solutions in one area could cause major issues in others. 12 of 43

Almost Totally Useless Generic Model Object 1 1 0..* 0..* Assoc 1 0..* AttribValue - Name - Value_NR - Value_DT - Value_TX 13 of 43

Data entry: Why is it a bad idea? Uses a lot of operations to retrieve a single object Data quality is hard to enforce. Data retrieval Indexes are useless. CBO goes crazy. Performance is sporadic and does not follow any meaningful logic. Functional complex reporting is impossible. 14 of 43

Although There are cases when key-value stores are perfect (NoSQL environments) BUT they should not be mixed with: OLTP solutions when high data quality is required Heavy reporting workload What could be done: Storage is cheap. Create duplicate structures that would look like real tables 15 of 43

Issue 3: Insufficient Hierarchical Structures 16 of 43

Good Idea/Bad Implementation Recursion Powerful modeling technique Can be used for a number of reasons BUT Linked lists (e.g. contract versions) Storage of tree structures (e.g. organizational hierarchy) Storage mechanisms are wrong, which causes a lot of issues 17 of 43

Pseudo-Recursion Trap Real recursion 0..1 THING Child of 0..* Kind of -recursion THING 1 < Child of 0..* 1 < Parent of 0..* THING ASSOCIATION from/to date 18 of 43

Reasons why people do it: Versioning Historical data Reporting purposes Why it is challenging: Why is it a trap? Hierarchical data consistency is not enforced. Timing can be very easily be off. What should be done: Very strict data quality checking! Denormalized data sources for querying 19 of 43

Issue 4: Datatype Misuse 20 of 43

Datatypes ARE parts of metadata Datatypes as Constraints (1) Oracle uses them to make a lot of decisions about execution plans. Wrong datatypes often mean wrong Explain Plans. Wrong datatypes open possibilities for corrupted data. What should be done: Fix datatypes as much as possible. Use views/virtual columns to separate storage and representation. Worst case Add check constraints to at least enforce data quality. 21 of 43

Problem: Date vs. Varchar2 (1) storing DATE as VARCHAR2 (~ YYYYMMDD) Reasons of issues Date range {December 31, 2012 to January 1, 2013} consists of only two distinct date values The textual range { 20121231, 20130101 } is huge. Since it is text, starting with the 4 th character there could be any valid character in the current charset. Result: Column-level statistics are not utilized and indexes are often ignored. What could be done: Build virtual column (TO_DATE) and let developers use it. 22 of 43

Date vs Varchar2(2) create table misha_date01 as select owner, object_name, to_char(created,'yyyymmdd') created_tx, created created_dt from dba_objects create index misha_date_tx_idx on misha_date01(created_tx); create index misha_date_dt_idx on misha_date01(created_dt); begin dbms_stats.gather_table_stats(user,'misha_date01'); end; 23 of 43

Date vs Varchar2 (3) SQL> explain plan for 2 select * 3 from misha_date01 4 where created_tx between '20121231' and '20130101'; Explained. SQL> select * from table(dbms_xplan.display()); PLAN_TABLE_OUTPUT ----------------------------------------------------------------------- 0 SELECT STATEMENT 48100 2113K 299 (1) * 1 TABLE ACCESS FULL MISHA_DATE01 48100 2113K 299 (1) ----------------------------------------------------------------------- SQL> explain plan for 2 select * 3 from misha_date01 Full table scan 4 where created_dt between to_date('20121231','yyyymmdd') 5 and to_date('20130101','yyyymmdd'); Explained. SQL> select * from table(dbms_xplan.display()); ------------------------------------------------------------------------ 0 SELECT STATEMENT 212 9540 1 TABLE ACCESS BY INDEX ROWID MISHA_DATE01 212 9540 * 2 INDEX RANGE SCAN MISHA_DATE_DT_IDX 212 ------------------------------------------------------------------------ Index is used! 24 of 43

Implicit datatype conversion Implicit datatype conversion is EVIL! Security nightmare A lot of confusion everywhere: Statistics Execution Plans Overload calls 25 of 43

Number vs Varchar2 SQL> explain plan for select * from misha_date01 2 where created_tx = 20121231; SQL> select * from table(dbms_xplan.display()); ------------------------------------------------------------------------ Id Operation Name Rows Bytes Cost (%CPU) ------------------------------------------------------------------------ 0 SELECT STATEMENT 573 25785 300 (1) * 1 TABLE ACCESS FULL MISHA_DATE01 573 25785 300 (1) ------------------------------------------------------------------------ Full table scan SQL> explain plan for select * from misha_date01 2 where created_tx = '20121231'; SQL> select * from table(dbms_xplan.display()); ------------------------------------------------------------------------ Id Operation Name Rows Bytes ------------------------------------------------------------------------ 0 SELECT STATEMENT 573 25785 1 TABLE ACCESS BY INDEX ROWID MISHA_DATE01 573 25785 * 2 INDEX RANGE SCAN MISHA_DATE_TX_IDX 573 ------------------------------------------------------------------------ Index is used! 26 of 43

Issue 5: Misuse of User-Defined Functions 27 of 43

Why bother? PL/SQL functions as a part of SQL can cause a lot of side effects. Cost of SQL to PL/SQL context switch is very high. Depending upon the execution plan, the same function could be called different numbers of times for the same SQL statement. What could be done: Make sure that the CBO takes into account the impact of PL/SQL functions on the overall cost. Manage the total number of calls. 28 of 43

OO-like get/set APIs Problem Areas/Solutions PL/SQL functions in SELECT and WHERE clauses Managing execution order Short-circuit evaluation Statistics-based cost Decreasing total number of function calls Scalar sub-query caching RESULT_CACHE In-line views based on PL/SQL functions returning nested tables 29 of 43

OO-like thinking People are accustomed to GET/SET APIs for every attribute Real story of 1 insert into table with 100 attributes 1 insert with only PK column 99 updates using PK System collapsed under its own weight because of thousands of roundtrips What could be done: train your developers to NOT use JAVA-style coding in PL/SQL development 30 of 43

PL/SQL functions inside of SQL The CBO is not psychic and cannot figure out what is going on inside of your PL/SQL function. UNLESS you tell it using associated statistics, because Oracle defaults are not perfect: Selectivity 1% (0.01) CPU cost 3000 I/O cost 0 Network cost - 0 There are two ways of doing it: Simple way Associate statistics with functions <function name> Default selectivity <value> Default cost (<CPU>,<IO>,<NETWORK>) Complex way [outside of the scope for today] Associate statistics with functions <function name> using <special object type> 31 of 43

Why does it matter? Because you may have multiple functions in the same SQL statement! Example Two functions: One is light and one is heavy associate statistics with functions f_misha_light_tx default selectivity 0.1 default cost (0,0,0); associate statistics with functions f_misha_heavy_tx default selectivity 0.1 default cost (99999,99999,99999); Both of them are used in the query select /*+ gather_plan_statistics */* from emp where f_misha_heavy_nr(empno) = 1 and f_misha_light_nr (empno) = 0 32 of 43

Explain Plan Impact SQL_ID a5u0gvdt0ju36, child number 0 ------------------------------------- select /*+ gather_plan_statistics */* from emp where f_misha_heavy_tx(empno) = 1 and f_misha_light_tx (empno) = 0 Plan hash value: 3956160932 ----------------------------------------------------------------------------- Id Operation Name E-Rows A-Rows A-Time Buffers ----------------------------------------------------------------------------- 0 SELECT STATEMENT 14 00:00:00.01 33 * 1 TABLE ACCESS FULL EMP 1 14 00:00:00.01 33 ----------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(("f_misha_light_tx"("empno")=0 AND "F_MISHA_HEAVY_TX"("EMPNO")=1)) Order of functions has been changed! 33 of 43

Function calls (1) Setup: create package misha_pkg is v_nr number:=0; end; create or replace function f_change_tx (i_tx varchar2) return varchar2 is begin misha_pkg.v_nr:=misha_pkg.v_nr+1; return lower(i_tx); end; Create or replace procedure p_check is begin end; dbms_output.put_line('fired:' misha_pkg.v_nr); misha_pkg.v_nr:=0; 34 of 43

Function calls (2) SQL> select empno, ename, f_change_tx(job) job_change_tx 2 from emp;... 14 rows selected. SQL> exec p_check Fired:14 PL/SQL procedure successfully completed. SQL> select empno, ename, (select f_change_tx(job) from dual) 2 from emp;... 14 rows selected. Scalar sub-query SQL> exec p_check Fired:5 PL/SQL procedure successfully completed. SQL> Only 5 executions! 35 of 43

Function calls (3) create or replace function f_change_tx (i_tx varchar2) return varchar2 result_cache is begin misha_pkg.v_nr:=misha_pkg.v_nr+1; return lower(i_tx); end; Enable function result cache SQL> select empno, ename, f_change_tx(job) from emp;... 14 rows selected. SQL> exec p_check Fired:5 Only distinct values SQL> select empno, ename, f_change_tx(job) from emp;... 14 rows selected. SQL> exec p_check Fired:0 No calls cache only! 36 of 43

Collection IN-lists (1) It is very convenient to build an IN-list as a collection and pass it to a WHERE clause But Oracle may or may not correctly interpret incoming data! Example (setting) create table misha_demo_inlist as select object_id, created from dba_objects where owner = 'MISHA' and object_id is not null; alter table misha_demo_inlist add constraint misha_demo_inlist_pk primary key (object_id) using index; begin dbms_stats.gather_table_stats(user,'misha_demo_inlist'); end; 37 of 43

Collection IN-lists (2) create type id_tt is table of number; select /*+ gather_plan_statistics*/ max(created) from misha_demo_inlist where object_id in ( select t.column_value from table(id_tt(227011,227415)) t ) 38 of 43

SQL_ID 6509b6f6d1mgy, child number 0 ------------------------------------- select /*+ gather_plan_statistics */ max(created) from misha_demo_inlist where object_id in ( select t.column_value from table(id_tt(227011,227415)) t) Plan hash value: 22551403 Collection IN-lists (3) --------------------------------------------------------------------------------------- Id Operation Name E-Rows A-Rows --------------------------------------------------------------------------------------- 0 SELECT STATEMENT 1 1 SORT AGGREGATE 1 1 * 2 HASH JOIN 8168 2 3 COLLECTION ITERATOR CONSTRUCTOR FETCH 8168 2 4 TABLE ACCESS FULL MISHA_DEMO_INLIST 29885 29885 --------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("object_id"=value(kokbf$)) Wrong cardinality 39 of 43

Collection IN-lists (4) Oracle does not correctly recognize how many objects are in the collection. Alternatives: Explicit cardinality hint select /*+ gather_plan_statistics */ max(created) from misha_demo_inlist where object_id in ( select /*+ cardinality (t 2) */t.column_value from table(id_tt(227011,227415)) t ) Dynamic sampling select /*+ gather_plan_statistics */ max(created) from misha_demo_inlist where object_id in ( select /*+ dynamic_sampling (t 4) */t.column_value from table(id_tt(227011,227415)) t ) 40 of 43

Collection IN-lists (5) Result for both options is the same and uses the index! ---------------------------------------------------------------------------------------- Id Operation Name E-Rows A-Rows ---------------------------------------------------------------------------------------- 0 SELECT STATEMENT 1 1 SORT AGGREGATE 1 1 2 NESTED LOOPS 2 3 NESTED LOOPS 2 2 4 COLLECTION ITERATOR CONSTRUCTOR FETCH 2 2 * 5 INDEX UNIQUE SCAN MISHA_DEMO_INLIST_PK 1 2 6 TABLE ACCESS BY INDEX ROWID MISHA_DEMO_INLIST 1 2 ---------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 5 - access("object_id"=value(kokbf$)) Dynamic sampling will also have a special note about its level (it can be lower than requested) Note ----- - dynamic sampling used for this statement (level=2) Correct cardinality! 41 of 43

Summary Not all errors can be fixed by DBAs. Strategic problems should not be covered by tactical solutions. Enterprise-level thinking is required from the very beginning. and let s not forget about bind variables 42 of 43

Contact Information Michael Rosenblum mrosenblum@dulcian.com Blog wonderingmisha.blogspot.com Website www.dulcian.com Available now: Expert PL/SQL Practices 43 of 43