这个内部联接声明是否正确?

时间:2016-04-15 08:20:05

标签: mysql inner-join

新列表 - 20,000个数据

记录列表 - 200,000个数据

我想显示从新列表到记录列表的所有匹配记录

它必须与FIRSTNAME,MIDDLENAME,LASTNAME匹配

SELECT new.FIRSTNAME,new.MIDDLENAME,new.LASTNAME 
FROM newlist as new 
INNER JOIN recordlist as bgc ON new.FIRSTNAME = bgc.FIRSTNAME 
AND new.MIDDLENAME = bgc.MIDDLENAME AND new.LASTNAME = bgc.LASTNAME";

此查询是否正确?显示所有比赛?

3 个答案:

答案 0 :(得分:0)

您将我假设的三列编入索引?如果不这样做:)

当它们已编入索引时:

使用多个OR,有时最好分割查询并使用Union连接结果集。这用于使用索引。

SELECT new.FIRSTNAME,new.MIDDLENAME,new.LASTNAME 
FROM newlist as new 
INNER JOIN recordlist as bgc ON new.FIRSTNAME = bgc.FIRSTNAME 
UNION
SELECT new.FIRSTNAME,new.MIDDLENAME,new.LASTNAME 
FROM newlist as new 
INNER JOIN recordlist as bgc ON new.MIDDLENAME = bgc.MIDDLENAME 
UNION
SELECT new.FIRSTNAME,new.MIDDLENAME,new.LASTNAME 
FROM newlist as new 
INNER JOIN recordlist as bgc ON new.LASTNAME = bgc.LASTNAME"

答案 1 :(得分:0)

你没有提到有关索引的任何内容。我假设你没有使用它们。你需要一张200.000长的桌子。

添加索引可能需要一段时间,但每个表只需执行一次。如果在创建表格时完成,则需要<为10ms。

添加索引:

ALTER TABLE `new` ADD INDEX (FIRSTNAME, MIDDLENAME, LASTNAME);
ALTER TABLE `bcg` ADD INDEX (FIRSTNAME, MIDDLENAME, LASTNAME);

然后你可以使用以下查询来做你的事情:

SELECT
  `new`.FIRSTNAME,
  `new`.MIDDLENAME,
  `new`.LASTNAME 
FROM newlist `new`
  INNER JOIN recordlist `bgc`
    ON `new`.FIRSTNAME = `bgc`.FIRSTNAME 
      AND `new`.MIDDLENAME = `bgc`.MIDDLENAME
      AND `new`.LASTNAME = `bgc`.LASTNAME";

答案 2 :(得分:0)

索引3列会非常快速地显示结果。

显示结果需要5秒钟。

SELECT new.FIRSTNAME,new.MIDDLENAME,new.LASTNAME 
FROM newlist as new 
INNER JOIN recordlist as bgc ON new.FIRSTNAME = bgc.FIRSTNAME 
AND new.MIDDLENAME = bgc.MIDDLENAME AND new.LASTNAME = bgc.LASTNAME";