Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Practical Guide For SQL Tuning #19108

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
843d61d
draft
dbsid Oct 13, 2024
1edd274
draft 2
dbsid Oct 14, 2024
7bae9c9
draft
dbsid Oct 14, 2024
bfe7da1
draft
dbsid Oct 14, 2024
9553527
draft
dbsid Oct 15, 2024
0bc72c1
draft
dbsid Oct 17, 2024
d91c1eb
draft
dbsid Oct 17, 2024
24d0f91
draft
dbsid Oct 21, 2024
50ae7e3
draft
dbsid Oct 22, 2024
ca106fa
draft
dbsid Oct 28, 2024
9694500
draft
dbsid Oct 29, 2024
f6745b2
draft
dbsid Nov 7, 2024
f15a334
draft
dbsid Nov 10, 2024
e0092a5
Update sql-tuning-best-practice.md
dbsid Nov 11, 2024
b1a2187
Apply suggestions from code review
dbsid Nov 11, 2024
866f82e
Apply suggestions from code review
dbsid Nov 11, 2024
46fa81a
fix format
dbsid Nov 11, 2024
6cde429
add variable link
dbsid Nov 11, 2024
fa0aea5
Apply suggestions from code review
dbsid Nov 12, 2024
c5735ef
Apply suggestions from code review
dbsid Nov 12, 2024
ef18d9a
fix indent with 4 spaces
dbsid Nov 12, 2024
3671292
Apply suggestions from code review
dbsid Nov 12, 2024
9d52617
Update sql-tuning-best-practice.md
dbsid Nov 12, 2024
7d30239
include tiflash
dbsid Nov 12, 2024
299aea9
add tiflash content
dbsid Nov 14, 2024
deec5e3
Apply suggestions from code review
dbsid Nov 14, 2024
6158783
add the document purpose and links
dbsid Nov 14, 2024
7e46611
Merge branch 'sql-tuning-best-practice' of https://github.com/dbsid/d…
dbsid Nov 14, 2024
66ee361
fix indent format
dbsid Nov 14, 2024
13dc573
fix CI
dbsid Nov 15, 2024
92fbeae
fix title
dbsid Nov 15, 2024
676c27a
fix format
dbsid Nov 15, 2024
f40b373
fix format
dbsid Nov 15, 2024
011f7c2
fix the comments
dbsid Nov 27, 2024
7920021
Update sql-tuning-best-practice.md
dbsid Dec 4, 2024
824e176
Apply suggestions from code review
dbsid Dec 4, 2024
fd86934
address comments;
dbsid Dec 7, 2024
73a23e1
address the comments
dbsid Dec 7, 2024
c8f7882
fix format
dbsid Dec 7, 2024
ad2efea
unify the terminology
dbsid Dec 7, 2024
03b6659
simple the terminology
dbsid Dec 7, 2024
153ff39
improve the formats
dbsid Dec 7, 2024
7ffcae6
fix
dbsid Dec 7, 2024
2dadad6
fix format
dbsid Dec 7, 2024
8796972
Update sql-tuning-best-practice.md
Yui-Song Dec 12, 2024
d019eba
Merge pull request #1 from Yui-Song/fix-format
dbsid Dec 12, 2024
d7ce1e1
Merge remote-tracking branch 'upstream/master' into sql-tuning-best-p…
dbsid Dec 12, 2024
15574bc
adjust TOC in Tuning guide
dbsid Dec 12, 2024
74f2127
fix ci
dbsid Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
draft
  • Loading branch information
dbsid committed Oct 21, 2024
commit 24d0f915568742e3c46765ff8eb80b9d6980c508
59 changes: 52 additions & 7 deletions sql-tuning-best-practice.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SQL tuning is a critical aspect of optimizing database system performance. It in
- This may include rewriting queries, adding or modifying indexes, updating statistics, or adjusting database parameters.

