1

假设有一个名为contact下一个结构的表:

id INT -- primary key,autoincrement,index 
firstname VARCHAR (255),
lastname VARCHAR(255),
type ENUM

执行这样的查询:

SELECT c1.id AS c1_id, c2.id AS c2_id
FROM contact c1
INNER JOIN contact c2 ON c1.firstname = c2.firstname AND c1.lastname = c2.lastname
WHERE c1.id <> c2.id AND c1.type=c2.type

在少量记录上还可以...但是当记录数从 30-40 增长到 1000 时,此查询非常慢。需要从记录数中抽象并尽可能加快此查询速度。有什么建议吗?

4

3 回答 3

4

你可以尝试这样的事情:

SELECT GROUP_CONCAT(DISTINCT id), firstname, lastname
FROM contact
GROUP BY firstname, lastname
HAVING COUNT(DISTINCT id)>1

这将返回所有重复的名称。如果您想要 ID,则可以使用 JOIN:

SELECT
  contact.id
FROM
  contact INNER JOIN (SELECT firstname, lastname
                      FROM contact
                      GROUP BY firstname, lastname
                      HAVING COUNT(DISTINCT id)>1) dup
  ON contact.firstname=dup.firstname AND contact.lastname=dup.lastname
于 2013-11-13T10:13:54.257 回答
1

与 fthiella 的回答略有偏差(以防这是您需要的):

SELECT group_concat(id) as ids, firstname, lastname
FROM contact
GROUP BY firstname, lastname

上面的查询将为每个名字+姓氏组合使用逗号分隔的 id 列表填充列 id。

于 2013-11-13T10:17:48.837 回答
1

除了“<>”之外,您的查询都很好(事实上,它甚至可能比提出的替代方案快一点!)。只是您的索引需要一些工作...

 CREATE TABLE contact
 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,firstname VARCHAR(255) NOT NULL
 ,lastname VARCHAR(255) NOT NULL
 ,type ENUM('small','medium','large') NOT NULL
 );

 INSERT INTO contact VALUES
 (NULL,'John','Brown','small'),
 (NULL,'Bill','Red','small'),
 (NULL,'Paul','Orange','medium'),
 (NULL,'Mike','Green','large'),
 (NULL,'John','Scarlet','small'),
 (NULL,'John','Cyan','medium'),
 (NULL,'Fiona','Brown','large'),
 (NULL,'John','Brown','small'),
 (NULL,'Chris','Copper','medium'),
 (NULL,'Steve','Silver','large');

 INSERT INTO contact SELECT NULL,x.firstname, y.lastname, z.type FROM contact x, contact y, contact z;

SELECT COUNT(*) FROM contact;
+----------+
| COUNT(*) |
+----------+
|     1010 |
+----------+
1 row in set (0.01 sec) 

 SELECT c1.id c1_id
      , c2.id c2_id
   FROM contact c1
   JOIN contact c2 
     ON c1.firstname = c2.firstname 
    AND c1.lastname = c2.lastname
    AND c1.type=c2.type
  WHERE c1.id < c2.id; 

  ...
  ...
  |  1006 |  1008 |
  +-------+-------+
  5634 rows in set (0.16 sec)

所以,现在让我们在 (firstname,lastname,type) 上添加一个索引...

 DROP TABLE IF EXISTS contact;

 CREATE TABLE contact
 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,firstname VARCHAR(255) NOT NULL
 ,lastname VARCHAR(255) NOT NULL
 ,type ENUM('small','medium','large') NOT NULL
 ,INDEX(firstname,lastname,type)
 );

 INSERT INTO contact VALUES
 (NULL,'John','Brown','small'),
 (NULL,'Bill','Red','small'),
 (NULL,'Paul','Orange','medium'),
 (NULL,'Mike','Green','large'),
 (NULL,'John','Scarlet','small'),
 (NULL,'John','Cyan','medium'),
 (NULL,'Fiona','Brown','large'),
 (NULL,'John','Brown','small'),
 (NULL,'Chris','Copper','medium'),
 (NULL,'Steve','Silver','large');

 INSERT INTO contact SELECT NULL,x.firstname, y.lastname, z.type FROM contact x, contact y, contact z;

 SELECT c1.id c1_id
      , c2.id c2_id
   FROM contact c1
   JOIN contact c2 
     ON c1.firstname = c2.firstname 
    AND c1.lastname = c2.lastname
    AND c1.type = c2.type
    AND c1.id < c2.id; 

  |   775 |   776 |
  ...
  |  1006 |  1008 |
  +-------+-------+
  5634 rows in set (0.05 sec)
于 2013-11-13T10:30:45.530 回答