最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

[MySQLView]最有意义的视图view优化过程,从30分钟到0.08秒

来源:懂视网 责编:小采 时间:2020-11-09 13:36:18
文档

[MySQLView]最有意义的视图view优化过程,从30分钟到0.08秒

[MySQLView]最有意义的视图view优化过程,从30分钟到0.08秒:[MySQL View]最有意思的视图view优化过程,从30分钟到0.08秒 开发人员写了一个view,select要30分钟,让我优化下,view如下: CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS SELECT o
推荐度:
导读[MySQLView]最有意义的视图view优化过程,从30分钟到0.08秒:[MySQL View]最有意思的视图view优化过程,从30分钟到0.08秒 开发人员写了一个view,select要30分钟,让我优化下,view如下: CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS SELECT o

[MySQL View]最有意思的视图view优化过程,从30分钟到0.08秒 开发人员写了一个view,select要30分钟,让我优化下,view如下: CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS SELECT ol.OFFER_ID AS OFFER_ID,ol.EFFECTIVE_DATE

[MySQL View]最有意思的视图view优化过程,从30分钟到0.08秒
开发人员写了一个view,select要30分钟,让我优化下,view如下:
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS
SELECT ol.OFFER_ID AS OFFER_ID,ol.EFFECTIVE_DATE AS EFFECTIVE_DATE
FROM offer_label ol
WHERE(
ol.ID =
(SELECT ol2.ID
FROM offer_label ol2
WHERE ((ol.OFFER_ID = ol2.OFFER_ID) AND (ol2.LABEL = 'PROD'))
ORDER BY ol2.EFFECTIVE_DATE DESC,ol2.ID DESC LIMIT 1
)
)

开发人员select一下需要30多分钟:
21068 rows in set (1987.08 sec)

先解析一下:
mysql> explain SELECT `ol`.`OFFER_ID` AS `OFFER_ID`,`ol`.`EFFECTIVE_DATE` AS `EFFECTIVE_DATE`
-> FROM `offer_label` `ol`
-> WHERE (`ol`.`ID` =
-> (SELECT `ol2`.`ID`
-> FROM `offer_label` `ol2`
-> WHERE ((`ol`.`OFFER_ID` = `ol2`.`OFFER_ID`) AND (`ol2`.`LABEL` = 'PROD'))
-> ORDER BY `ol2`.`EFFECTIVE_DATE` DESC,`ol2`.`ID` DESC LIMIT 1));
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
| 1 | PRIMARY | ol | index | NULL | offer_label_index | 1542 | NULL | 143299 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | ol2 | ref | OFFER_LABEL_FKEY,offer_label_index | offer_label_index | 1534 | const,catalog.ol.OFFER_ID | 1 | Using where; Using index; Using filesort |
+----+--------------------+-------+-------+------------------------------------+-------------------+---------+---------------------------+--------+------------------------------------------+
2 rows in set (0.00 sec)

看到有 Using filesort,要优化where后面的子判断,优化如下:
select max(ol2.ID)
from offer_label ol2
where ol2.LABEL = 'PROD'
group by ol2.OFFER_ID
order by ol2.EFFECTIVE_DATE DESC,ol2.ID DESC;
mysql> explain select max(ol2.ID)
-> from offer_label ol2
-> where ol2.LABEL = 'PROD'
-> group by ol2.OFFER_ID
-> order by ol2.EFFECTIVE_DATE DESC,ol2.ID DESC;
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
| 1 | SIMPLE | ol2 | ref | offer_label_index | offer_label_index | 767 | const | 71649 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+-------+-----------------------------------------------------------+
1 row in set (0.00 sec)

有些不对劲,再仔细看了view的结构,恍然大悟:

优化成如下样子:

CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS
SELECT ol2.OFFER_ID, max(EFFECTIVE_DATE) EFFECTIVE_DATE
FROM offer_label ol2
WHERE ol2.LABEL = 'PROD'
group by ol2.OFFER_ID ;


执行结果为:

21068 rows in set (0.08 sec)

不到0.08秒,数据完全正确。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

[MySQLView]最有意义的视图view优化过程,从30分钟到0.08秒

[MySQLView]最有意义的视图view优化过程,从30分钟到0.08秒:[MySQL View]最有意思的视图view优化过程,从30分钟到0.08秒 开发人员写了一个view,select要30分钟,让我优化下,view如下: CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_offer_label AS SELECT o
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top