These steps are iteratively repeated until:
- The system performance meets the desired targets, or
- The system performance meets the desired targets
dbsid marked this conversation as resolved.
Show resolved Hide resolved
- No further improvements can be made to the remaining statements.

It's important to note that SQL tuning is an ongoing process. As your data volumes grow and query patterns evolve, you should:
Expand Down Expand Up @@ -483,15 +483,60 @@ This optimized plan demonstrates the importance of accurate statistics and prope
+---------------------------------------+----------+---------+-----------+----------------------------------------------------------------------------------------+---------------...+----------+------+
```

# 4. Real-World Use Cases
## Index Strategy in TiDB

TiDB is a distributed SQL database that completely decouples the SQL layer (TiDB) from the storage layer (TiKV). Unlike traditional databases, TiDB does not have a buffer pool to cache data at the compute node. As a result, the performance of SQL queries and the TiDB cluster is closely tied to the number of key-value (KV) requests that need to be processed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need to be a bit careful about the names. It will be ambiguous if "TiDB" means the entire TiDB product or only the SQL layer.
Following this doc, I think "TiDB server" and "TiKV server" will be better when you are talking about the SQL layer and the storage layer separately.


In TiDB, leveraging indexes effectively is crucial for performance tuning, as it can significantly reduce the number of KV RPC (Remote Procedure Call) requests. By minimizing these requests, you can greatly improve query performance and overall system efficiency. Here are some key strategies:
dbsid marked this conversation as resolved.
Show resolved Hide resolved

- Avoiding full table scans
- Avoiding sorting
- Skipping row lookups when possible
dbsid marked this conversation as resolved.
Show resolved Hide resolved


1. Create appropriate indexes: Design indexes that cover the columns frequently used in WHERE, JOIN, and ORDER BY clauses.

2. Use covering indexes: When possible, create indexes that include all the columns needed for a query, allowing TiDB to satisfy the query using only the index (Index-Only Scan).

3. Optimize query patterns: Structure your queries to take advantage of existing indexes and avoid operations that might prevent index usage.

4. Consider composite indexes: For queries that filter or sort on multiple columns, a well-designed composite index can be more efficient than multiple single-column indexes.

dbsid marked this conversation as resolved.
Show resolved Hide resolved
5. Use prefix indexes: For string columns, consider indexing only the first few characters if that's sufficient for most queries.

6. Regularly analyze tables: Keep statistics up-to-date to help the query optimizer make better decisions about index usage.

By focusing on these index optimization strategies, you can significantly reduce the number of KV RPC requests, leading to improved query performance in TiDB.


### SQL Tuning Strategy - Reducing the Required I/O

Minimize the number of columns in the SELECT statement
Use a WHERE clause to filter irrelevant results
Use a LIMIT clause to minimize the size of the result set
Carefully create indexes on relevant columns for fast lookup
Push down predicates and functions to TiKV layer when possible
Create indexes that can hold all required data for small, frequent queries to enable IndexReader


Indexing for Query Performance

A good indexing strategy enables TiDB reduces I/O by
Avoiding full table scans
Avoiding sorting
Skipping row lookups when possible

The Cost of Indexing


dbsid marked this conversation as resolved.
Show resolved Hide resolved
Do NOT create unnecessary indexes in TiDB
Data modifications are slower in tables that have many indexes
Affected indexes must be updated in cascade
Negatively impacts the write-intensive workloads significantly
Indexes consume disk space

## Debugging a Bad Plan
When a SQL query performs poorly, it’s usually due to an inefficient execution plan. Key steps to debug:

Compare estimated rows and actual rows from the execution plan. Large discrepancies indicate a problem with the statistics or index selection.
Ensure appropriate indexes are being used. If the query scans large portions of data, it may require better indexing or statistics updates.

## Index Strategy in TiDB
Indexes play a critical role in query performance. A well-designed index can drastically reduce query execution time.

Make sure to follow the composite index strategy
Expand Down