SQL Tuning or SQL Optimization

By | August 12, 2013

Sql Statements are used to retrieve data from the database. We can get same results by writing different sql queries. But use of the best query is important when performance is considered. So you need to sql query tuning based on the requirement. Here is the list of queries which we use regularly and how these sql queries can be optimized for better performance.

SQL Tuning/SQL Optimization Techniques:

Try to avoid ‘SELECT * FROM…’

The sql query becomes faster if you use the actual columns names in SELECT statement instead of than ‘*’.

For Example: Write the query as

SELECT id, first_name, last_name, age, subject FROM student_details;

Instead of:

SELECT * FROM student_details;

Avoid using ‘HAVING’ as an alternative to ‘WHERE’

HAVING clause is used to filter the rows after all the rows are selected. It is just like a filter. Do not use HAVING as an alternative to WHERE clause.
For Example: Write the query as

SELECT subject, COUNT(subject)
FROM student_details
WHERE subject != 'Science'
AND subject != 'Maths'
GROUP BY subject;

Instead of:

SELECT subject, COUNT(subject)
FROM student_details
GROUP BY subject
HAVING subject!= 'Vancouver' AND subject!= 'Toronto';

Its not that we have to avoid ‘HAVING’ clause completely, there are some cases where we can’t use ‘WHERE’ clause instead of ‘HAVING’, for example, if ‘HAVING’ clause is applied in aggregate functions then it can’t be replaced by ‘WHERE’ clause.

Try to use less number of sub-queries in your code

Sometimes you may have more than one subqueries in your main query. Try to minimize the number of subquery block in your query.
For Example: Write the query as

SELECT name
FROM employee
WHERE (salary, age ) = (SELECT MAX (salary), MAX (age)
FROM employee_details)
AND dept = 'Electronics';

Instead of:

SELECT name
FROM employee
WHERE salary = (SELECT MAX(salary) FROM employee_details)
AND age = (SELECT MAX(age) FROM employee_details)
AND emp_dept = 'Electronics';

Use operators EXISTS and IN appropriately

a) Usually IN has the slowest performance.
b) IN is efficient when most of the filter criteria is in the sub-query.
c) EXISTS is efficient when most of the filter criteria is in the main query.

For Example: Write the query as

SELECT * FROM product p
WHERE EXISTS (SELECT * FROM order_items o
WHERE o.product_id = p.product_id);

Instead of:

SELECT * FROM product p
WHERE product_id IN
(SELECT product_id FROM order_items o
WHERE o.product_id = p.product_id);

Use EXISTS instead of DISTINCT

Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship.
For Example: Write the query as

SELECT d.dept_id, d.dept
FROM dept d
WHERE EXISTS ( SELECT 'X' FROM employee e WHERE e.dept = d.dept);

Instead of:

SELECT DISTINCT d.dept_id, d.dept
FROM dept d,employee e
WHERE e.dept = e.dept;

use UNION ALL instead of UNION

Try to use UNION ALL in place of UNION.
For Example: Write the query as

SELECT id, first_name
FROM student_details_class10
UNION ALL
SELECT id, first_name
FROM sports_team;

Instead of:

SELECT id, first_name, subject
FROM student_details_class10
UNION
SELECT id, first_name
FROM sports_team;

Use WHERE clause properly

Be careful while using conditions in WHERE clause.
For Example: Write the query as

SELECT id, first_name, age FROM student_details WHERE age > 10;

Instead of:

SELECT id, first_name, age FROM student_details WHERE age != 10;

Write the query as

SELECT id, first_name, age
FROM student_details
WHERE first_name LIKE 'Chan%';

Instead of:

SELECT id, first_name, age
FROM student_details
WHERE SUBSTR(first_name,1,3) = 'Cha';

Write the query as

SELECT id, first_name, age
FROM student_details
WHERE first_name LIKE NVL ( :name, '%');

Instead of:

SELECT id, first_name, age
FROM student_details
WHERE first_name = NVL ( :name, first_name);

Write the query as

SELECT product_id, product_name
FROM product
WHERE unit_price BETWEEN MAX(unit_price) AND MIN(unit_price);

Instead of:

SELECT product_id, product_name
FROM product
WHERE unit_price >= MAX(unit_price)
AND unit_price <= MIN(unit_price);

Write the query as

SELECT id, name, salary
FROM employee
WHERE dept = 'Electronics'
AND location = 'Bangalore';

Instead of:

SELECT id, name, salary
FROM employee
WHERE dept || location= 'ElectronicsBangalore';

Use non-column expression on one side of the query because it will be processed earlier.

Write the query as

SELECT id, name, salary
FROM employee
WHERE salary < 25000;

Instead of:

SELECT id, name, salary
FROM employee
WHERE salary + 10000 < 35000;

Write the query as

SELECT id, first_name, age
FROM student_details
WHERE age > 10;

Instead of:

SELECT id, first_name, age
FROM student_details
WHERE age NOT = 10;

Make use of available Operators

If operators are available for your desired task, use it instead of writing it on your own.
For example:
Write the query as

SELECT product_id, product_name
FROM product
WHERE unit_price BETWEEN MAX(unit_price) AND MIN(unit_price);

Instead of:

SELECT product_id, product_name
FROM product
WHERE unit_price >= MAX(unit_price)
AND unit_price <= MIN(unit_price);

Please note: BETWEEN operates behaves differently in different databases. For some databases BETWEEN operator will return all the value between lower and upper limit, for some databases it will return values including the upper and lower limit, for some others it will return values including either one of the limits.

Follow standard SQL rules

To write queries which provide efficient performance follow the general SQL standard rules.

a) Use single case for all SQL verbs
b) Begin all SQL verbs on a new line
c) Separate all words with a single space
d) Right or left aligning verbs within the initial SQL verb

WOW! Did you like this post? We'll send more interesting posts like SQL Tuning or SQL Optimization to you!
Enter your Email Address: