有朋友问了:虽然我没有进入决赛,但是重在参与,对于初赛的题目我还有很多疑惑,到底应该怎么调优呢?
那么今天除了公布成绩,数据君也为大家带来了初赛的赛题解析和选手的解题思路分享。
一、赛题设置
首先我们来回顾一下比赛规则:腾讯云在云服务器上模拟业务访问,造成每组的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` set
create_time = now()
where id in (
select parent from order_itemwhere type = 2
)
SELECT *
FROM `order` o
INNER JOIN order_itemi ON i.parent = o.id
ORDER BY o.status ASC,
i.update_timeDESC
LIMIT , 20
二、赛题解析
update `order` set
create_time = now()
where id in (
select distinct(parent) from order_item where type = 2
)
2. DBbrain优化方案
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_id
FROM `order` o
INNER JOIN order_item i
ON i.parent =o.id
WHERE o.status =
ORDER BY i.update_time DESC
LIMIT , 20)
UNION ALL
(SELECT o.id,
i.id item_id
FROM `order` o
INNER JOIN order_item i
ON i.parent =o.id
WHERE o.status = 1
ORDER BY i.update_time DESC
LIMIT , 20)
) tmp
INNER JOIN `order` o ON tmp.id = o.id
INNER JOIN order_item i ON tmp.item_id =i.id
ORDER BY o.status ASC,
i.update_time DESC
LIMIT , 20