inner join MySQL Profiles 三表内联查询效率优化
2017-06-25 17:29
344 查看
今天系统爆出一个慢SQL语句,经过分析确定查询时间过长的原因主要是数据量大三张表内联耗时较长;经过重新设计将不必要的内联逻辑排除,成功的将查询时间降低一个数量级。
原始SQL:select b.*, c.type_name from msop_t_set_storedata a INNER JOIN msop_t_store_data b on a.data_id = b.Id INNER JOIN msop_t_store_data_type c on b.data_type_id = c.Id where a.cust_num = '6009256673';
修改后:
select b.*, c.type_name from (select data_id ,cust_num from msop_t_set_storedata where cust_num = '6009256673') a INNER JOIN msop_t_store_data b on a.data_id = b.Id INNER JOIN msop_t_store_data_type c on b.data_type_id = c.Id;
执行效率对比:
mysql> set profiling=1;
Query OK, 0 rows affected
mysql> select b.*, c.type_name from msop_t_set_storedata a INNER JOIN msop_t_store_data b on a.data_id = b.Id INNER JOIN msop_t_store_data_type c on b.data_type_id = c.Id where a.cust_num = '6009256673';
+----+----------------+--------------+----------------------------------------------------------+------------+----------+--------------+---------------------+----------+--------+--------------+----------------+-----------+
| Id | data_name | data_type_id | req_url | force_show | sort_num | data_code | update_time | operator | is_new | supplierType | plugin_type_id | type_name |
+----+----------------+--------------+----------------------------------------------------------+------------+----------+--------------+---------------------+----------+--------+--------------+----------------+-----------+
| 13 | 今日浏览量111 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 0 | browseNum | 2016-12-15 16:09:46 | 14050313 | 1 | C,H,S | 8 | 今日数据 |
| 14 | 今日总访客数 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 1 | visitorNum | 2016-06-07 19:30:36 | 14050313 | 0 | C,H,S | 8 | 今日数据 |
| 15 | 今日成交总金额 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 4 | dealAmount | 2016-10-28 11:04:08 | 14050313 | 0 | C,H,S | 8 | 今日数据 |
| 17 | 今日订单数 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 7 | dealOrderNum | 2016-12-15 16:10:26 | 14050313 | 0 | C,H,S | 8 | 今日数据 |
+----+----------------+--------------+----------------------------------------------------------+------------+----------+--------------+---------------------+----------+--------+--------------+----------------+-----------+
4 rows in set
mysql> select b.*, c.type_name from (select data_id ,cust_num from msop_t_set_storedata where cust_num = '6009256673') a INNER JOIN msop_t_store_data b on a.data_id = b.Id INNER JOIN msop_t_store_data_type c on b.data_type_id = c.Id;
+----+----------------+--------------+----------------------------------------------------------+------------+----------+--------------+---------------------+----------+--------+--------------+----------------+-----------+
| Id | data_name | data_type_id | req_url | force_show | sort_num | data_code | update_time | operator | is_new | supplierType | plugin_type_id | type_name |
+----+----------------+--------------+----------------------------------------------------------+------------+----------+--------------+---------------------+----------+--------+--------------+----------------+-----------+
| 13 | 今日浏览量111 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 0 | browseNum | 2016-12-15 16:09:46 | 14050313 | 1 | C,H,S | 8 | 今日数据 |
| 14 | 今日总访客数 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 1 | visitorNum | 2016-06-07 19:30:36 | 14050313 | 0 | C,H,S | 8 | 今日数据 |
| 15 | 今日成交总金额 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 4 | dealAmount | 2016-10-28 11:04:08 | 14050313 | 0 | C,H,S | 8 | 今日数据 |
| 17 | 今日订单数 | 11 | http://msoppre.cnsuning.com/msop/custview/data/index.htm | 1 | 7 | dealOrderNum | 2016-12-15 16:10:26 | 14050313 | 0 | C,H,S | 8 | 今日数据 |
+----+----------------+--------------+----------------------------------------------------------+------------+----------+--------------+---------------------+----------+--------+--------------+----------------+-----------+
4 rows in set
mysql> show profiles;
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | 0.001387 | select b.*, c.type_name from msop_t_set_storedata a INNER JOIN msop_t_store_data b on a.data_id = b.Id INNER JOIN msop_t_store_data_type c on b.data_type_id = c.Id where a.cust_num = '6009256673' |
| 2 | 0.00091275 | select b.*, c.type_name from (select data_id ,cust_num from msop_t_set_storedata where cust_num = '6009256673') a INNER JOIN msop_t_store_data b on a.data_id = b.Id INNER JOIN msop_t_store_data_type c on b.data_type_id = c.Id |
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set
mysql>
优化前执行时间:0.00138700
优化后执行时间:0.00091275
优化时间:0.00047425
效率提升:34.19250180245133%
相关文章推荐
- 关于mysql inner join 连接查询的优化
- 关于mysql inner join 连接查询的优化
- 关于mysql inner join 连接查询的优化
- 关于mysql inner join 连接查询的优化
- 使用mysql update join优化update in的查询效率
- mysql 的优化(如何查询mysql中执行效率低的sql语句)
- Mysql 多表联合查询效率分析及优化
- MySQL查询优化:LIMIT 1避免全表扫描提高查询效率
- Mysql 多表联合查询效率分析及优化
- Mysql 多表联合查询效率分析及优化
- MYSQl left join 联合查询效率分析
- MySql的join(连接)查询 (三表 left join 写法)
- [慢查优化]联表查询注意谁是驱动表 & 你搞不清楚谁join谁更好时请放手让mysql自行判定
- mysql处理上百万条的数据库如何优化语句来提高处理查询效率
- 使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录
- mysql limit的效率及优化和不支持limit子查询的猜测[数据库]
- MySQL查询优化:连接查询排序limit(join、order by、limit语句)
- 使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录
- MySQL查询优化:连接查询排序limit(join、order by、limit语句)介绍
- MySQL查询效率优化--添加索引命令