更新:我找到了解决方案。请参阅下面的我的答案。
我的问题
如何优化此查询以最大程度地减少停机时间?我需要更新 50 多个模式,票证数量从 100,000 到 200 万不等。是否建议尝试同时设置 ticket_extra 中的所有字段?我觉得这里有一个我没有看到的解决方案。一天多来,我一直在努力解决这个问题。
此外,我最初尝试不使用子 SELECT,但性能比我目前拥有的要差得多。
背景
我正在尝试针对需要运行的报告优化我的数据库。我需要聚合的字段计算起来非常昂贵,因此我将现有模式非规范化以适应此报告。请注意,我通过删除几十个不相关的列来大大简化了票证表。
我的报告将按Manager When Created和Manager When Resolved汇总票数。这种复杂的关系如下图所示:
(来源:mosso.com)
为了避免即时计算所需的六个讨厌的连接,我将下表添加到我的模式中:
mysql> show create table tickets_extra\G
*************************** 1. row ***************************
Table: tickets_extra
Create Table: CREATE TABLE `tickets_extra` (
`ticket_id` int(11) NOT NULL,
`manager_created` int(11) DEFAULT NULL,
`manager_resolved` int(11) DEFAULT NULL,
PRIMARY KEY (`ticket_id`),
KEY `manager_created` (`manager_created`,`manager_resolved`),
KEY `manager_resolved` (`manager_resolved`,`manager_created`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
现在的问题是,我没有将这些数据存储在任何地方。经理总是动态计算的。我在多个数据库中拥有数百万张具有相同架构的票证,需要填充此表。我想以尽可能高效的方式执行此操作,但在优化我用来执行此操作的查询方面没有成功:
INSERT INTO tickets_extra (ticket_id, manager_created)
SELECT
t.id,
su.user_id
FROM (
SELECT
t.id,
shift_times.shift_id AS shift_id
FROM tickets t
JOIN shifts ON t.shop_id = shifts.shop_id
JOIN shift_times ON (shifts.id = shift_times.shift_id
AND shift_times.dow = DAYOFWEEK(t.created)
AND TIME(t.created) BETWEEN shift_times.start AND shift_times.end)
) t
LEFT JOIN shifts_users su ON t.shift_id = su.shift_id
LEFT JOIN shift_positions ON su.shift_position_id = shift_positions.id
WHERE shift_positions.level = 1
此查询需要一个多小时才能在具有 > 170 万张票证的模式上运行。这对于我拥有的维护窗口是不可接受的。此外,它甚至不处理计算 manager_resolved 字段,因为尝试将其组合到同一个查询中会将查询时间推到平流层。我目前的倾向是将它们分开,并使用 UPDATE 来填充 manager_resolved 字段,但我不确定。
最后,这是该查询的 SELECT 部分的 EXPLAIN 输出:
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 167661
Extra:
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: su
type: ref
possible_keys: shift_id_fk_idx,shift_position_id_fk_idx
key: shift_id_fk_idx
key_len: 4
ref: t.shift_id
rows: 5
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: PRIMARY
table: shift_positions
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 6
Extra: Using where; Using join buffer
*************************** 4. row ***************************
id: 2
select_type: DERIVED
table: t
type: ALL
possible_keys: fk_tickets_shop_id
key: NULL
key_len: NULL
ref: NULL
rows: 173825
Extra:
*************************** 5. row ***************************
id: 2
select_type: DERIVED
table: shifts
type: ref
possible_keys: PRIMARY,shop_id_fk_idx
key: shop_id_fk_idx
key_len: 4
ref: dev_acmc.t.shop_id
rows: 1
Extra:
*************************** 6. row ***************************
id: 2
select_type: DERIVED
table: shift_times
type: ref
possible_keys: shift_id_fk_idx
key: shift_id_fk_idx
key_len: 4
ref: dev_acmc.shifts.id
rows: 4
Extra: Using where
6 rows in set (6.30 sec)
非常感谢您的阅读!