Django Admin上的某些页面出现问题,导致MySQL的查询卡住(cpu为100%)。
几个星期前,我不得不将所有表从MyISAM转换为InnoDB。这项工作运作良好。
但在某些网页上,请求已被冻结。经过一些调查,我发现这是MySQL的查询。
我注意到外键丢失了。所以我添加了丢失的外键并且查询工作正常。它适用于我的机器(localhost),但不适用于现场。每次我在现场加载页面时,我都必须重新启动MySQL服务器。
我通过MySQL工作台运行查询:
实时环境:AWS(带有RDS的EC2),Django 1.10.5
和MySQL 5.6.x
本地环境:Django 1.10.5
和MySQL 5.7.x
我一直坚持寻找解决方案而且我不知道现在该做什么或寻找。
请告知。
*编辑31-01-2017 *
正在执行的查询:
SELECT `clients_campaignday`.*, `clients_campaign`.*, `clients_client`.*
FROM `clients_campaignday`
INNER JOIN `clients_campaign` ON (`clients_campaignday`.`campaign_id` = `clients_campaign`.`id`)
INNER JOIN `clients_client` ON (`clients_campaign`.`client_id` = `clients_client`.`id`)
ORDER BY `clients_campaignday`.`day` DESC, `clients_campaignday`.`id` DESC LIMIT 50
解释本地:
的查询+----+-------------+---------------------+------------+--------+------------------------------------------------------------------+---------+---------+-------------------------------------------------+-------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------------+------------+--------+------------------------------------------------------------------+---------+---------+-------------------------------------------------+-------+----------+----------------+
| 1 | SIMPLE | clients_campaignday | NULL | ALL | clients_campaignday_6cd0afe2,idx_clients_campaignday_campaign_id | NULL | NULL | NULL | 51839 | 100.00 | Using filesort |
| 1 | SIMPLE | clients_campaign | NULL | eq_ref | PRIMARY,clients_campaign_4fea5d6a | PRIMARY | 4 | tool_2017_01_30.clients_campaignday.campaign_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | clients_client | NULL | eq_ref | PRIMARY | PRIMARY | 4 | tool_2017_01_30.clients_campaign.client_id | 1 | 100.00 | NULL |
+----+-------------+---------------------+------------+--------+------------------------------------------------------------------+---------+---------+-------------------------------------------------+-------+----------+----------------+
解释直播:
的查询+----+-------------+---------------------+------------+--------+----------------------------------------------------------------------------+-----------------------------------------------+---------+-----------------------------------------+------+----------+---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------------+------------+--------+----------------------------------------------------------------------------+-----------------------------------------------+---------+-----------------------------------------+------+----------+---------------------------------+
| 1 | SIMPLE | clients_campaign | NULL | ALL | PRIMARY,clients_campaign_4fea5d6a | NULL | NULL | NULL | 145 | 100.00 | Using temporary; Using filesort |
| 1 | SIMPLE | clients_client | NULL | eq_ref | PRIMARY | PRIMARY | 4 | tool_v3_prod.clients_campaign.client_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | clients_campaignday | NULL | ref | clients_campaignday_campaign_id_283ca030_uniq,clients_campaignday_6cd0afe2 | clients_campaignday_campaign_id_283ca030_uniq | 4 | tool_v3_prod.clients_campaign.id | 201 | 100.00 | NULL |
+----+-------------+---------------------+------------+--------+----------------------------------------------------------------------------+-----------------------------------------------+---------+-----------------------------------------+------+----------+---------------------------------+
在本地:
上显示创建表格CREATE TABLE `clients_campaignday` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`day` date NOT NULL,
`cache_power` int(11) DEFAULT NULL,
`nb_articles` int(11) DEFAULT NULL,
`add_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `clients_campaignday_campaign_id_283ca030_uniq` (`campaign_id`,`day`),
KEY `clients_campaignday_6cd0afe2` (`campaign_id`),
CONSTRAINT `clients_campaignday_ibfk_1` FOREIGN KEY (`campaign_id`) REFERENCES `clients_campaign` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=80960 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
在实时:
上显示创建表格CREATE TABLE `clients_campaignday` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`day` date NOT NULL,
`cache_power` int(11) DEFAULT NULL,
`nb_articles` int(11) DEFAULT NULL,
`add_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `clients_campaignday_6cd0afe2` (`campaign_id`),
KEY `idx_clients_campaignday_campaign_id` (`campaign_id`),
CONSTRAINT `clients_campaignday_campaign_id_6dac75d8_fk_clients_campaign_id` FOREIGN KEY (`campaign_id`) REFERENCES `clients_campaign` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=80960 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
*编辑02-02-2017 *
在调查EXPLAIN结果后,我在当地找到了一些线索。
我应用了一个添加外键并优化数据库的SQL查询:
-- Table : clients_campaign
ALTER TABLE clients_campaign ADD CONSTRAINT clients_campaign_client_id_j3w96j6U FOREIGN KEY (client_id) REFERENCES clients_client(id);
-- Table : clients_campaignday
ALTER TABLE clients_campaignday ADD CONSTRAINT clients_campaignday_campaign_id_69svf27c FOREIGN KEY (campaign_id) REFERENCES clients_campaign(id);
-- Optimize tables
OPTIMIZE TABLE clients_campaignday, clients_campaign;
现在执行的顺序是固定的,但MySQL仍然停留在100%,我不知道为什么。
请建议。