12 Tips For Optimizing SQL Queries For Faster Performance - by Sarang S. Babu - Learning SQL - Medium
12 Tips For Optimizing SQL Queries For Faster Performance - by Sarang S. Babu - Learning SQL - Medium
155 5
Image by Author
down query performance. When using wildcard characters, the database has
Search Write
to scan the entire table to find the relevant data. To optimize SQL queries, it
is important to minimize the use of wildcard characters and to use them only
when absolutely necessary.
Let’s consider a query to locate all clients whose last name of the city begins
with the letter “P”, for instance. The following query uses a wildcard
character to find all matching records:
This query will work, but it will be slower than a query that uses an index on
the last_name_city column. The query can be improved by adding an index
to the last_name_city column and rewriting it as follows:
SELECT * FROM customers WHERE last_name_city >= 'P' AND last_name < 'Q';
This query will use the index on the last name column and will be faster than
the previous query.
Use the following query to find all orders made by a specific customer:
Because the database must search the entire table for the entries that match
the customer number, this query may take a long time if the orders table
contains a lot of records. You can make an index on the customer_number
column to improve the query:
The order total column contains numeric values. If the order total column is
stored as a text data type, queries that perform calculations on the order
total will be slower than if the column was stored as a numeric data type.
4. Avoid subqueries
Subqueries can slow down query performance, especially when used in the
WHERE or HAVING clauses. It is important to avoid subqueries whenever
possible and to use JOINs or other techniques instead.
For example, consider a query that finds all customers who have placed an
order in the last 30 days. The following query uses a subquery to find all
order IDs within the last 30 days:
SELECT * FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHE
This query will work, but it will be slower than a query that uses a JOIN to
find the relevant data. The following query uses a JOIN to find all customers
who have placed an order in the last 30 days:
This query joins the customers table with the orders table and retrieves all
customer information for those who have placed an order in the last 30 days.
This query will be faster than the previous query because it avoids the use of
a subquery.
For example, consider a query to find all customers who have placed an
order in the last 27 days. If there are a large number of customers who have
placed orders in the last 27 days, the query could return a large number of
rows. This can be optimized using LIMIT or TOP. The following query limits
the number of rows returned to 10:
SELECT TOP 10 * FROM customers WHERE customer_id IN (SELECT customer_id FROM ord
This query will only return the top 10 rows that match the criteria, which
will improve query performance.
For example, consider a query to find all customers who have placed an
order in the last 30 days. The following query selects all columns from the
customers table:
SELECT * FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHE
To optimize the query, the SELECT statement can be modified to only select
the columns that are needed:
SELECT customer_id, first_name, last_name FROM customers WHERE customer_id IN (S
This query will only select the customer ID, first name, and last name
columns, which will improve query performance.
For example, consider a query to find all customers who have placed an
order in the last 30 days:
SELECT * FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHE
This query uses IN to compare the customer ID with the list of customer IDs
returned by the subquery. To optimize the query, you can use EXISTS instead
of IN:
SELECT * FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_
This query uses EXISTS to check if a matching row exists in the orders table
instead of using IN. This can improve query performance by avoiding a full
table scan.
For example, consider a query to find the total number of orders placed by
each customer:
This query uses GROUP BY to group the rows by customer ID and count the
number of orders placed by each customer. To optimize the query, you can
use a subquery to retrieve the customer information and join it with the
orders table:
This query uses a subquery to calculate the number of orders placed by each
customer and then joins the result with the customers table to retrieve the
customer information. This avoids the use of GROUP BY and can improve
query performance
Conclusion
Optimizing SQL queries for faster performance is an important step in
ensuring that database applications run efficiently. Through this article, we
can conclude the following points -
4. Test queries on realistic data sets to ensure that optimizations are having
the desired effect.
References
1. Sarang S, What are SQL Wildcard Operators? (2022), Plumbers of Data
Science
A tech enthusiast with a great taste in technology, avid gamer and a marketer by
profession. 😎
Responses (5)
Respond
Bappy
about 1 year ago
1 1 reply Reply
Akash Borigi
about 2 months ago (edited)
Do you have any open thoughts when formulating a text-to-SQL query on how to detect a best-fit query for
faster execution?
Reply
Baksa Peter
6 months ago
last_name
Reply
Python for SQL: An Introduction to Must Know SQL for Data Analytics
Database Connectivity (Comprehensive Guide)
Overview SQL is your non-arguable prerequisite in
breaking into data and excelling in your data…
How to Calculate Median the Right Using Python for Malware Analysis
Way in PostgreSQL — A Beginners Guide
Let me show you 2 different ways! Overview
See all from Sarang S. Babu See all from Learning SQL
Lists