You are on page 1of 5

5/1/23, 10:42 AM 6 SQL Query Optimisation Techniques!!

| by Abdelilah MOULIDA | Medium

Sign in to Medium with Google

deepesh kumar
deepeshkumarpal@gmail.com
You have 2 free member-only stories left this month.
Sign up for Medium and get an extra one Continue as deepesh

To create your account, Google will share your name,


email address and profile picture with Medium. See
Abdelilah MOULIDA Follow Medium's privacy policy and Terms of Service.
Jan 26 · 3 min read · · Listen

Save

6 SQL Query Optimisation Techniques!!


This blog provides query optimisation techniques for designing good logic and
extracting maximum performance from the database. Query logic impacts database
server, application server, network, IO and end-user experience.

Query optimisation has many benefits, some of which are listed below.

https://medium.com/@abdelilah.moulida/6-sql-query-optimisation-techniques-60eda6d0fcd2 1/5
5/1/23, 10:42 AM 6 SQL Query Optimisation Techniques!! | by Abdelilah MOULIDA | Medium

Minimize production issues: Every slow query requires high CPU, memory, and
IOPS. We have seen most production database issues are caused by non-
optimised queries.

Performance issues: Slow query means slower response time of applications


using the query, which results in poor end-user experience. It is very important
to test logic/query with sufficient data before running it in production.

Save infra cost: Unoptimised query requires more CPU, IOPS and memory.
Additional load can be put on the same server if queries are optimised.

Use Explain Plan for Query analysing


Explain Plan is a great utility of MySQL to check query behaviour. It works for select,
insert, update and delete query. There are lot of insights we can get from Explain
Plan. Below are few important points.

1. Check index is available or not

2. Index is used or not.

3. How many rows are getting scanned for providing results.

4. Determine ordering operation using filesort or else.

5. Length of an index.

6. Determine access type.

7. Use SHOW WARNINGS after explain give some additional information.

Query optimisation techniques and Common mistakes

Avoid Using Different Character Encoding


It requires implicit conversion while making join on tables with different charset.

Most of time optimiser does on perform proper implicit conversion of charset and
not utilise index.

We should use the same encoding across tables to utilise the better indexing feature
of the DB.

https://medium.com/@abdelilah.moulida/6-sql-query-optimisation-techniques-60eda6d0fcd2 2/5
5/1/23, 10:42 AM 6 SQL Query Optimisation Techniques!! | by Abdelilah MOULIDA | Medium

Understanding of column datatype comparing with values:


In queries, one should avoid comparing different datatype column and value. When
a query compares different datatypes, it uses implicit datatype conversion.

For example in the query below, mobile_no is a BIGINT datatype but query
comparing as CHAR value.

select customer_name from CUSTOMER where mobile_no='9876543210'

For better utilisation of indexes, comparison should be done as the same data type
as an integer to integer or varchar to varchar .

Avoid using Function-based clause in where condition:


When a query uses a function in the where or join clause on the column, it would not
utilise the index.

where date(mail_sent) ='2018–01–29'


where lower(user_name)='abc'
where concat(first_name,' ',last_name)='jon bon'
inner join lower(first_name)=lower(user_name)
date(sent_time)=date(now)

One way to optimise query below

select count(email_sent)
from EMAIL_TABLE
where date(sent_time)=date(now())

is by changing date(mail_sent)=date(now) as following

select count(email_sent)
from EMAIL_TABLE
where sent_time between
https://medium.com/@abdelilah.moulida/6-sql-query-optimisation-techniques-60eda6d0fcd2 3/5
5/1/23, 10:42 AM 6 SQL Query Optimisation Techniques!! | by Abdelilah MOULIDA | Medium

str_to_date(concat(current_date,' 00:00:00'),'%Y-%m-%d %H:%i:%s')


and
str_to_date(concat(current_date,' 23:59:59'),'%Y-%m-%d %H:%i:%s'))

The function-based index is available only certain database version like MySQL 8.
But if this kind of use of the function is unavoidable in MySQL 5.7 we can create a
computed column and create an index on that.

Avoid using DISTINCT and GROUP BY at the same time:


The query below has performance overhead when it uses Distinct and Group By
together, GROUP BY itself makes rows distinct. Using GROUP BY as in the below
query also has one drawback, it will give random values for those
columns(b.customer_name) that do not have a group function.

select distinct a.user_name,b.customer_name,count(a.id)


from USER a inner join CUSTOMER b
on a.user_name = b.customer_name
group by a.user_name

There is no need to use DISTINCT and GROUP BY together. This query could be re-
written as follows

select a.user_name,group_concat(b.customer_name),count(a.id)
from USER a inner join CUSTOMER b
on a.user_name = b.customer_name
group by a.user_name

Avoid using UNION and DISTINCT at the same time:


Union itself makes distinct records, so we need not use DISTINCT with UNION as
shown in the query below.

select distinct user_name from USER


UNION

https://medium.com/@abdelilah.moulida/6-sql-query-optimisation-techniques-60eda6d0fcd2 4/5
5/1/23, 10:42 AM 6 SQL Query Optimisation Techniques!! | by Abdelilah MOULIDA | Medium

select distinct customer_name from CUSTOMER

Avoid selecting unnecessary columns:


Selecting unnecessary columns would be a waste of memory, CPU cycle and
network. Every query should select only the required columns for better query
performance.

As example query below is select all records

select * from CUSTOMER where id = 123;

it is better to select column name instead of * or extra columns.


Open in app Sign up Sign In

select customer_name from CUSTOMER where id = 123;

Sql Database Optimization User Experience

About Help Terms Privacy

Get the Medium app

https://medium.com/@abdelilah.moulida/6-sql-query-optimisation-techniques-60eda6d0fcd2 5/5

You might also like