0% found this document useful (0 votes)
43 views3 pages

SQL Opt 1668669156631

Jean Habimana's paper discusses various query optimization techniques that can be used to improve the performance of SQL queries. The paper covers general tips for query optimization including using column names instead of "*" and avoiding unnecessary DISTINCT conditions. Specific techniques are described such as eliminating subqueries by rewriting them as joins, using IN predicates when querying indexed columns, and avoiding HAVING clauses in SELECT statements. Examples are provided to demonstrate how these optimization strategies can reduce query execution times by over 60% in some cases. The goal of the paper is to provide simple and practical tips that can be applied to immediately boost query performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

SQL Opt 1668669156631

Jean Habimana's paper discusses various query optimization techniques that can be used to improve the performance of SQL queries. The paper covers general tips for query optimization including using column names instead of "*" and avoiding unnecessary DISTINCT conditions. Specific techniques are described such as eliminating subqueries by rewriting them as joins, using IN predicates when querying indexed columns, and avoiding HAVING clauses in SELECT statements. Examples are provided to demonstrate how these optimization strategies can reduce query execution times by over 60% in some cases. The goal of the paper is to provide simple and practical tips that can be applied to immediately boost query performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

INTERNATIONAL JOURNAL OF SCIENTIFIC & TECHNOLOGY RESEARCH VOLUME 4, ISSUE 10, OCTOBER 2015 ISSN 2277-8616

Query Optimization Techniques - Tips For


Writing Efficient And Faster SQL Queries
Jean HABIMANA

Abstract: SQL statements can be used to retrieve data from any database. If you've worked with databases for any amount of time retrieving
information, it's practically given that you've run into slow running queries. Sometimes the reason for the slow response time is due to the load on the
system, and other times it is because the query is not written to perform as efficiently as possible which is the much more common reason. For better
performance we need to use best, faster and efficient queries. This paper covers how these SQL queries can be optimized for b etter performance.
Query optimization subject is very wide but we will try to cover the most important points. In this paper I am not focusing on, in- depth analysis of
database but simple query tuning tips & tricks which can be applied to gain immediate performance gain.
————————————————————

I. INTRODUCTION
Query optimization is an important skill for SQL
developers and database administrators (DBAs). In
order to improve the performance of SQL queries,
developers and DBAs need to understand the query
optimizer and the techniques it uses to select an access
path and prepare a query execution plan. Query tuning
involves knowledge of techniques such as cost-based and
heuristic-based optimizers, plus the tools an SQL platform
provides for explaining a query execution plan.The best
way to tune performance is to try to write your queries in a
number of different ways and compare their reads and
execution plans. In this paper I proposed various
techniques that you can use to try to optimize your
database queries.
Tip #2:
II. GENERAL TIPS FOR QUERY Avoid including a HAVING clause in SELECT
OPTIMIZATION statements
The HAVING clause is used to filter the rows after all the
Each tip was tested by running both the original query and
rows are selected and it is used like a filter. It is quite
improved query while retrieving information from the Oracle
useless in a SELECT statement. It works by going through
11g sample database especially on Sales schema. I
the final result table of the query parsing out the rows that
recorded the average time of each query to show the speed
don’t meet the HAVING condition.
increase of using the more efficient query.
Example:
Tip #1:
Original query:
Use Column Names Instead of * in a SELECT SELECT s.cust_id,count(s.cust_id)
Statement FROM SH.sales s
If you are selecting only a few columns from a table there is GROUP BY s.cust_id
no need to use SELECT *. Though this is easier to write, it HAVING s.cust_id != '1660' AND s.cust_id != '2';
will cost more time for the database to complete the query. Improved query:
By selecting only the columns you need, you are reducing SELECT s.cust_id,count(cust_id)
the size of the result table, reducing the network traffic and FROM SH.sales s
also in turn boosting the overall performance of the query. WHERE s.cust_id != '1660'
AND s.cust_id !='2'
Example: GROUP BY s.cust_id;
Original query:
SELECT * FROM SH.Sales;
Improved query:
SELECT s.prod_id FROM SH.sales s;

1
IJSTR©20
15
INTERNATIONAL JOURNAL OF SCIENTIFIC & TECHNOLOGY RESEARCH VOLUME 4, ISSUE 10, OCTOBER 2015 ISSN 2277-8616

Example:
Original query:
SELECT *
FROM SH.products p
WHERE p.prod_id =
(SELECT s.prod_id
FROM SH.sales s
WHERE s.cust_id = 100996
AND s.quantity_sold = 1 );
Improved query:
SELECT p.*
FROM SH.products p, sales s
WHERE p.prod_id =
s.prod_id AND s.cust_id =
100996
AND s.quantity_sold = 1;
Tip #3:
Eliminate Unnecessary DISTINCT Conditions
Considering the case of the following example, the 61% Time Reduction
DISTINCT keyword in the original query is unnecessary
because the table_name contains the primary key p.ID,
which is part of the result set.

Example:
Original query:
SELECT DISTINCT * FROM SH.sales s
JOIN SH.customers c
ON s.cust_id= c.cust_id
WHERE c.cust_marital_status = 'single';
Improved query: Tip #5:
SELECT * FROM SH.sales s JOIN Consider using an IN predicate when querying
SH.customers c an indexed column
ON s.cust_id = c.cust_id The IN-list predicate can be exploited for indexed retrieval
WHERE c.cust_marital_status='single'; and also, the optimizer can sort the IN-list to match the sort
sequence of the index, leading to more efficient retrieval.
Note that the IN-list must contain only constants, or values
that are constant during one execution of the query block,
such as outer references.

Example:
Original query:
SELECT s.*
FROM SH.sales s
WHERE s.prod_id = 14
OR s.prod_id = 17;
Improved query:
SELECT s.*
FROM SH.sales s
WHERE s.prod_id IN (14, 17);

Tip #4:
Un-nest sub queries
Rewriting nested queries as joins often leads to more
efficient execution and more effective optimization. In
general, sub-query un-nesting is always done for correlated
sub-queries with, at most, one table in the FROM clause,
which are used in ANY, ALL, and EXISTS predicates. A
uncorrelated sub-query, or a sub-query with more than one
table in the FROM clause, is flattened if it can be decided,
based on the query semantics, that the sub-query returns at
most one row.

2
IJSTR©20
15
Only two pages were converted.
Please Sign Up to convert the full document.

www.freepdfconvert.com/membership

You might also like