SQL-OPTIMIZATION
SQL-OPTIMIZATION
Theegala
SQL OPTIMIZATION
CAN BE HELPFUL!
LEARN HOW
Varun Sagar
Theegala
WHAT IT IS ?
Using indexes to improve the database
query performance.
WHY IT MATTERS ?
Indexes speed up the retrieval of rows from
a table, making queries more efficient.
HOW TO OPTIMIZE ?
Identify frequently queried columns and
create indexes on those columns.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
A
Varun Sagar
Theegala
#2
Avoiding SELECT *
WHAT IT IS ?
Specifying needed columns instead of using
SELECT *
WHY IT MATTERS ?
Increases query efficiency by reducing data
load and processing time.
HOW TO OPTIMIZE ?
Analyze the data requirements and explicitly
list only the necessary columns in your
SELECT statement.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#3
Use Joins Instead
of Sub-queries
WHAT IT IS ?
Replacing sub-queries with joins to enhance
performance.
WHY IT MATTERS ?
Joins are generally more efficient and faster
than nested sub-queries.
HOW TO OPTIMIZE ?
Convert sub-queries into JOIN operations
where possible to ensure the most efficient
type of JOIN for your data.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#4
Proper Use Of
WHERE Clauses
WHAT IT IS ?
Efficiently filtering data using WHERE
clauses.
WHY IT MATTERS ?
Reduces the amount of data processed,
improving query speed.
HOW TO OPTIMIZE ?
Utilize WHERE clauses to filter data as early
as possible in the query process.
Varun Sagar
Theegala
f
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#5
Limiting Result Set
WHAT IT IS ?
Using clauses like LIMIT or FETCH to restrict
the number of rows returned.
WHY IT MATTERS ?
Prevents over-fetching of data, saving
resources and time.
HOW TO OPTIMISE ?
Implement LIMIT or FETCH FIRST clauses in
queries where the full dataset is not
required.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#6
Optimize GROUP BY
& HAVING Clauses
WHAT IT IS ?
Efficiently grouping data and filtering
groups.
WHY IT MATTERS ?
Improves performance, especially in large
datasets.
HOW TO OPTIMIZE ?
Ensure the use of GROUP BY and HAVING
clauses is done in a way that minimizes the
amount of data being grouped.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#7
Use Temporary
Tables Wisely
WHAT IT IS ?
Strategic use of temporary tables for
complex queries.
WHY IT MATTERS ?
Can simplify queries and improve
performance in multi-step processes.
HOW TO OPTIMIZE ?
Use temporary tables to store intermediate
results, especially when dealing with
multiple complex joins or subqueries.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#8
Avoid or Optimize
OR Clauses
WHAT IT IS ?
Replacing or optimizing OR clauses for
efficiency.
WHY IT MATTERS ?
OR clauses can slow down queries;
alternatives can improve performance.
HOW TO OPTIMIZE ?
Replace OR clauses with IN statements
where possible, or break the query into
multiple UNIONed queries.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#9
Use EXISTS
Instead of IN
WHAT IT IS ?
Using EXISTS for subquery checks instead of
IN.
WHY IT MATTERS ?
EXISTS can be faster, especially with large
subquery results.
HOW TO OPTIMIZE ?
Replace IN clauses with EXISTS when
checking for the existence of a row in a
subquery.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
#10
Optimize
Join Orders
WHAT IT IS ?
Ordering joins to process smaller datasets
first.
WHY IT MATTERS ?
Reduces overall query processing time by
minimizing intermediate result size.
HOW TO OPTIMIZE ?
Analyze the size of the tables involved in
joins and structure the query to start with
the smallest table, gradually joining larger
tables.
Varun Sagar
Theegala
LET’S WORK WITH
AN EXAMPLE
Varun Sagar
Theegala
REMEMBER
Varun Sagar
Theegala
Varun Sagar
Theegala
SHARE THIS
If you think
your network
would
find this
valuable
FOLLOW
ME
I help you
GROW &
SUSTAIN as a
Data Analyst