我正在尝试规范化我的数据库。我已经分解了所有冗余数据,现在正在加入并插入新数据。我一直在一次移植 100 万行,并且直到现在都运行良好。现在一百万行需要几天而不是几分钟,而且它会卡在读取数百万行并且永远不会到达插入部分。
我有这个查询:
INSERT IGNORE INTO bbointra_normalized.entry (DATE,keyword,url,POSITION,competition,searchEngine) SELECT DATE(insDate) AS DATE,k.id AS kid ,u.id uid, POSITION, competition ,s.id AS sid FROM oldSingleTabels.tempData
INNER JOIN bbointra_normalized.keyword k ON tempData.keyword = k.keyword
INNER JOIN bbointra_normalized.searchEngine s ON tempData.searchEngine = s.searchEngine
INNER JOIN bbointra_normalized.urlHash u ON tempData.url = u.url
GROUP BY k.id, s.id, u.id ORDER BY k.id, s.id, u.id
解释:
id select_type table type possible_keys key key_len ref rows Extra
------ ----------- -------- ------ -------------------------------------------- ------------ ------- ---------------------------- ------ ----------------------------------------------
1 SIMPLE s index (NULL) searchEngine 42 (NULL) 539 Using index; Using temporary; Using filesort
1 SIMPLE k index (NULL) keyword 42 (NULL) 17652 Using index; Using join buffer
1 SIMPLE tempData ref keyword_url_insDate,keyword,searchEngine,url keyword 767 func 433 Using where
1 SIMPLE u ref url url 767 oldSingleTabels.tempData.url 1 Using index
显示 INNODB 状态:
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread process no. 4245, id 140024097179392, state: waiting for server activity
Number of rows inserted 26193732, updated 0, deleted 0, read 3383512394
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 39676.56 reads/s
入口SQL:
CREATE TABLE `entry` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`insDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`date` int(11) NOT NULL,
`project` int(11) NOT NULL,
`keyword` int(11) NOT NULL,
`url` int(11) NOT NULL,
`position` int(11) NOT NULL,
`competition` int(11) NOT NULL,
`serachEngine` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unikt` (`date`,`keyword`,`position`,`serachEngine`)
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
最佳答案
尝试删除 GROUP BY 和 ORDER BY 子句,它们处理起来很繁重而且似乎没有增加任何值(value)。
如果表 bbointra_normalized.entry 上有索引,请尝试暂时删除这些索引,因为在插入许多行时更新索引是一个繁重的过程。
关于mysql - SELECT 读取 50 000 行/s 几天从不插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20582055/