我有一个查询,它在一台机器上运行25秒,在另一台机器上运行0.00秒。
编辑-只需升级mysql即可解决此问题。出于历史目的留有疑问。
扩展解释说明它在不良系统的第一行上拉了2084行,而在其他所有步骤上都拉了1或2行?在好的系统上,它们都是1到3行。
慢查询发生在具有MySQL 5.5.56且具有row_format = COMPACT的虚拟实例(AWS)上。
我使用mysqldump将整个数据库移到了系统上。它是功能更强大的桌面,MySQL版本5.7.22,导入后为row_format = DYNAMIC。
表结构是一个非常基本的外键设置,用于“批处理具有事务”,“事务具有项目”和“项目具有itemexceptions”。
这是查询(从Django模型自动生成的累积查询)
SELECT COUNT(DISTINCT `decision_batch`.`id`)
FROM `decision_batch`
INNER JOIN `decision_transaction`
ON (`decision_batch`.`id` = `decision_transaction`.`batch_id`)
INNER JOIN `decision_item`
ON (`decision_transaction`.`id` = `decision_item`.`transaction_id`)
WHERE `decision_item`.`id` IN (
SELECT U0.`id`
FROM `decision_item` U0
INNER JOIN `decision_transaction` U1
ON (U0.`transaction_id` = U1.`id`)
LEFT OUTER JOIN `decision_itemexception` U2
ON (U0.`id` = U2.`item_id`)
WHERE U1.`batch_id` IN (2266)
GROUP BY U0.`id`, U0.`transaction_id`, U0.`item_type`,
U0.`sequence`, U0.`applied_amount`, U0.`accept`
ORDER BY NULL );
扩展说明显示了不良系统上的“依赖子查询”,但在良好系统上一直“简单”:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
| 1 | PRIMARY | decision_batch | index | PRIMARY | decision_batch_c81d214 | 4 | NULL | 2084 | 100.00 | Using index |
| 1 | PRIMARY | decision_transaction | ref | PRIMARY,decision_transaction_58f424c5,batch_transaction,transaction_accept | transaction_accept | 4 | cloudbox.decision_batch.id | 3 | 100.00 | Using index |
| 1 | PRIMARY | decision_item | ref | decision_item_45d19ab3 | decision_item_45d19ab3 | 4 | cloudbox.decision_transaction.id | 1 | 100.00 | Using where; Using index
| 2 | DEPENDENT SUBQUERY | U1 | ref | PRIMARY,decision_transaction_58f424c5,batch_transaction,transaction_accept | decision_transaction_58f424c5 | 4 | const | 2 | 100.00 | Using index; Using temporary; Using filesort |
| 2 | DEPENDENT SUBQUERY | U0 | ref | decision_item_45d19ab3 | decision_item_45d19ab3 | 4 | cloudbox.U1.id | 1 | 100.00 | |
| 2 | DEPENDENT SUBQUERY | U2 | ref | decision_itemexception_67b70d25,item | decision_itemexception_67b70d25 | 4 | cloudbox.U0.id | 1 | 100.00 | Using index | 3 | 100.00 | Using index; End temporary |
如果有帮助,请显示创建表输出(省略数据行)
decision_batch
CREATE TABLE `decision_batch` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`id`),
KEY `decision_batch_c81d214` (`box_id`),
KEY `decision_batch_1c65f4ce` (`finalized_by_id`),
KEY `dec_opt_idx` (`finalized`,`batch_status`,`box_id`),
CONSTRAINT `decision_batch_ibfk_1`
FOREIGN KEY (`box_id`) REFERENCES `lockbox_lockbox` (`id`),
CONSTRAINT `finalized_by_id_refs_id_31bb6e9b`
FOREIGN KEY (`finalized_by_id`) REFERENCES `config_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2267 DEFAULT CHARSET=latin1
decision_transaction
CREATE TABLE `decision_transaction` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`batch_id` int(11) NOT NULL,
...
PRIMARY KEY (`id`),
KEY `decision_transaction_58f424c5` (`batch_id`),
KEY `batch_transaction` (`id`,`batch_id`),
KEY `transaction_accept` (`batch_id`,`accept`),
CONSTRAINT `batch_id_refs_id_24828b6d`
FOREIGN KEY (`batch_id`) REFERENCES `decision_batch` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19306 DEFAULT CHARSET=latin1
decision_item
CREATE TABLE `decision_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`transaction_id` int(11) NOT NULL,
...
PRIMARY KEY (`id`),
KEY `decision_item_45d19ab3` (`transaction_id`),
CONSTRAINT `transaction_id_refs_id_56c7fe1d`
FOREIGN KEY (`transaction_id`) REFERENCES `decision_transaction` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=45544 DEFAULT CHARSET=latin1
decision_itemexception
CREATE TABLE `decision_itemexception` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item_id` int(11) NOT NULL,
...
PRIMARY KEY (`id`),
KEY `decision_itemexception_67b70d25` (`item_id`),
KEY `item` (`item_id`),
KEY `field` (`field_id`),
CONSTRAINT `field`
FOREIGN KEY (`field_id`) REFERENCES `decision_field` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `item`
FOREIGN KEY (`item_id`) REFERENCES `decision_item` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=44589 DEFAULT CHARSET=latin1