在MySQL

时间:2015-08-14 08:57:05

标签: mysql sql

我有两个表都有一个名为key的列,我喜欢MySQL中的SELECT key FROM table1 MINUS SELECT key FROM table2(5.6.19)。 (table1包含大约150万行,table2包含大约100,000行。)我尝试的东西是

SELECT key FROM table1 WHERE key NOT IN (SELECT key FROM table2);
SELECT a.key FROM table1 a LEFT JOIN table2 b USING (key) WHERE b.key IS NULL;
SELECT a.key FROM table1 a LEFT JOIN table2 b ON a.key=b.key WHERE b.key IS NULL;

但两者都是令人难以置信的低效率! (在等待第一个查询的结果后,我停止了它并在夜间并行启动所有查询。第一个分别是疯狂12小时51分钟,第二个和第三个分别是7小时32分钟和7小时53分钟)

如何有效地完成这项工作?它只是MySQL或所有SQL实现的问题吗? (如果重要:key是char(32)类型,table1包含更多列(也包含很多字符串),table2除了键只有一些整数列)。非常感谢您提前提示!

@Steve Rukuts:EXPLAIN SELECT a.key FROM table1 a LEFT JOIN table2 b ON a.key=b.key WHERE b.key IS NULL;给出:

+----+-------------+-------+-------+---------------+------+---------+------+---------+-----------------------------------------------------------------+
| id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows    | Extra                                                           |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-----------------------------------------------------------------+
|  1 | SIMPLE      | a     | index | NULL          | Key  | 97      | NULL | 1372811 | Using index                                                     |
|  1 | SIMPLE      | b     | index | NULL          | Key  | 33      | NULL |  101580 | Using where; Using index; Using join buffer (Block Nested Loop) |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-----------------------------------------------------------------+

0 个答案:

没有答案