gpt4 book ai didi

mysql - 合并包含 1700 万条记录的表

转载 作者:行者123 更新时间:2023-11-29 00:55:10 25 4
gpt4 key购买 nike

我有 3 个表,其中 2 个表有 200 000 条记录,另一个表有 1 800 000 条记录。我确实使用 OCN 和 TIMESTAMP(month,year) 两个约束合并了这 3 个表。前两个表的月份和年份列为 Monthx(包括月份、日期和年份)。和其他表格作为每个月和每年的单独列。我给出的查询是,

mysql--> insert into trail 
select * from A,B,C
where A.OCN=B.OCN
and B.OCN=C.OCN
and C.OCN=A.OCN
and date_format(A.Monthx,'%b')=date_format(B.Monthx,'%b')
and date_format(A.Monthx,'%b')=C.IMonth
and date_format(B.Monthx,'%b')=C.month
and year(A.Monthx)=year(B.Monthx)
and year(B.Monthx)=C.Iyear
and year(A.Monthx)=C.Iyear

我在它仍在运行前 4 天给出了这个查询。你能告诉我这个查询是正确的还是错误的,并提供一个准确的查询......(我给了 tat '%b'因为我的 C 表有一个列,其中的月份格式为 JAN,MAR)。

最佳答案

请不要使用隐式 where 连接,将它埋在 1989 年它所属的地方。改用显式连接

select * from a inner join b on (a.ocn = b.ocn and 
date_format(A.Monthx,'%b')=date_format(B.Monthx,'%b') ....

查询的这个选择部分(不得不重写它,因为我拒绝处理 '89 语法)

select * from A
inner join B on (
A.OCN=B.OCN
and date_format(A.Monthx,'%b')=date_format(B.Monthx,'%b')
and year(A.Monthx)=year(B.Monthx)
)
inner join C on (
C.OCN=A.OCN
and date_format(A.Monthx,'%b')=C.IMonth
and date_format(B.Monthx,'%b')=C.month
and year(B.Monthx)=C.Iyear
and year(A.Monthx)=C.Iyear
)

很多问题。

  1. 在字段上使用函数会扼杀在该字段上使用索引的任何机会。
  2. 您正在进行大量重复测试。如果 (A = B)(B = C) 那么它在逻辑上遵循 (A = C)
  3. 日期字段的翻译需要很多时间

我建议您重写您的表以使用不需要翻译(使用函数)但可以直接比较的字段。
yearmonth : char(6) 这样的字段,例如201006 可以更快地进行索引和比较。

如果表 A、B、C 有一个名为 ym 的字段,那么您的查询可以是:

INSERT INTO TRAIL
SELECT a.*, b.*, c.* FROM a
INNER JOIN b ON (
a.ocn = b.ocn
AND a.ym = b.ym
)
INNER JOIN c ON (
a.ocn = c.ocn
AND a.ym = c.ym
);

如果将索引放在 ocn(可能是主索引)和 ym 上,查询应该每秒运行大约一百万行(或更多)。

关于mysql - 合并包含 1700 万条记录的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6421307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com