Professional Documents
Culture Documents
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
Save
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.
Save infra cost: Unoptimised query requires more CPU, IOPS and memory.
Additional load can be put on the same server if queries are optimised.
5. Length of an index.
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
For example in the query below, mobile_no is a BIGINT datatype but query
comparing as CHAR value.
For better utilisation of indexes, comparison should be done as the same data type
as an integer to integer or varchar to varchar .
select count(email_sent)
from EMAIL_TABLE
where date(sent_time)=date(now())
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
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.
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
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
https://medium.com/@abdelilah.moulida/6-sql-query-optimisation-techniques-60eda6d0fcd2 5/5