如何加速作为笛卡儿的MySQL查询

时间:2013-11-13 10:08:56

标签: php mysql sql

假设有一个名为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时,这个查询非常慢。需要从记录计数中抽象出来并尽可能加快这个查询的最大速度。任何建议?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

与fthiella的回答略有偏差(只是这就是你所需要的):

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

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

答案 2 :(得分:1)

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

 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)

所以,现在让我们在(名字,姓氏,类型)...

上添加一个索引
 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)