gpt4 book ai didi

mysql - 如何在具有数百万条记录的登台表和主表之间查找重复项

转载 作者:行者123 更新时间:2023-11-30 23:14:06 25 4
gpt4 key购买 nike

我正在使用 mysql,我想检查两个表之间的重复行。我使用了 join 但是它花费了太多时间,因为有数百万条记录(例如临时表有 800k 条记录,而主表有大约 1 亿条记录)。

我正在使用的查询如下:

INSERT INTO 
tblspduplicate
SELECT
T2.SP,T1.FileImportedDate,T2.XYZFileName
FROM
tblspmaster T1
INNER JOIN
tblstaging T2
ON
T1.SP=T2.SP;

CREATE TABLE `tblspmaster` (
`CSN` bigint(20) NOT NULL AUTO_INCREMENT,
`SP` varchar(50) NOT NULL,
`FileImportedDate` date NOT NULL,
`XYZFileName` varchar(50) NOT NULL,
`XYZBatch` varchar(50) NOT NULL,
`BatchProcessedDate` date NOT NULL,
`ExpiryDate` date NOT NULL,
`Region` varchar(50) NOT NULL,
`FCCity` varchar(50) NOT NULL,
`VendorID` int(11) NOT NULL,
`LocationID` int(11) NOT NULL,
PRIMARY KEY (`CSN`)
) ENGINE=InnoDB AUTO_INCREMENT=7484570 DEFAULT CHARSET=latin1;


CREATE TABLE `tblstaging` (
`CSN` bigint(20) NOT NULL AUTO_INCREMENT,
`SP` varchar(50) NOT NULL,
`FileImportedDate` date NOT NULL,
`XYZFileName` varchar(50) NOT NULL,
`XYZBatch` varchar(50) NOT NULL,
`BatchProcessedDate` date NOT NULL,
`ExpiryDate` date NOT NULL,
`Region` varchar(50) NOT NULL,
`FCCity` varchar(50) NOT NULL,
`VendorID` int(11) NOT NULL,
`LocationID` int(11) NOT NULL,
PRIMARY KEY (`CSN`),
KEY `ind_staging` (`SP`)
) ENGINE=InnoDB AUTO_INCREMENT=851956 DEFAULT CHARSET=latin1;

最佳答案

tblspmaster.SP 有索引吗?那将是最重要的事情。有了这样的索引,你查询应该没问题。不过,首先,测试仅使用 select 的查询。

您可能遇到的另一个问题是重复匹配。这可能会显着增加您拥有的数据。您可以通过以下方式对此进行测试:

select sp, count(*) as cnt
from tblmaster
group by sp
having cnt > 1
order by cnt desc;

select sp, count(*) as cnt
from tblstaging
having cnt > 1
order by cnt desc;

编辑:

根据表结构,我重复了为 tblMaster(SP) 创建索引的建议。您可能还需要删除 tblStaging(SP) 上的索引。或者,您可以使用索引提示强制使用主索引而不是暂存索引(简单语法描述为 here )。

此外,我建议您运行上述计数,以查看由于 SP 值的多重性而导致意外大量行的风险。

关于mysql - 如何在具有数百万条记录的登台表和主表之间查找重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18673125/

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