我的MySQL集群:Ver 5.6.30-76.3-56 for debian-linux-gnu on x86_64 (Percona XtraDB Cluster (GPL), Release rel76.3, Revision aa929cb, WSREP version 25.16, wsrep_25.16)
INSERT INTO SELECT时间长集群
我有一个复杂的SQL查询其插入约36K行入表的语法如下:
INSERT INTO `sometable` (SELECT ...);
的选择是有点复杂但不慢(0.0023s),但插入大约需要40-50s。当我插入行时,表格未被使用。
我的问题是:
- 我能否加快点不知何故?
- 慢插入导致其他表上的锁定问题(因为选择)
- 此工作流程是好还是坏的做法?有没有更好的?
感谢
UPDATE:
表架构:
CREATE TABLE `sometable` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned DEFAULT NULL,
`a` varchar(255) DEFAULT NULL,
`b` smallint(6) unsigned DEFAULT NULL,
`c` smallint(6) unsigned DEFAULT NULL,
`d` smallint(6) unsigned DEFAULT NULL,
`e` smallint(6) unsigned DEFAULT NULL,
`f` varchar(255) DEFAULT '',
`country_id` int(10) unsigned DEFAULT NULL,
`city_id` int(10) unsigned DEFAULT NULL,
`g` smallint(6) unsigned DEFAULT NULL,
`h` smallint(6) unsigned DEFAULT NULL,
`i` smallint(6) unsigned DEFAULT NULL,
`j` smallint(6) unsigned DEFAULT NULL,
`k` smallint(6) unsigned DEFAULT NULL,
`l` varchar(3) DEFAULT NULL,
`m` varchar(3) DEFAULT NULL,
`n` text,
`o` varchar(255) DEFAULT NULL,
`p` varchar(32) DEFAULT NULL,
`q` varchar(32) DEFAULT NULL,
`r` varchar(32) DEFAULT NULL,
`s` time DEFAULT NULL,
`t` time DEFAULT NULL,
`u` text,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `country_id` (`country_id`),
KEY `city_id` (`city_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
UPDATE2:
当我尝试运行查询,我得到一个错误有些情况下:
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
我的解决方案:
这是我最后的解决办法,如果有人感兴趣: gist
的主要问题是,虽然我填写mytable
其他查询卡和集群有严重的性能问题。在这个解决方案中,我创建了一个临时表并在“脏读”模式下填充数据,然后我将这些数据以块的形式复制到mytable
,因此需要多一点时间,但没有性能问题,也没有卡住查询。
头脑与我们分享您的表的'CREATE语句TABLE'? (仅包含重要的东西,比如键,索引列,索引等) –
请仔细阅读本http://meta.stackoverflow.com/a/271056/特别是对查询性能的一部分。那么请[编辑]你的问题。 –
你确定你的'SELECT'操作真的很快吗?阅读此:http://meta.stackoverflow.com/a/271056/ –