gpt4 book ai didi

mysql - 比较mysql中的两个大数据集或表

转载 作者:行者123 更新时间:2023-11-30 23:13:46 26 4
gpt4 key购买 nike

假设我有 2 个大型 csv 文件(超过 100 万行),并且都是具有排名的名称列表。目标是在两个列表中找到相同的名称、列表 1 的唯一名称和列表 2 的唯一名称。

我想在 mySQL 上这样做,所以我为每个列表创建了一个表,但是循环遍历超过一百万条记录一百万次似乎是一种糟糕的方法,而且非常慢。你会怎么做?

这是一个示例但错误的查询:http://sqlfiddle.com/#!2/9f272/2

最佳答案

下面返回名称在每个表中出现的次数,以及计数。如果名称在每个表中都是唯一的,那么它可能会返回如下内容:

InTable1   InTable2    Count
1 0 xxx
0 1 yyy
1 1 zzz

查询使用 union allgroup by :

select InTable1, InTable2, count(*), min(name), max(name)
from (select name, sum(which = 1) as InTable1, sum(which = 2) as InTable2
from ((select name, 1 as which
from table1
) union all
(select name, 2 as which
from table2
)
) t
group by name
) t
group by InTable1, InTable2;

编辑:

您需要创建索引。语法如下:

create index table1_name on table1(name);
create index table2_name on table2(name);

关于mysql - 比较mysql中的两个大数据集或表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18786211/

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