SQL Tips to Improve
Query Performance
Vikas Rajput
@vikasrajputin
1. Use Limited Joins
➔ Make a habit of using as less join
as possible, using more joins
degrade the query performance
➔ Use only when it's absolutely
required
@vikasrajputin
2. Indexing and Primary Key
➔ Make sure to create indexes for
the columns being frequently
searched
➔ Make sure to put the primary key
in the table, it also helps in
boosting the performance
@vikasrajputin
3. Use Query Plan
➔ Query plan/optimization report
breaks the query processing into
multiple parts and tells you how
much time each part is taking
➔ Helps a lot in identifying the issue,
and you can take necessary actions
accordingly
Note: Query Plan/Optimization Report’s Name might
depends on DB to DB.
@vikasrajputin
4. Restrict Columns
➔ Avoid fetching all columns or any
unused columns, focus on the
columns you need to accomplish
the given task
➔ Don't try to reuse same query
everywhere in which you're using
unused columns
➔ Create another method, and write a
separate query
@vikasrajputin
5. Avoid Wildcards
➔ The wildcard-based search slows
down the query performance
➔ It scans all the records to find
correct results
➔ Avoid them if the same job can be
done with an alternate approach
@vikasrajputin
6. Avoid Subqueries
➔ Too much subquery can degrade
the performance very badly
➔ They get executed for each row
fetched so it is advisable to limit
their use
@vikasrajputin
7. Try EXISTS over Joins
➔ Sometimes, to check if a record exist
or not, we use join with the main
table
➔ If you are not retrieving the columns
of the other table which you've
joined, then try to use EXISTS
instead of Join
➔ EXISTS is blazing fast!
Note: Not in all cases Exists is fast, it depends on your DB
server, Query execution plan used by your server so first
try if EXISTS is performing better in place of Join, if yes
then only use it.
@vikasrajputin
8. Prefer Primary Key
➔ Primary keys are optimised for
searching, whenever possible
always use primary key in where
clause Instead of using any non
primary key column
@vikasrajputin
❤ Thanks for reading !
For more content on
Java & Backend Development,
follow me on below handles
Vikas Rajput
@vikasrajputin