
有朋友问了:虽然我没有进入决赛,但是重在参与,对于初赛的题目我还有很多疑惑,到底应该怎么调优呢?
那么今天除了公布成绩,数据君也为大家带来了初赛的赛题解析和优秀选手的解题思路分享。
一、赛题设置
首先我们来回顾一下比赛规则:腾讯云在云服务器上模拟业务访问,造成每组的MySQL云数据库上出现故障、异常、隐患等现象,参赛者需根据云基础监控和日志信息,在不降低业务访问量的情况下,对业务代码或数据库进行优化。对每个参赛用户的实例进行健康巡检和打分,比赛结束后,按照DBbrain的健康打分确定晋级/排名。如分数相同的,用时最短的参赛者获胜。本文的赛题解析中的优化方案,由DBbrain给出。
比赛共设置了两道题目,分别关于更新语句和查询语句。
先来看一下题目:
CREATE TABLE `order` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`name` varchar(32) NOT NULL,`creator` varchar(24) NOT NULL,`price` varchar(64) NOT NULL,`create_time` timestamp NOT NULL DEFAULTCURRENT_TIMESTAMP,`status` tinyint(1) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `order_item` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`name` varchar(32) NOT NULL,`parent` bigint(20) NOT NULL,`status` int(11) NOT NULL,`type` varchar(12) NOT NULL DEFAULT '0',`quantity` int(11) NOT NULL DEFAULT '1',`update_time` timestamp NOT NULL DEFAULTCURRENT_TIMESTAMP,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
update `order` setcreate_time = now()where id in (select parent from order_itemwhere type = 2)

SELECT *FROM `order` oINNER JOIN order_itemi ON i.parent = o.idORDER BY o.status ASC,i.update_timeDESCLIMIT , 20

二、赛题解析
update `order` setcreate_time = now()where id in (select distinct(parent) from order_item where type = 2)
alter table `order` add index idx_1(type,parent);update `order` o inner join (select type, parent from `order_item` where type = '2' group by type, parent) i on o.id = i.parent set create_time = now();

alter table order_item add index `item_idx_1` (`update_time`,`parent`);SELECT o.*,i.*FROM ((SELECT o.id,i.id item_idFROM `order` oINNER JOIN order_item iON i.parent =o.idWHERE o.status =ORDER BY i.update_time DESCLIMIT , 20)UNION ALL(SELECT o.id,i.id item_idFROM `order` oINNER JOIN order_item iON i.parent =o.idWHERE o.status = 1ORDER BY i.update_time DESCLIMIT , 20)) tmpINNER JOIN `order` o ON tmp.id = o.idINNER JOIN order_item i ON tmp.item_id =i.idORDER BY o.status ASC,i.update_time DESCLIMIT , 20

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69940575/viewspace-3146917/ ,如需转载,请注明出处,否则将追究法律责任。