SQL 性能调优

    在开始之前,你可以示例数据:

    使用 TiDB Cloud 的 Import 功能导入预先准备好的示例数据。

    慢查询最常见的原因就是 语句执行是全表扫描,或者是用了不合适的索引。

    当基于不在主键或任何二级索引中的列从大表中检索少量行时,通常会获得较差的性能:

    1. SELECT * FROM books WHERE title = 'Marian Yost';
    1. +------------+-------------+-----------------------+---------------------+-------+--------+
    2. | id | title | type | published_at | stock | price |
    3. +------------+-------------+-----------------------+---------------------+-------+--------+
    4. | 65670536 | Marian Yost | Arts | 1950-04-09 06:28:58 | 542 | 435.01 |
    5. | 1164070689 | Marian Yost | Education & Reference | 1916-05-27 12:15:35 | 216 | 328.18 |
    6. | 1414277591 | Marian Yost | Arts | 1932-06-15 09:18:14 | 303 | 496.52 |
    7. | 2305318593 | Marian Yost | Arts | 2000-08-15 19:40:58 | 398 | 402.90 |
    8. | 2638226326 | Marian Yost | Sports | 1952-04-02 12:40:37 | 191 | 174.64 |
    9. +------------+-------------+-----------------------+---------------------+-------+--------+
    10. 5 rows in set
    11. Time: 0.582s

    可以使用 EXPLAIN 来查看这个查询的执行计划,看看为什么查询这么慢:

    1. EXPLAIN SELECT * FROM books WHERE title = 'Marian Yost';
    1. +---------------------+------------+-----------+---------------+-----------------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +---------------------+------------+-----------+---------------+-----------------------------------------+
    4. | TableReader_7 | 1.27 | root | | data:Selection_6 |
    5. | └─Selection_6 | 1.27 | cop[tikv] | | eq(bookshop.books.title, "Marian Yost") |
    6. | └─TableFullScan_5 | 1000000.00 | cop[tikv] | table:books | keep order:false |
    7. +---------------------+------------+-----------+---------------+-----------------------------------------+

    从执行计划中的 TableFullScan_5 可以看出,TiDB 将会对表 books 进行全表扫描,然后对每一行都判断 title 是否满足条件。TableFullScan_5estRows 值为 1000000.00,说明优化器估计这个全表扫描会扫描 1000000.00 行数据。

    更多关于 EXPLAIN 的使用介绍,可以阅读使用 EXPLAIN 解读执行计划

    1. CREATE INDEX title_idx ON books (title);

    现在再执行这个查询将会快很多:

    1. SELECT * FROM books WHERE title = 'Marian Yost';
    1. +------------+-------------+-----------------------+---------------------+-------+--------+
    2. | id | title | type | published_at | stock | price |
    3. +------------+-------------+-----------------------+---------------------+-------+--------+
    4. | 1414277591 | Marian Yost | Arts | 1932-06-15 09:18:14 | 303 | 496.52 |
    5. | 2305318593 | Marian Yost | Arts | 2000-08-15 19:40:58 | 398 | 402.90 |
    6. | 2638226326 | Marian Yost | Sports | 1952-04-02 12:40:37 | 191 | 174.64 |
    7. | 65670536 | Marian Yost | Arts | 1950-04-09 06:28:58 | 542 | 435.01 |
    8. +------------+-------------+-----------------------+---------------------+-------+--------+
    9. Time: 0.007s

    可以使用 EXPLAIN 来查看这个查询的执行计划,看看为什么查询变快了:

    1. +---------------------------+---------+-----------+-------------------------------------+-------------------------------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +---------------------------+---------+-----------+-------------------------------------+-------------------------------------------------------+
    4. | IndexLookUp_10 | 1.27 | root | | |
    5. | ├─IndexRangeScan_8(Build) | 1.27 | cop[tikv] | table:books, index:title_idx(title) | range:["Marian Yost","Marian Yost"], keep order:false |
    6. | └─TableRowIDScan_9(Probe) | 1.27 | cop[tikv] | table:books | keep order:false |
    7. +---------------------------+---------+-----------+-------------------------------------+-------------------------------------------------------+

    从执行计划中的 IndexLookUp_10 可以看出,TiDB 将会通过索引 title_idx 来查询数据,其 estRows 值为 1.27,说明优化器估计只会扫描 1.27 行数据,远远小于之前全表扫的 1000000.00 行数据。

    IndexLookUp_10 执行计划的执行流程是先用 IndexRangeScan_8 算子通过 title_idx 索引获取符合条件的索引数据,然后 TableRowIDScan_9 再更据索引数据里面的 Row ID 回表查询相应的行数据。

    更多关于 TiDB 执行计划的内容,可以阅读TiDB 执行计划概览

    上述解决方案中,需要先读取索引信息,再回表查询对应的行数据。但如果索引数据中包含了 SQL 查询所需的所有信息,就可以省去回表查询这个步骤。

    例如下面查询中,仅需要根据 title 查询对应的 price

    1. SELECT title, price FROM books WHERE title = 'Marian Yost';
    1. +-------------+--------+
    2. | title | price |
    3. +-------------+--------+
    4. | Marian Yost | 435.01 |
    5. | Marian Yost | 328.18 |
    6. | Marian Yost | 496.52 |
    7. | Marian Yost | 402.90 |
    8. | Marian Yost | 174.64 |
    9. +-------------+--------+
    10. 5 rows in set
    11. Time: 0.007s
    1. EXPLAIN SELECT title, price FROM books WHERE title = 'Marian Yost';
    1. +---------------------------+---------+-----------+-------------------------------------+-------------------------------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +---------------------------+---------+-----------+-------------------------------------+-------------------------------------------------------+
    4. | IndexLookUp_10 | 1.27 | root | | |
    5. | ├─IndexRangeScan_8(Build) | 1.27 | cop[tikv] | table:books, index:title_idx(title) | range:["Marian Yost","Marian Yost"], keep order:false |

    删除 title_idx 索引,并新建一个 title_price_idx 索引:

    1. ALTER TABLE books DROP INDEX title_idx;
    1. CREATE INDEX title_price_idx ON books (title, price);

    现在,price 数据已经存储在索引 title_price_idx 中了,所以下面查询仅需扫描索引数据,无需回表查询了。这种索引通常被叫做覆盖索引:

    1. --------------------+---------+-----------+--------------------------------------------------+-------------------------------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +--------------------+---------+-----------+--------------------------------------------------+-------------------------------------------------------+
    4. | IndexReader_6 | 1.27 | root | | index:IndexRangeScan_5 |
    5. | └─IndexRangeScan_5 | 1.27 | cop[tikv] | table:books, index:title_price_idx(title, price) | range:["Marian Yost","Marian Yost"], keep order:false |
    6. +--------------------+---------+-----------+--------------------------------------------------+-------------------------------------------------------+

    现在这条查询的速度将会更快:

    1. SELECT title, price FROM books WHERE title = 'Marian Yost';
    1. +-------------+--------+
    2. | title | price |
    3. +-------------+--------+
    4. | Marian Yost | 174.64 |
    5. | Marian Yost | 328.18 |
    6. | Marian Yost | 402.90 |
    7. | Marian Yost | 435.01 |
    8. | Marian Yost | 496.52 |
    9. +-------------+--------+
    10. 5 rows in set
    11. Time: 0.004s

    由于后面的示例还会用到这个库,删除 title_price_idx 索引。

    1. ALTER TABLE books DROP INDEX title_price_idx;

    如果查询中使用主键过滤数据,这条查询的执行速度会非常快,例如表 books 的主键是列 id,使用列 id 来查询数据:

    1. SELECT * FROM books WHERE id = 896;
    1. +-----+----------------+----------------------+---------------------+-------+--------+
    2. | id | title | type | published_at | stock | price |
    3. +-----+----------------+----------------------+---------------------+-------+--------+
    4. | 896 | Kathryne Doyle | Science & Technology | 1969-03-18 01:34:15 | 468 | 281.32 |
    5. +-----+----------------+----------------------+---------------------+-------+--------+
    6. 1 row in set
    7. Time: 0.004s

    使用 EXPLAIN 查看执行计划:

    1. EXPLAIN SELECT * FROM books WHERE id = 896;

    Point_Get,又名 “点查”,它的执行速度也非常快。