Top 10 SQL Tuning tips to Boost SQL Query Performance

SQL Tuning Tips

SQL statements are basically used for manipulation of data or data retrieval. When we consider the large software, industrial software it has very high volume of data. So there is huge need of tuning the query which will improves the performance of data and application. There are lot of factors we need to consider in Performance tuning like the Server hardware, Speed of Server, Database using, Volume of data etc. In this article I am giving you the basic idea of SQL  Tuning Tips in 2022. We will get same result using multiple type of queries. But by considering the cost and execution of the query we will able to tune our sql and improve the performance. We will see How to improve SQL Query Performance? in this article. In this article  we will give you SQL Tuning tips in detailed level.

CLICK HERE TO GET INFORMATION ON INDEXING OF SQL..

SQL Performance Tuning Tips is the process of ensuring that the SQL statements  will run in the fastest possible time.

  • How to do SQL Tuning ? 

Following are some basic tips using which you will able to optimize your query and improve SQL Performance Tuning Techniques .The first step to check is cost of your query. For checking the cost of query use following simple command. I will explain the ‘Explain’ statement in detail in my other advanced tuning articles. As of now just refer following statement to find out the cost of query. More the cost less the performance of query.

Explain  [Query]

Explain Select * from Employee;

How to improve SQL Query Performance? – SQL Tuning Tips and Examples

Check 1 : Need to Avoid Multiple number of Joins

1. There is common practice  where we are using the unnecessary multiple joins.
2.If you see the explain plan the joins cost is very high.
3.We require to check all the joins and remove the unnecessary joins by restructuring query.
 

Check 2 : Cursors Illumination

1.We require to revisit the outputs of multiple queries and  need to remove the unnecessary cursors.

2.Dynamic cursors limits the optimizer to use nested loop joins.

3.We require to avoid cursors and use set-based queries instead of cursors.

Check 3 : Avoid non-correlated sub queries

1. There are so many times due to bad SQL techniques the people are using the non correlated sub-queries.
 
2.We require to use correlated sub queries.

Check 4 : Avoid the costly functions and statements

1.We require to check the system functions and its cost .

2.We require to avoid distinct or count like functions wherever possible. These functions are costly and will reduce the cost.

Check 5 : Need to create correct index on correct places

1.We require to create the indexes on correct places.

2.We require to reduce the data retrieval time for queries.

3.We require to create highly selective indexes by checking and analyzing data.

Check 6 :  Check the data volume

1.We require to check the data volume of statement.

2.We need to review the data volume and apply the partitioning techniques accordingly.

check 7 : Drop unused indexes

1. We require to check the low performing indexes and unused indexes.

2.We need to recreate the good indexes and remove the unused indexes.

check 8 : Create Stats with update stats :

1.There are so many times when we forget to gather stats of tables,indexes or schemas.

2.We need to gather stats and set some jobs to gather stats on regular basis.

Check 9 :  Need to check the constraints

1.There are so many times when the performance will decrease because of the constraints

2.We need too revisit the design and check the primary key, foreign key and not null and check constraints.

Some tips to improve the SQL Performance :

Tip 1:   Never use *(Star) to fetch all records from table

Sql query become fast if you use actual columns instead of * to fetch all the records from the table.

Not Recommended:

Select * from Employee;

Recommended:

Select Eno,Ename,Address from Employee;

  • Tip 2: Try to avoid distinct keyword from the query

Try to avoid distinct keyword from select statements. Distinct keyword has high cost and low performance. When anyone uses distinct keyword it first sorts the data from column and then fetches the distinct values. Use exist operator inspite of distinct keyword.

Not Recommended:

SELECT DISTINCT d.dept_no, d.department_name
FROM Department d,Employee e
WHERE d.dept_no= e.dept_no;

Recommended:

SELECT d.dept_no d.department_name
FROM Department d
WHERE EXISTS ( SELECT ‘X’ FROM Employee e WHERE d.dept_no= e.dept_no);

CLICK HERE TO GET INTERVIEW QUESTIONS ON PERFORMANCE TUNING..

  • Tip 3:Carefully use where conditions in sql

Try to use correct operator as per requirement given.

Not Recommended:

Select * from Employee where salary != 65000;

Recommended:

Select * from Employee where salary > 65000 and salary < 65000;

  • Tip 4: Use Like operator in spite of equal to (=)

Not Recommended:

Select * from Employee where name=’Amit’;

Recommended:

Select * from Employee where name like ‘Amit%’;

  • Tip 5: Avoid having clause/group by statements

Having clause and group by statements have high cost.So try to avoid it in sql query.

Not Recommended:

Select * from Employee where name=’Amit’  group by department having salary=45000;

Recommended:

Select * from Employee where name=’Amit’ and salary=45000;

Tip 6: Use of Exist and In Operators

Basically Operator IN has lowest performance.IN operator is used when Filter criteria is in subquery whereas Exist operator is used when filter criteria is in main query.

Example:

In Operator:

Select * from Employee where Department_name In(Select Department_name from Department where Dno=10);

Exist operator:

Select * from Employee where Exist(Select Department_name from Department where Dno=10);

Try to use union all inspite of union.As union scans all data first and then eliminate duplicate so it has slow performance.

Not Recommended:

Select * from Employee where dept_no=10

Union

Select * from Employee where dept_no=20;

Recommended:

Select * from Employee where dept_no=10

Union  All

Select * from Employee where dept_no=20;

  • Tip 8: Avoid use of Functions in Where condition.

Not Recommended:

Select * from Employee where Substr(name,1,3)=’Ami’;

Recommended:

Select * from Employee where name like ‘Ami%’;

CLICK HERE TO GET INFORMATION ON SQL OPTIMIZER AND PARSER..

I have explained the Basic tips which is used to improve and optimize performance of SQL Queries. Hope this article is helpful to you to improve the performance of your sql queries.

HOME

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

1 week ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

2 weeks ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

2 weeks ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

2 weeks ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

2 weeks ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

2 weeks ago