1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. People & Blogs
  6. >
  7. Secret To Optimizing SQL Queries - Understand The SQL Execution Order

Secret To Optimizing SQL Queries - Understand The SQL Execution Order

Available In Following Subtitles
English
Variant 1
Posted on:
Video by: ByteByteGo
Get a Free System Design PDF with 158 pages by subscribing to our weekly newsletter: https://bytebytego.ck.page/subscribe Animation tools: Adobe Illustrator and After Effects. Checkout our bestselling System Design Interview books: Volume 1: https://amzn.to/3Ou7gkd Volume 2: https://amzn.to/3HqGozy The digital version of System Design Interview books: https://bit.ly/3mlDSk9 ABOUT US: Covering topics and trends in large-scale system design, from the authors of the best-selling System Design Interview series.
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:07
In this video, we're going to deep dive into SQL query execution.
0:12
We'll cover query processing, optimization techniques, and best practices.
0:17
Let's get started.
0:19
Here's the example query we'll use throughout this video.
0:23
In this query, we find the top 10 customers with at least a $1,000 spend on orders since
0:29
January 1, 2023 by joining the Customers and Orders table and grouping by customer ID.
0:36
We display their total orders and amount spent sorted in descending order
0:40
by the total spent.
0:42
Now before we dive into the query,
0:44
let's discuss query execution plans.
0:47
Database systems create these plans
0:49
to optimize queries and minimize resource usage.
0:53
Understanding the plan can help us
0:55
optimize our query for better performance.
0:58
Execution plans provide information
1:00
like estimated costs, chosen,
1:03
joint algorithms, and a sequence of operations.
1:07
Now first let's start with the from and join causes.
1:09
This is where we choose the tables we want to work with and specify how to join them.
1:16
In our query, we are using the Customers table and joining it with the Orders table using
1:21
the common ID and Custom ID columns.
1:25
Using indexes on join columns can significantly improve the performance of the join operation.
1:32
Make sure we have appropriate indexes in place for faster query execution.
1:36
Now, index types, such as B-tree and bitmap indexes, can impact performance based on data
1:44
distribution and query types.
1:46
Now we move to the WHERE clause.
1:48
This filters the combined data by applying a condition.
1:52
In our case, we are considering orders placed on or after January 1, 2023.
1:59
It's important to write a soluble query to leverage indexes effectively.
2:04
A soluble means search argument able, and
2:09
it refers to queries that can use indexes for faster execution.
2:14
Now let's dive deeper into the concept of soluble queries.
2:17
When we say a query is soluble, it means that the query can efficiently use
2:22
indexes to speed up the execution process.
2:25
Writing soluble queries is essential for optimizing database performance.
2:30
Here is an example to help illustrate soluble vs. non-soluble queries.
2:36
In a soluble query, we directly compare the two variables.
2:39
the Order Date column to a specific date. This allows the database engine to use an
2:45
index on the Order Date column to quickly filter out the records that meet the condition.
2:51
In contrast, the non-sargible query uses the year function on the Order Date column.
2:57
This prevents the database engine from using an index on Order Date because the function
3:03
must be applied to every row in the table, even if the index exists. The non-sargible
3:09
query will be slower because there are a lot more records to scan.
3:14
To write sargible queries, 1. Avoid using functions or calculations on index columns
3:20
in the WHERE clause. 2. Use direct comparisons when possible. Do not wrap columns in a function.
3:28
3. If we need to use a function on a column, create a computer column or a function-based
3:35
index if the database system supports it.
3:39
Next up are the Group By and Having causes. In our query, we are grouping the records
3:45
by customer ID and filtering the groups based on the condition, total spend greater than
3:51
or equal to $1000. This query finds those who have spent a significant amount on orders.
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome