top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

12 Proven Tips to Speed Up Your SQL Queries

Does it feel like your SQL queries are crawling instead of flying? Don’t worry—you’re not the only one. Many developers and DBAs run into slow query issues at some point. The good news is that with the right optimization techniques, you can significantly boost performance and make your applications run smoother.


In this post, we’ll cover 12 practical best practices to help you fine-tune your SQL queries and keep your database running like a well-oiled machine.

1. Use Indexes Wisely

Indexes are one of the most powerful tools in SQL performance tuning. They act like a table of contents, helping the database locate rows without scanning the entire table.

Types of indexes you should know:

  • Clustered indexes: Organize data rows physically on disk (like sorting books on a shelf).

  • Non-clustered indexes: Store pointers to the data (like a library catalog).

  • Full-text indexes: Useful for searching text-heavy columns quickly.

👉 Tip: Add indexes to frequently queried columns, but don’t overdo it. Too many indexes can slow down writes (INSERT, UPDATE, DELETE).


2. Skip SELECT * and Fetch Only What You Need

Using SELECT * is like ordering the whole buffet when you only want one plate. Instead, specify only the columns you actually need:

-- Avoid

SELECT * FROM customers;


-- Better

SELECT name, email, phone FROM customers;


This cuts down on data transfer and speeds up query execution.


3. Optimize Your Joins

Joins are essential but can also be a performance killer if misused.

Best practices for joins:

  • Favor INNER JOINs when possible—they’re usually faster.

  • Use OUTER JOINs only when necessary.

  • Always join on indexed columns.

  • Combine multiple conditions if it helps narrow down the result set.


4. Replace Nested Subqueries with CTEs

Subqueries can quickly get messy and inefficient. Common Table Expressions (CTEs) provide a cleaner alternative:

WITH recent_signups AS (

    SELECT name, signup_date

    FROM customers

    WHERE signup_year = 2023

)

SELECT MAX(signup_date) AS latest_signup

FROM recent_signups;


CTEs are easier to read, maintain, and often execute more efficiently than deeply nested subqueries.


5. Don’t Run Queries Inside Loops

Executing queries inside loops is like scanning one grocery item at a time—slow and painful. Instead, try batch operations:

-- Slow

FOR EACH customer IN list LOOP

   UPDATE customers SET status = 'active'

   WHERE id = customer.id;

END LOOP;


-- Fast

UPDATE customers

SET status = 'active'

WHERE id IN (SELECT id FROM customer_list);


This way, the database processes updates in bulk.


6. Use Stored Procedures for Heavy Lifting

Stored procedures act like precompiled scripts inside the database. They:

  • Run faster due to caching

  • Reduce network traffic

  • Improve security with controlled execution

  • Simplify maintenance

CREATE PROCEDURE GetLatestSignup AS

BEGIN

    SELECT MAX(signup_date) AS latest_signup

    FROM customer_signups;

END;


7. Normalize Your Schema

Normalization keeps your database clean and avoids redundant data. Following the first three normal forms (1NF, 2NF, 3NF) usually strikes the right balance between performance and consistency.

That said, for very complex queries, selective denormalization may be worth considering—but test thoroughly before applying it.


8. Monitor and Profile Queries

You can’t optimize what you don’t measure. Use tools like:

  • Execution plans

  • Query analyzers

  • Database logs

Regularly monitoring queries helps you identify bottlenecks before they become major issues.


9. Prefer UNION ALL Over UNION

When combining result sets, UNION checks for duplicates, which takes extra processing time. If duplicates aren’t a concern, use UNION ALL:

SELECT name FROM new_customers

UNION ALL

SELECT name FROM old_customers;


10. Use EXISTS Instead of IN for Large Datasets

EXISTS usually outperforms IN because it stops searching after finding the first match:

SELECT *

FROM orders o

WHERE EXISTS (

   SELECT 1

   FROM customers c

   WHERE c.id = o.customer_id

   AND c.status = 'active'

);


11. Limit Your Result Sets

If you don’t need every row, use LIMIT (or TOP in SQL Server) to keep queries lean. For pagination, combine LIMIT with OFFSET:

SELECT name

FROM customers

ORDER BY signup_date DESC

LIMIT 100 OFFSET 200;


12. Take Advantage of Cloud DB Features

If you’re on cloud databases like Snowflake, BigQuery, or Redshift, leverage their built-in optimizations. For instance, Snowflake lets you query JSON fields directly:

SELECT 

   JSON_EXTRACT_PATH_TEXT(customer_data, 'name') AS name,

   JSON_EXTRACT_PATH_TEXT(customer_data, 'email') AS email

FROM orders;


Cloud-native features often provide performance boosts you won’t get from standard SQL tuning alone.


Final Thoughts

SQL optimization isn’t a one-and-done job—it’s an ongoing process. By applying these 12 techniques, you’ll improve query execution, reduce costs, and deliver a much smoother experience for your users.

Keep monitoring, testing, and refining your queries regularly, and you’ll keep your database running at peak performance.



FAQ

How often should I optimize my queries? Check them regularly—monthly or quarterly is a good rhythm, especially after major schema or application changes.

Can too many indexes hurt performance? Yes. While indexes speed up reads, they slow down writes since every insert or update must update the indexes too.

How do I find slow queries? Use profiling tools and execution plans. Look for queries with high execution time or frequent usage.

Are stored procedures always better? Not always. They’re best for complex or repetitive tasks. For simple, one-time queries, inline SQL can be more straightforward.

Does normalization always help? Normalization improves consistency and reduces redundancy but may require more joins. For some workloads, partial denormalization can improve performance.



 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page