SQL優化之多表關聯查詢-案例一
慢SQL日誌裡看到一個三張表的關聯查詢,如下:
SELECT COUNT(1) FROM refund_order_item i, artisan a, user u WHERE u.userid = i.user_id AND a.artisan_id = i.artisan_id;
測試查詢時間:
mysql> SELECT COUNT(1) -> FROM refund_order_item i, artisan a, user u -> WHERE u.userid = i.user_id -> AND a.artisan_id = i.artisan_id; +----------+ | COUNT(1) | +----------+ | 260605 | +----------+ 1 row in set (2.30 sec)
檢視執行計劃:
mysql> explain SELECT COUNT(1) -> FROM refund_order_item i, artisan a, user u -> WHERE u.userid = i.user_id -> AND a.artisan_id = i.artisan_id; +----+-------------+-------+------------+--------+----------------------------+---------+---------+------------------+--------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+--------+----------------------------+---------+---------+------------------+--------+----------+-------------+ | 1 | SIMPLE | i | NULL | ALL | idx_user_id,idx_artisan_id | NULL | NULL | NULL | 255599 | 100.00 | NULL | | 1 | SIMPLE | a | NULL | eq_ref | PRIMARY | PRIMARY | 122 | hlj.i.artisan_id | 1 | 100.00 | Using index | | 1 | SIMPLE | u | NULL | eq_ref | userid | userid | 122 | hlj.i.user_id | 1 | 100.00 | Using index | +----+-------------+-------+------------+--------+----------------------------+---------+---------+------------------+--------+----------+-------------+
可以看到refund_order_item表沒有走索引。
建立聯合索引:
ALTER TABLE refund_order_item ADD INDEX idx_aid_uid (artisan_id, user_id);
檢視執行計劃:
explain SELECT COUNT(1) FROM refund_order_item i, artisan a, user u WHERE u.userid = i.user_id AND a.artisan_id = i.artisan_id; +----+-------------+-------+------------+--------+----------------------------------------+-------------+---------+------------------+--------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+--------+----------------------------------------+-------------+---------+------------------+--------+----------+-------------+ | 1 | SIMPLE | i | NULL | index | idx_user_id,idx_artisan_id,idx_aid_uid | idx_aid_uid | 244 | NULL | 255599 | 100.00 | Using index | | 1 | SIMPLE | a | NULL | eq_ref | PRIMARY | PRIMARY | 122 | hlj.i.artisan_id | 1 | 100.00 | Using index | | 1 | SIMPLE | u | NULL | eq_ref | userid | userid | 122 | hlj.i.user_id | 1 | 100.00 | Using index | +----+-------------+-------+------------+--------+----------------------------------------+-------------+---------+------------------+--------+----------+-------------+
可以看到執行計劃已經走索引。
測試查詢時間:
mysql> SELECT COUNT(1) -> FROM refund_order_item i, artisan a, user u -> WHERE u.userid = i.user_id -> AND a.artisan_id = i.artisan_id; +----------+ | COUNT(1) | +----------+ | 260605 | +----------+ 1 row in set (1.15 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30135314/viewspace-2654178/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JPA多表關聯查詢
- JPA 之 多表聯合查詢
- MyBatisPlus怎麼多表關聯查詢?MyBatis
- mysql中的多表關聯查詢MySql
- 如何做多表關聯查詢
- Mybatis【15】-- Mybatis一對一多表關聯查詢MyBatis
- 多表聯合查詢 - 基於註解SQLSQL
- 十七、Mysql之SQL優化查詢MySql優化
- onethinkphp 如何做多表關聯查詢PHP
- SQL查詢的:子查詢和多表查詢SQL
- 深入sql多表差異化聯合查詢的問題詳解SQL
- Spring Data JPA 實現多表關聯查詢Spring
- 優化sql查詢速度優化SQL
- 《MySQL慢查詢優化》之SQL語句及索引優化MySql優化索引
- jpa動態查詢與多表聯合查詢
- 多表關聯查詢中,關聯欄位都應該建立索引嗎?索引
- 兩表關聯查詢:sql、mybatisSQLMyBatis
- pgsql查詢優化之模糊查詢SQL優化
- MySQL exists關聯子查詢SQL效能及其低下最佳化之等值子查詢轉換MySql
- springDataJpa多表級聯查詢(@ManyToOne @OneToOne)Spring
- 一文終結SQL 子查詢優化SQL優化
- MySQL SQL優化案例(一)MySql優化
- 關於關聯查詢sql的一次最佳化過程及其他SQL
- Oracle效能優化-SQL優化(案例一)Oracle優化SQL
- 多表查詢
- MySQL調優之查詢優化MySql優化
- mybatis多表聯合查詢的寫法MyBatis
- SQL面試題 三(單表、多表查詢)SQL面試題
- Oracle總結【SQL細節、多表查詢、分組查詢、分頁】OracleSQL
- MySQL 優化器引數derived_merge導致多表關聯SQL效能及其低下MySql優化
- Oracle\MS SQL Server Update多表關聯更新OracleSQLServer
- sql 多表關聯刪除表資料SQL
- KunlunDB 查詢優化(一)優化
- MySQL 優化五(關聯查詢子查詢以及 in 的效率問題)(高階篇)MySql優化
- 【MySQL】多表查詢MySql
- mysql多表查詢MySql
- 04多表查詢
- MySQL 多表查詢MySql