SlideShare ist ein Scribd-Unternehmen logo
1 von 40
SQL
Tunning
By:
Dhananjay Goel
CTO, Alphalogic Inc
www.alphalogicinc.com
Agenda
1. Introduction
2. SQL Processing
3. Optimizer and Execution Plan
4. Accessing Tables
5. Performance Improvement Consideration
6. Partition Technique
Introduction
SQL Tuning is the iterative process of improving SQL statement performance to
meet specific, measurable, and achievable goals. SQL tuning implies fixing
problems in deployed applications. In contrast, application design sets the
security and performance goals before deploying an application.
Prerequisites for SQL Tuning:
● Familiarity with database architecture
● Knowledge of SQL and PL/SQL
● Familiarity with database-provided SQL tuning tools
Introduction
Purpose of SQL Tuning:
1) Reduce user response time, which means decreasing the time between
when a user issues a statement and receives a response
2) Improve throughput, which means using the least amount of resources
necessary to process all rows accessed by a statement
SQL Processing
Syntax Check: It make sure SQL statement is right?
Semantic Check: It make sure that it is valid
statement and the statement is in the database.
Shared Pool Check: If The statement is previously
executed in SQL, SQL tries to find the similar match
and if found it will directly execute.
Optimization: If SQL does not find the similar
match it will Generate multiple execution plans.
Row Source Generation: It will decide which
execution plan is to execute.
Execution: It will execute the query and generate
the result.
SQL Processing
Soft Parse:
During the parse the database performs a stored pool check to see if
existing parse statement already exist and then determines whether it
can skip resource intensive steps of statement processing .
Hard Parse:
If oracle statement can not reuse existing code then it must be build a
new executable version of application code this is known as hard parse.
Optimizer and Execution Plan
Cost Based Optimization / Rule Based Optimization:
It is designed to determine most efficient way to carry out SQL Statement , up-to
date statical information on data being accessed.
Main Optimizer factors are: columns, data_type ,Row Count , Row Length,
Percentage of Distinct values per column, Index , Constraints Available on Table
What is Execution Plan?
It is a list of steps that Oracle will follow in order to execute SQL Statements.
each step is of one of finite number of basic operation known database server.
Optimizer and Execution Plan
Explain Plan: It enables you to view Execution Plan.
Autotrace : It generates the performance of the Query , this command provides
the statistics such as disk and memory space.
V$SQL Plan : Information about Executed SQL Statement and their Execution
plan that are still in shared poo.
Accessing Tables
Types of Table Access:
● Table Access FULL: Full table scan sequentially read each row from the
table , It reads in blocks.
● Table Access By Row ID: Oracle reads the row by their ROWID , It contain
physical location of row
● Index Unique Scan: It searches the Index for specified key
● Index Range Scan: It is used when there is NON-Unique Index or you are
searching for non rage of values.
Example:-
SELECT * FROM SALES WHERE SALES_ID BETWEEN 0 AND 1;
Simplest Tunning Rules
SELECT: Select the data only you need do not select all tha data.
Example:
Select SALES_DATE,SALES_AMOUNT,SALES_ID from Sales;
Use Table Aliases: Uset the Alias for retrieving the data.
Example:
Select s.sales_id, s.sales_amount, s.product_name, p.product_name,
p.product_code
from SALES s, Product p
where s.product_id, p.product_id
Simplest Tunning Rules
Use Where rather than Having:
– Having clause filter the the row only after its has been fetched, grouped and
sorted.
– Where clause reduces the overload of the database
Example:
Don't:
SELECT SalesOrderID, SUM(UnitPrice * OrderQty) AS TotalPrice FROM
Sales.SalesOrderDetail GROUP BY SalesOrderID HAVING SalesOrderID >
50000;
Do:
SELECT SalesOrderID, SUM(UnitPrice * OrderQty) AS TotalPrice FROM
Sales.SalesOrderDetail WHERE SalesOrderID > 50000 GROUP BY
SalesOrderID;
Index Suppression
SUBSTR Function:
Using SUBSTR function will disable index.
Example:
Don't:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE SUBSTR(PRODUCT_NAME,1,3)='SAM'
Do:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE PRODUCT_NAME LIKE 'SAM%'
Index Suppression
Following are the possible reasons why Index get suppressed.
Index can tell you what is "ON" table but not what is unavailable in table <> It will
disable index usages.
Use of <> operator in Indexed column
Example:
Don't:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID<>12345;
Do:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID>12345 AND
ORDER_ID<12345;
Index Suppression
Use of Arithmetic Operator (+,*,/,-,)
If you use following Arithmetic Operator it will suppress the Index
Example:
Don't:
SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID+1000<5000;
Do:
SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID<4000;
Index Suppression
Use of | | Operator (Concatenation Operator)
Concatenation Operator
Don't:
SELECT * FROM PRODUCT WHERE COLOR | |
PRODUCT_CATEGORY='BlackMobile'
Do:
SELECT * FROM PRODUCT WHERE COLOR ='BLACK' AND
PRODUCT_CATOGERY='MOBILE'
Index Suppression
Comparing Character to Number Value
Following example will show the comparison that will suppress the index
Example
Don't:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE PRODUCT_NAME=123;
In this example database will get confused and tries to avoid the Index
Do:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE PRODUCT_NAME='123'
In this query single quotation will be used so Index will be used.
Index Suppression
Use of IS NULL / IS NOT NULL Operator
Relational Database Ignores the NULL Values because it assumes NULL means
nothing.
Example:
SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID IS NULL;
Better Option is use following values
Update the NULL DATA With Default Values
● If it is character column , update with "x"
● If it is numeric column , update with "0"
Index Suppression
Use of function based Index:
Whenever possible try to avoid function on Indexed column, but if there is
business Requirement you can not avoid , then Instead of using function on
column try to use function based Index.
For example use following SQL query
CREATE INDEX PRODUCT_IDX ON SALES_HISTORY(SUBSTR
(PRODUCT_NAME,1,3));
Performance Improvement Consideration
Use UNION ALL Instead of UNION:
Union Performs Sort , Unique Operation its overload and time consuming
process.
Example of Union ALL
SELECT SALES_ID FROM SALES
UNION ALL
SELECT PRODUCT_AMOUNT FROM PRODUCT;
Performance Improvement Consideration
Minimize the Table Look up in the Query:
Using Sub-queries into the query it is time consuming process,
Instead you can use this
SELECT PRODUCT_ID, PRODUCT_NAME
WHERE
PRODUCT_NAME IN(SELECT PRODUCT_NAME FROM SALES);
Performance Improvement Consideration
Use of DISTINCT Keyword Instead of EXISTS:
- It will easily search the Value from the table and displays the results
SELECT DISTINCT(SALES_CATEGORY) FROM SALES;
Avoid Searching same table Multiple Times
- It slow down the database and the execution time
USE TRUNCATE Instead of DELETE
- Truncate is Faster than DELETE it does not store UNDO Information.
Example:
TRUNCATE TABLE SALES;
Consideration While Using SQL Statement
● Reduce the No. of database access Oracle needs to perform all internal
steps to display the result
● Use of Commit Command After any Operation.
For Example
INSERT INTO SALES (SALES_ID, SALES_AMOUNT,SALES_PERSON,)
VALUES (100, 5000, JOHN);
COMMIT;
Information Held in Roll back SEGMENT to UNDO the Transaction is freed up.
Locks on Tables are Released.
Consideration While Using SQL Statement
Bulk Collect:
A bulk collect is a method of fetching data where the PL/SQL engine tells the
SQL engine to collect many rows at once and place them in a collection. The
SQL engine retrieves all the rows and loads them into the collection and
switches back to the PL/SQL engine.
Consideration While Using SQL Statement
For Example
DECLARE TYPE two_cols_rt IS RECORD
(employee_id employees.employee_id%TYPE, salary employees.salary
%TYPE);
TYPE employee_info_t IS TABLE OF two_cols_rt;
l_employees employee_info_t;
BEGIN SELECT employee_id, salary
BULK COLLECT INTO l_employees
FROM employees
WHERE department_id = 10; END;
Join Method
Nested Loop Join:
The nested loops join, also called nested iteration, uses one join input as the
outer input table and one as the inner (bottom) input table. The outer loop
consumes the outer input table row by row.
SELECT C.CustomerID, c.TerritoryID FROM Sales.SalesOrderHeader oh
JOIN
Sales.Customer c ON c.CustomerID = oh.CustomerID WHERE
c.CustomerID IN (10,12) GROUP BY C.CustomerID, c.TerritoryID
Join Method
Sort Merge Join:
The sort-merge join (also known as merge join) is a join algorithm and is used in
the implementation of a relational database management system. The basic
problem of a join algorithm is to find, for each distinct value of the join attribute,
the set of tuples in each relation which display that value.
Steps to Perform Sort Merge Join
1. Check if the rows match. If they do produce output row
2. If the value in input A is smaller than the value in input B, attempt to read
next row from input A and skip next step
3. Attempt to read next row from Input B If read attempt was successful,
loop
Join Method
Hash Join:
The HASH join is similar to a NESTED LOOPS join in the sense that there is a
nested loop that occurs—Oracle first builds a hash table to facilitate the
operation and then loops through the hash table. When using an ORDERED
hint, the first table in the FROM clause is the table used to build the hash table.
SELECT OC.FirstName, OC.LastName, OH.SalesOrderID FROM
Sales.SalesOrderHeader AS OH
JOIN
Person.Contact AS OC ON OH.ContactID = OC.ContactID WHERE
OC.FirstName LIKE 'John%'
Hints The Data
Hint is an addition to the SQL standard that instructs the database engine on
how to execute the query. For example, a hint may tell the engine to use or not
to use an index (even if the query optimizer would decide otherwise).
OR
Hint is an Instruction passed to the optimizer through in SQL. hints enable you to
make decision normally made automatically by the optimizer. It is very useful for
testing the performance of specific accesses path.
Example
SELECT /*+ INDEX (s,sales_prd_idx)/ ORDER_ID,SALES_DATE,
TOTAL_AMOUNT, PRODUCT_NAME, FROM SALES_HISTORY s WHERE
CUSTOMER_ID=1000;
House Keeping
Invalid Optimizer Statistics:
We know how important statistics are for optimizer to come up with good
Execution plan. Some optimizer statistics might stale or missing.
Solution:
Check Analyzed Date ,
Ensure Table Indexes , Columns and partitions have statistics
SELECT * FROM DBA_TAB_STATISTICS;
SELECT * FROM DBA_IND_STATISTICS;
SELECT * FROM DBA_COL_STATISTICS;
House Keeping
How Do I Know DBA Performing BAD:
Oracle storage arranged into blocks of a given size. Table are indexes are made
up of series of blocks on the disk when these blocks are in the memory they
occupy buffer.
When Oracle requires a block first it check to see if it already has blocks it needs
in memory if so , the in- memory version is used. If it does not have block in
memory then it will read it from disk into memory.
So buffer get represents no.of time the oracle had to access the block.
Design Constraints
Effective Schema Design
● It Plays Very Important Role in SQL Tunning.
● Choose the data type for all Primary and Unique key carefully and remain
consistent through schema.
● Create Indexed whenever necessary if possible try to create index on
Multiple column.
● Normalize the Tables.
● Put Necessary constraints on Tables
Design Constraints
Separate Tables for Data and Index:
● It is Important to create separate table space for Tables, Indexes, Temporary
segment and rollback segment.
● Separating tables for Indexes is key to Balancing I/O and reducing
contention.
Index Organized Tables
● Index Organized Tables have their Primary Key Data and and non key
column data stored within same binary Tree. Effectively data is stored in
Primary Key Index.
● Row are Not Much Longer than their Index Key.
● Small Look Up Tables
Design Constraints
Table Partitions
Table partitioning is a way to divide a large table into smaller, more manageable
parts without having to create separate tables for each part. Data in a partitioned
table is physically stored in groups of rows called partitions and each partition
can be accessed and maintained separately. It Improves the Query
Performance.
Following are the Table partitions Methods
● Range Partitioning
● List Partitioning
● Hash Partitioning
● Composite Partitioning
Partition Technique
Range Partitioning:
Range partitioning is a partitioning technique where ranges of data is stored
separately in different sub-tables. MAXVALUE is provided as a catch-all for
values that exceed all ranges specified. Note that Oracle sorts NULLs greater
than all other values, except MAXVALUE.
Range partitioning divides the information into a number of partitions depending
on ranges of values of the particular partitioning keys for every partition of data.
It is a popular partitioning scheme which is normally used with dates. For
example, representing the days of the May month, it will have a table with the
column name as May and rows with dates from 1st of May to 31st of May.
List Partitioning
List partitioning allows to openly organize the rows, which are divided into
partitions by spelling out a roll of distinct standards for the partitioning key in an
account for every division. Using this scheme of partitioning, even dissimilar and
shuffled information tables can be managed in a comfortable approach.
In order to avoid the errors during the partition of rows in the giant database, the
addition of the probable terms into the table formed by the list partitioning
method can be avoided by using the default partition process.
Hash Partitioning
Oracle has got a hash algorithm for recognizing the partition tables. This
algorithm uniformly divides the rows into various partitions in order to make all
the partitions of identical dimensions. The process carried on by using this hash
algorithm to divide the database tables into smaller divisions is termed as the
hash partitioning.
Hash partitioning is the perfect means for sharing out data consistently among
different devices. This method of partitioning is an user-friendly partitioning
system, particularly when the information to be detached has no apparent
partitioning key.
Composite Partitioning
The Composite Partitioning method includes a minimum of two partitioning
procedures on the data. Initially, the database table will be divided by using one
partition procedure and then the output partition slices are again partitioned
further by using another partitioning procedure.
Bitmap Indexes
A Bitmap Index is a special kind of database index that uses bitmaps. Bitmap
indexes have traditionally been considered to work well for low-cardinality
columns, which have a modest number of distinct values, either absolutely, or
relative to the number of records that contain the data.
Thank You!!!

Weitere ähnliche Inhalte

Was ist angesagt?

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 

Was ist angesagt? (20)

Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Sql DML
Sql DMLSql DML
Sql DML
 
Less08 Schema
Less08 SchemaLess08 Schema
Less08 Schema
 
SQL
SQLSQL
SQL
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 

Ähnlich wie SQL Tunning

Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republic
Kaing Menglieng
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
Sidney Chen
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
maxpane
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluation
avniS
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 

Ähnlich wie SQL Tunning (20)

05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx
 
Understand when to use user defined functions in sql server tech-republic
Understand when to use user defined functions in sql server   tech-republicUnderstand when to use user defined functions in sql server   tech-republic
Understand when to use user defined functions in sql server tech-republic
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statements
 
Oracle Sql Tuning
Oracle Sql TuningOracle Sql Tuning
Oracle Sql Tuning
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Sq lite
Sq liteSq lite
Sq lite
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluation
 
Database testing
Database testingDatabase testing
Database testing
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query optimizer
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

SQL Tunning

  • 2. Agenda 1. Introduction 2. SQL Processing 3. Optimizer and Execution Plan 4. Accessing Tables 5. Performance Improvement Consideration 6. Partition Technique
  • 3. Introduction SQL Tuning is the iterative process of improving SQL statement performance to meet specific, measurable, and achievable goals. SQL tuning implies fixing problems in deployed applications. In contrast, application design sets the security and performance goals before deploying an application. Prerequisites for SQL Tuning: ● Familiarity with database architecture ● Knowledge of SQL and PL/SQL ● Familiarity with database-provided SQL tuning tools
  • 4. Introduction Purpose of SQL Tuning: 1) Reduce user response time, which means decreasing the time between when a user issues a statement and receives a response 2) Improve throughput, which means using the least amount of resources necessary to process all rows accessed by a statement
  • 5. SQL Processing Syntax Check: It make sure SQL statement is right? Semantic Check: It make sure that it is valid statement and the statement is in the database. Shared Pool Check: If The statement is previously executed in SQL, SQL tries to find the similar match and if found it will directly execute. Optimization: If SQL does not find the similar match it will Generate multiple execution plans. Row Source Generation: It will decide which execution plan is to execute. Execution: It will execute the query and generate the result.
  • 6. SQL Processing Soft Parse: During the parse the database performs a stored pool check to see if existing parse statement already exist and then determines whether it can skip resource intensive steps of statement processing . Hard Parse: If oracle statement can not reuse existing code then it must be build a new executable version of application code this is known as hard parse.
  • 7. Optimizer and Execution Plan Cost Based Optimization / Rule Based Optimization: It is designed to determine most efficient way to carry out SQL Statement , up-to date statical information on data being accessed. Main Optimizer factors are: columns, data_type ,Row Count , Row Length, Percentage of Distinct values per column, Index , Constraints Available on Table What is Execution Plan? It is a list of steps that Oracle will follow in order to execute SQL Statements. each step is of one of finite number of basic operation known database server.
  • 8. Optimizer and Execution Plan Explain Plan: It enables you to view Execution Plan. Autotrace : It generates the performance of the Query , this command provides the statistics such as disk and memory space. V$SQL Plan : Information about Executed SQL Statement and their Execution plan that are still in shared poo.
  • 9. Accessing Tables Types of Table Access: ● Table Access FULL: Full table scan sequentially read each row from the table , It reads in blocks. ● Table Access By Row ID: Oracle reads the row by their ROWID , It contain physical location of row ● Index Unique Scan: It searches the Index for specified key ● Index Range Scan: It is used when there is NON-Unique Index or you are searching for non rage of values. Example:- SELECT * FROM SALES WHERE SALES_ID BETWEEN 0 AND 1;
  • 10. Simplest Tunning Rules SELECT: Select the data only you need do not select all tha data. Example: Select SALES_DATE,SALES_AMOUNT,SALES_ID from Sales; Use Table Aliases: Uset the Alias for retrieving the data. Example: Select s.sales_id, s.sales_amount, s.product_name, p.product_name, p.product_code from SALES s, Product p where s.product_id, p.product_id
  • 11. Simplest Tunning Rules Use Where rather than Having: – Having clause filter the the row only after its has been fetched, grouped and sorted. – Where clause reduces the overload of the database Example: Don't: SELECT SalesOrderID, SUM(UnitPrice * OrderQty) AS TotalPrice FROM Sales.SalesOrderDetail GROUP BY SalesOrderID HAVING SalesOrderID > 50000; Do: SELECT SalesOrderID, SUM(UnitPrice * OrderQty) AS TotalPrice FROM Sales.SalesOrderDetail WHERE SalesOrderID > 50000 GROUP BY SalesOrderID;
  • 12. Index Suppression SUBSTR Function: Using SUBSTR function will disable index. Example: Don't: SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE SUBSTR(PRODUCT_NAME,1,3)='SAM' Do: SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE PRODUCT_NAME LIKE 'SAM%'
  • 13. Index Suppression Following are the possible reasons why Index get suppressed. Index can tell you what is "ON" table but not what is unavailable in table <> It will disable index usages. Use of <> operator in Indexed column Example: Don't: SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE ORDER_ID<>12345; Do: SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE ORDER_ID>12345 AND ORDER_ID<12345;
  • 14. Index Suppression Use of Arithmetic Operator (+,*,/,-,) If you use following Arithmetic Operator it will suppress the Index Example: Don't: SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE ORDER_ID+1000<5000; Do: SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE ORDER_ID<4000;
  • 15. Index Suppression Use of | | Operator (Concatenation Operator) Concatenation Operator Don't: SELECT * FROM PRODUCT WHERE COLOR | | PRODUCT_CATEGORY='BlackMobile' Do: SELECT * FROM PRODUCT WHERE COLOR ='BLACK' AND PRODUCT_CATOGERY='MOBILE'
  • 16. Index Suppression Comparing Character to Number Value Following example will show the comparison that will suppress the index Example Don't: SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE PRODUCT_NAME=123; In this example database will get confused and tries to avoid the Index Do: SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE PRODUCT_NAME='123' In this query single quotation will be used so Index will be used.
  • 17. Index Suppression Use of IS NULL / IS NOT NULL Operator Relational Database Ignores the NULL Values because it assumes NULL means nothing. Example: SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME FROM SALES_HISTORY WHERE ORDER_ID IS NULL; Better Option is use following values Update the NULL DATA With Default Values ● If it is character column , update with "x" ● If it is numeric column , update with "0"
  • 18. Index Suppression Use of function based Index: Whenever possible try to avoid function on Indexed column, but if there is business Requirement you can not avoid , then Instead of using function on column try to use function based Index. For example use following SQL query CREATE INDEX PRODUCT_IDX ON SALES_HISTORY(SUBSTR (PRODUCT_NAME,1,3));
  • 19. Performance Improvement Consideration Use UNION ALL Instead of UNION: Union Performs Sort , Unique Operation its overload and time consuming process. Example of Union ALL SELECT SALES_ID FROM SALES UNION ALL SELECT PRODUCT_AMOUNT FROM PRODUCT;
  • 20. Performance Improvement Consideration Minimize the Table Look up in the Query: Using Sub-queries into the query it is time consuming process, Instead you can use this SELECT PRODUCT_ID, PRODUCT_NAME WHERE PRODUCT_NAME IN(SELECT PRODUCT_NAME FROM SALES);
  • 21. Performance Improvement Consideration Use of DISTINCT Keyword Instead of EXISTS: - It will easily search the Value from the table and displays the results SELECT DISTINCT(SALES_CATEGORY) FROM SALES; Avoid Searching same table Multiple Times - It slow down the database and the execution time USE TRUNCATE Instead of DELETE - Truncate is Faster than DELETE it does not store UNDO Information. Example: TRUNCATE TABLE SALES;
  • 22. Consideration While Using SQL Statement ● Reduce the No. of database access Oracle needs to perform all internal steps to display the result ● Use of Commit Command After any Operation. For Example INSERT INTO SALES (SALES_ID, SALES_AMOUNT,SALES_PERSON,) VALUES (100, 5000, JOHN); COMMIT; Information Held in Roll back SEGMENT to UNDO the Transaction is freed up. Locks on Tables are Released.
  • 23. Consideration While Using SQL Statement Bulk Collect: A bulk collect is a method of fetching data where the PL/SQL engine tells the SQL engine to collect many rows at once and place them in a collection. The SQL engine retrieves all the rows and loads them into the collection and switches back to the PL/SQL engine.
  • 24. Consideration While Using SQL Statement For Example DECLARE TYPE two_cols_rt IS RECORD (employee_id employees.employee_id%TYPE, salary employees.salary %TYPE); TYPE employee_info_t IS TABLE OF two_cols_rt; l_employees employee_info_t; BEGIN SELECT employee_id, salary BULK COLLECT INTO l_employees FROM employees WHERE department_id = 10; END;
  • 25. Join Method Nested Loop Join: The nested loops join, also called nested iteration, uses one join input as the outer input table and one as the inner (bottom) input table. The outer loop consumes the outer input table row by row. SELECT C.CustomerID, c.TerritoryID FROM Sales.SalesOrderHeader oh JOIN Sales.Customer c ON c.CustomerID = oh.CustomerID WHERE c.CustomerID IN (10,12) GROUP BY C.CustomerID, c.TerritoryID
  • 26. Join Method Sort Merge Join: The sort-merge join (also known as merge join) is a join algorithm and is used in the implementation of a relational database management system. The basic problem of a join algorithm is to find, for each distinct value of the join attribute, the set of tuples in each relation which display that value. Steps to Perform Sort Merge Join 1. Check if the rows match. If they do produce output row 2. If the value in input A is smaller than the value in input B, attempt to read next row from input A and skip next step 3. Attempt to read next row from Input B If read attempt was successful, loop
  • 27. Join Method Hash Join: The HASH join is similar to a NESTED LOOPS join in the sense that there is a nested loop that occurs—Oracle first builds a hash table to facilitate the operation and then loops through the hash table. When using an ORDERED hint, the first table in the FROM clause is the table used to build the hash table. SELECT OC.FirstName, OC.LastName, OH.SalesOrderID FROM Sales.SalesOrderHeader AS OH JOIN Person.Contact AS OC ON OH.ContactID = OC.ContactID WHERE OC.FirstName LIKE 'John%'
  • 28. Hints The Data Hint is an addition to the SQL standard that instructs the database engine on how to execute the query. For example, a hint may tell the engine to use or not to use an index (even if the query optimizer would decide otherwise). OR Hint is an Instruction passed to the optimizer through in SQL. hints enable you to make decision normally made automatically by the optimizer. It is very useful for testing the performance of specific accesses path. Example SELECT /*+ INDEX (s,sales_prd_idx)/ ORDER_ID,SALES_DATE, TOTAL_AMOUNT, PRODUCT_NAME, FROM SALES_HISTORY s WHERE CUSTOMER_ID=1000;
  • 29. House Keeping Invalid Optimizer Statistics: We know how important statistics are for optimizer to come up with good Execution plan. Some optimizer statistics might stale or missing. Solution: Check Analyzed Date , Ensure Table Indexes , Columns and partitions have statistics SELECT * FROM DBA_TAB_STATISTICS; SELECT * FROM DBA_IND_STATISTICS; SELECT * FROM DBA_COL_STATISTICS;
  • 30. House Keeping How Do I Know DBA Performing BAD: Oracle storage arranged into blocks of a given size. Table are indexes are made up of series of blocks on the disk when these blocks are in the memory they occupy buffer. When Oracle requires a block first it check to see if it already has blocks it needs in memory if so , the in- memory version is used. If it does not have block in memory then it will read it from disk into memory. So buffer get represents no.of time the oracle had to access the block.
  • 31. Design Constraints Effective Schema Design ● It Plays Very Important Role in SQL Tunning. ● Choose the data type for all Primary and Unique key carefully and remain consistent through schema. ● Create Indexed whenever necessary if possible try to create index on Multiple column. ● Normalize the Tables. ● Put Necessary constraints on Tables
  • 32. Design Constraints Separate Tables for Data and Index: ● It is Important to create separate table space for Tables, Indexes, Temporary segment and rollback segment. ● Separating tables for Indexes is key to Balancing I/O and reducing contention. Index Organized Tables ● Index Organized Tables have their Primary Key Data and and non key column data stored within same binary Tree. Effectively data is stored in Primary Key Index. ● Row are Not Much Longer than their Index Key. ● Small Look Up Tables
  • 33. Design Constraints Table Partitions Table partitioning is a way to divide a large table into smaller, more manageable parts without having to create separate tables for each part. Data in a partitioned table is physically stored in groups of rows called partitions and each partition can be accessed and maintained separately. It Improves the Query Performance. Following are the Table partitions Methods ● Range Partitioning ● List Partitioning ● Hash Partitioning ● Composite Partitioning
  • 35. Range Partitioning: Range partitioning is a partitioning technique where ranges of data is stored separately in different sub-tables. MAXVALUE is provided as a catch-all for values that exceed all ranges specified. Note that Oracle sorts NULLs greater than all other values, except MAXVALUE. Range partitioning divides the information into a number of partitions depending on ranges of values of the particular partitioning keys for every partition of data. It is a popular partitioning scheme which is normally used with dates. For example, representing the days of the May month, it will have a table with the column name as May and rows with dates from 1st of May to 31st of May.
  • 36. List Partitioning List partitioning allows to openly organize the rows, which are divided into partitions by spelling out a roll of distinct standards for the partitioning key in an account for every division. Using this scheme of partitioning, even dissimilar and shuffled information tables can be managed in a comfortable approach. In order to avoid the errors during the partition of rows in the giant database, the addition of the probable terms into the table formed by the list partitioning method can be avoided by using the default partition process.
  • 37. Hash Partitioning Oracle has got a hash algorithm for recognizing the partition tables. This algorithm uniformly divides the rows into various partitions in order to make all the partitions of identical dimensions. The process carried on by using this hash algorithm to divide the database tables into smaller divisions is termed as the hash partitioning. Hash partitioning is the perfect means for sharing out data consistently among different devices. This method of partitioning is an user-friendly partitioning system, particularly when the information to be detached has no apparent partitioning key.
  • 38. Composite Partitioning The Composite Partitioning method includes a minimum of two partitioning procedures on the data. Initially, the database table will be divided by using one partition procedure and then the output partition slices are again partitioned further by using another partitioning procedure.
  • 39. Bitmap Indexes A Bitmap Index is a special kind of database index that uses bitmaps. Bitmap indexes have traditionally been considered to work well for low-cardinality columns, which have a modest number of distinct values, either absolutely, or relative to the number of records that contain the data.