我有 2 个 MySql 表有一些相似的列(存储相同类型的数据)
示例
如果tableA
有一些字段为 email
, phone
, cellphone
, id
tableB
还有email
, phone
, mobile
, id
等等
我想从表A where email = email or phone=phone or mobile=cellphone or id=id
中选择所有用户
但是两个表中的数据都太大>每个表100万条记录。
我们能得到的最好的 Sql 查询是什么?
我正在使用的是
SELECT LN.LEADS360ID ,LN.fundingDate,LN.closedDate
FROM fsbcorploan.loan LN inner JOIN fsbcorponline.leads360leads LD ON
LD.RefId = LN.LEADS360ID OR
LD.Email = LN.borrower_email OR
LD.CellPhone = LN.borrower_home_phone OR
LD.CellPhone = LN.borrower_cell_phone OR
LD.DayPhone = LN.borrower_home_phone OR
LD.DayPhone = LN.borrower_cell_phone OR
LD.EveningPhone = LN.borrower_home_phone OR
LD.EveningPhone = LN.borrower_cell_phone
WHERE dateAdded between '11/01/2012 05:10:00' and '12/11/2012 05:10:00'
执行需要13-15秒
最佳答案
尝试一下这是否更快...可能不会,但值得一试。
SELECT LN.LEADS360ID ,LN.fundingDate,LN.closedDate
FROM fsbcorploan.loan LN
where dateAdded between '11/01/2012 05:10:00' and '12/11/2012 05:10:00'
and exists (
select 1 from fsbcorponline.leads360leads LD
where LD.RefId = LN.LEADS360ID
OR LD.Email = LN.borrower_email
OR LD.CellPhone = LN.borrower_home_phone
OR LD.CellPhone = LN.borrower_cell_phone
OR LD.DayPhone = LN.borrower_home_phone
OR LD.DayPhone = LN.borrower_cell_phone
OR LD.EveningPhone = LN.borrower_home_phone
OR LD.EveningPhone = LN.borrower_cell_phone)
如果确实没有解决办法,您可以使用某种可以预先计算的“找到匹配”计算列。您如何执行此操作将取决于此数据更改的频率。
关于mysql - 我需要连接 2 个表,并在给定的任何列与其他表上的相应列匹配时获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13818981/