我正在尝试优化查询的导出过程。我有以下表格(我省略了一些不相关的字段):
CREATE TABLE _termsofuse (
ID int(11) NOT NULL AUTO_INCREMENT, TTC_ART_ID int(11) DEFAULT NULL,
TTC_TYP_ID int(11) DEFAULT NULL,
TERM_OF_USE_NAME varchar(200) DEFAULT NULL,
TERM_OF_USE_VALUE varchar(200) DEFAULT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=185905671 DEFAULT CHARSET=utf8;
CREATE TABLE vehicle (
ID mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
TTC_TYP_ID int(11) unsigned NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY TTC_TYP_ID_UNIQUE (TTC_TYP_ID)
) ENGINE=InnoDB AUTO_INCREMENT=44793 DEFAULT CHARSET=utf8;
CREATE TABLE part (
ID int(11) unsigned NOT NULL AUTO_INCREMENT,
TTC_ART_ID int(11) unsigned NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY TTC_ART_ID_UNIQUE (TTC_ART_ID)
) ENGINE=InnoDB AUTO_INCREMENT=3732260 DEFAULT CHARSET=utf8;
CREATE TABLE term_of_use_name (
ID smallint(5) unsigned NOT NULL AUTO_INCREMENT,
ID_Lang tinyint(3) unsigned NOT NULL,
Name varchar(200) NOT NULL,
PRIMARY KEY (ID, ID_Lang),
UNIQUE KEY Name_Lang_UNIQUE (Name, ID_Lang),
KEY fk_term_of_use_name_lang_id_lang_idx (ID_Lang),
CONSTRAINT fk_term_of_use_name_lang_id_lang FOREIGN KEY (ID_Lang)
REFERENCES lang (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=732 DEFAULT CHARSET=utf8;
CREATE TABLE term_of_use_value (
ID mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
ID_Lang tinyint(3) unsigned NOT NULL,
Value varchar(200) NOT NULL,
PRIMARY KEY (ID,ID_Lang),
UNIQUE KEY Value_Lang_UNIQUE (Value,ID_Lang),
KEY fk_term_of_use_value_lang_id_lang_idx (ID_Lang),
CONSTRAINT fk_term_of_use_value_lang_id_lang FOREIGN KEY (ID_Lang)
REFERENCES lang (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=887502 DEFAULT CHARSET=utf8;
现在我尝试为 csv 文件选择一些列。之后我会将文件导入数据库表,但我怀疑这不会花费太多时间。我的 Select 语句如下:
SELECT DISTINCT vehicle.ID, part.ID, term_of_use_name.ID, term_of_use_value.ID FROM _termsofuse
INNER JOIN vehicle ON vehicle.TTC_TYP_ID = _termsofuse.TTC_TYP_ID
INNER JOIN part ON part.TTC_ART_ID = _termsofuse.TTC_ART_ID
INNER JOIN term_of_use_name ON term_of_use_name.Name = _termsofuse.TERM_OF_USE_NAME AND term_of_use_name.ID_Lang = 2
INNER JOIN term_of_use_value ON term_of_use_value.Value = _termsofuse.TERM_OF_USE_VALUE AND term_of_use_value.ID_Lang = 2
INTO OUTFILE 'termsofuse.csv'
CHARACTER SET utf8
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';
这个查询在我的笔记本电脑上需要超过 8 个小时(我有 4 GB 的 RAM)。我试图查看 SELECT 部分的解释,它显示以下内容:
我不明白瓶颈到底在哪里。我在不到 1 小时内导出了一个类似的(大约 9500 万条记录)查询。也使用将结果分成多个表limit
似乎没有多大帮助......请看一下,您需要的任何其他信息都告诉我。先感谢您。
编辑 2016
年 1 月 15 日
解释选择的结果