这是我尝试加入的七个表的表结构:
-- tables: en, fr, de, zh_cn, es, ru, pt_br
`geoname_id` INT (11),
`continent_code` VARCHAR (200),
`continent_name` VARCHAR (200),
`country_iso_code` VARCHAR (200),
`country_name` VARCHAR (200),
`subdivision_1_name` VARCHAR (200),
`subdivision_2_name` VARCHAR (200),
`city_name` VARCHAR (200),
`time_zone` VARCHAR (200)
这是新的表结构,其中将存储所有数据:
CREATE TABLE `geo_lists` (
`city_id` int (11), -- en.geoname_id (same for all 7 tables)
`continent_code` varchar (2), -- en.continent_code (same for all 7 tables)
`continent_name` varchar (200), -- en.continent_name (just in english)
`country_code` varchar (2), -- en.country_iso_code (same for all 7 tables)
`en_country_name` varchar (200), -- en.country_name
`fr_country_name` varchar (200), -- fr.country_name
`de_country_name` varchar (200), -- de.country_name
`zh_country_name` varchar (200), -- zh_cn.country_name
`es_country_name` varchar (200), -- es.country_name
`ru_country_name` varchar (200), -- ru.country_name
`pt_country_name` varchar (200), -- pt_br.country_name
`en_state_name` varchar (200), -- en.subdivision_1_name
`fr_state_name` varchar (200), -- fr.subdivision_1_name
`de_state_name` varchar (200), -- de.subdivision_1_name
`zh_state_name` varchar (200), -- zh_cn.subdivision_1_name
`es_state_name` varchar (200), -- es.subdivision_1_name
`ru_state_name` varchar (200), -- ru.subdivision_1_name
`pt_state_name` varchar (200), -- pt_br.subdivision_1_name
`en_province_name` varchar (200), -- en.subdivision_2_name
`fr_province_name` varchar (200), -- fr.subdivision_2_name
`de_province_name` varchar (200), -- de.subdivision_2_name
`zh_province_name` varchar (200), -- zh_cn.subdivision_2_name
`es_province_name` varchar (200), -- es.subdivision_2_name
`ru_province_name` varchar (200), -- ru.subdivision_2_name
`pt_province_name` varchar (200), -- pt_br.subdivision_2_name
`en_city_name` varchar (200), -- en.city_name
`fr_city_name` varchar (200), -- fr.city_name
`de_city_name` varchar (200), -- de.city_name
`zh_city_name` varchar (200), -- zh_cn.city_name
`es_city_name` varchar (200), -- es.city_name
`ru_city_name` varchar (200), -- ru.city_name
`pt_city_name` varchar (200), -- pt_br.city_name
`time_zone` varchar (30) -- en.time_zone (same for all 7 tables)
);
我想加入所有这些,使用区域设置(语言)代码作为列名的前缀。
答案 0 :(得分:3)
哦! @GabrielBlanca你是对的,在这种情况下尝试这个查询,让我知道它是否有效。您可以复制并粘贴:
insert into geo_lists
-- columns
(city_id, continent_code, continent_name, country_code, time_zone,
en_country_name,
fr_country_name,
de_country_name,
zh_country_name,
es_country_name,
ru_country_name,
pt_country_name,
en_state_name,
fr_state_name,
de_state_name,
zh_state_name,
es_state_name,
ru_state_name,
pt_state_name,
en_province_name,
fr_province_name,
de_province_name,
zh_province_name,
es_province_name,
ru_province_name,
pt_province_name,
en_city_name,
fr_city_name,
de_city_name,
zh_city_name,
es_city_name,
ru_city_name,
pt_city_name)
-- end columns
select
en.city_id, en.continent_code, en.continent_name, en.country_code, en.time_zone,
en.country_name as en_country_name,
fr.country_name as fr_country_name,
de.country_name as de_country_name,
zh.country_name as zh_country_name,
es.country_name as es_country_name,
ru.country_name as ru_country_name,
pt.country_name as pt_country_name,
en.state_name as en_state_name,
fr.state_name as fr_state_name,
de.state_name as de_state_name,
zh.state_name as zh_state_name,
es.state_name as es_state_name,
ru.state_name as ru_state_name,
pt.state_name as pt_state_name,
en.province_name as en_province_name,
fr.province_name as fr_province_name,
de.province_name as de_province_name,
zh.province_name as zh_province_name,
es.province_name as es_province_name,
ru.province_name as ru_province_name,
pt.province_name as pt_province_name,
en.city_name as en_city_name,
fr.city_name as fr_city_name,
de.city_name as de_city_name,
zh.city_name as zh_city_name,
es.city_name as es_city_name,
ru.city_name as ru_city_name,
pt.city_name as pt_city_name
from en, fr, de, zh_cn, es, ru, pt_br
where en.city_id = fr.city_id
and fr.city_id = de.city_id
and de.city_id = zh_cn.city_id
and zh_cn.city_id = es.city_id
and es.city_id = ru.city_id
and ru.city_id = pt_br.city_id
答案 1 :(得分:3)
好的Gabriel我们现在在哪里优化。
您在数据加载方面取得了哪些成就
带索引的表结构
行数
我会在这里提出一个答案,因为我将你从一个你愿意删除的重复问题中拖回来(thx)
答案 2 :(得分:2)
我还看到了有关查询优化的重复问题。我找到了解决方案。问题是您的表没有索引。只是做:
ALTER TABLE en
ADD PRIMARY KEY(geoname_id);
ALTER TABLE fr
ADD PRIMARY KEY(geoname_id);
ALTER TABLE de
ADD PRIMARY KEY(geoname_id);
ALTER TABLE zh_cn
ADD PRIMARY KEY(geoname_id);
ALTER TABLE es
ADD PRIMARY KEY(geoname_id);
ALTER TABLE ru
ADD PRIMARY KEY(geoname_id);
ALTER TABLE pt_br
ADD PRIMARY KEY(geoname_id);
然后运行:
DROP TABLE IF EXISTS geo_lists;
CREATE TABLE `geo_lists` (
`city_id` int (11), -- en.geoname_id (same for all 7 tables)
`continent_code` varchar (2), -- en.continent_code (same for all 7 tables)
`continent_name` varchar (200), -- en.continent_name (just in english)
`country_code` varchar (2), -- en.country_iso_code (same for all 7 tables)
`en_country_name` varchar (200), -- en.country_name
`fr_country_name` varchar (200), -- fr.country_name
`de_country_name` varchar (200), -- de.country_name
`zh_country_name` varchar (200), -- zh_cn.country_name
`es_country_name` varchar (200), -- es.country_name
`ru_country_name` varchar (200), -- ru.country_name
`pt_country_name` varchar (200), -- pt_br.country_name
`en_state_name` varchar (200), -- en.subdivision_1_name
`fr_state_name` varchar (200), -- fr.subdivision_1_name
`de_state_name` varchar (200), -- de.subdivision_1_name
`zh_state_name` varchar (200), -- zh_cn.subdivision_1_name
`es_state_name` varchar (200), -- es.subdivision_1_name
`ru_state_name` varchar (200), -- ru.subdivision_1_name
`pt_state_name` varchar (200), -- pt_br.subdivision_1_name
`en_province_name` varchar (200), -- en.subdivision_2_name
`fr_province_name` varchar (200), -- fr.subdivision_2_name
`de_province_name` varchar (200), -- de.subdivision_2_name
`zh_province_name` varchar (200), -- zh_cn.subdivision_2_name
`es_province_name` varchar (200), -- es.subdivision_2_name
`ru_province_name` varchar (200), -- ru.subdivision_2_name
`pt_province_name` varchar (200), -- pt_br.subdivision_2_name
`en_city_name` varchar (200), -- en.city_name
`fr_city_name` varchar (200), -- fr.city_name
`de_city_name` varchar (200), -- de.city_name
`zh_city_name` varchar (200), -- zh_cn.city_name
`es_city_name` varchar (200), -- es.city_name
`ru_city_name` varchar (200), -- ru.city_name
`pt_city_name` varchar (200), -- pt_br.city_name
`time_zone` varchar (30) -- en.time_zone (same for all 7 tables)
);
INSERT INTO geo_lists
SELECT
en.geoname_id,
en.continent_code,
en.continent_name,
en.country_iso_code,
en.country_name AS en_country_name,
fr.country_name AS fr_country_name,
de.country_name AS de_country_name,
zh_cn.country_name AS zh_cn_country_name,
es.country_name AS es_country_name,
ru.country_name AS ru_country_name,
pt_br.country_name AS pt_br_country_name,
en.subdivision_1_name AS en_subdivision_1_name,
fr.subdivision_1_name AS fr_subdivision_1_name,
de.subdivision_1_name AS de_subdivision_1_name,
zh_cn.subdivision_1_name AS zh_cn_subdivision_1_name,
es.subdivision_1_name AS es_subdivision_1_name,
ru.subdivision_1_name AS ru_subdivision_1_name,
pt_br.subdivision_1_name AS pt_br_subdivision_1_name,
en.subdivision_2_name AS en_subdivision_2_name,
fr.subdivision_2_name AS fr_subdivision_2_name,
de.subdivision_2_name AS de_subdivision_2_name,
zh_cn.subdivision_2_name AS zh_cn_subdivision_2_name,
es.subdivision_2_name AS es_subdivision_2_name,
ru.subdivision_2_name AS ru_subdivision_2_name,
pt_br.subdivision_2_name AS pt_br_subdivision_2_name,
en.city_name AS en_city_name,
fr.city_name AS fr_city_name,
de.city_name AS de_city_name,
zh_cn.city_name AS zh_cn_city_name,
es.city_name AS es_city_name,
ru.city_name AS ru_city_name,
pt_br.city_name AS pt_br_city_name,
en.time_zone
FROM en
INNER JOIN fr USING(geoname_id)
INNER JOIN de USING(geoname_id)
INNER JOIN zh_cn USING(geoname_id)
INNER JOIN es USING(geoname_id)
INNER JOIN ru USING(geoname_id)
INNER JOIN pt_br USING(geoname_id);
在我的电脑上,查询时间是8.3秒。祝你好运!
答案 3 :(得分:1)
尝试这样的事情:
INSERT INTO new_table (continent_code, subdivision_1_name)
SELECT en.continent_code , en.subdivision_1_name
FROM en
修改
INSERT INTO new (cz_val,cz_value,en_val,en_value)
SELECT cz.val, cz.value, en.val, en.value
FROM cz
INNER JOIN en ON en.id = cz.id
GROUP BY cz.id
答案 4 :(得分:1)
试试这个:
INSERT INTO geo_lists
SELECT * FROM en UNION
SELECT * FROM fr UNION
SELECT * FROM de UNION
SELECT * FROM zh_cn UNION
SELECT * FROM es UNION
SELECT * FROM ru UNION
SELECT * FROM pt_br UNION