这可能会让你大吃一惊,但 MySQL 在后台使用 GROUP BY 来执行 DISTINCT !!!
这是您可能想尝试的东西
如果表被调用mytable
,请执行以下两件事:
首先运行这个
ALTER TABLE mytable ADD INDEX ABC (A,B,C);`
二、运行这个查询
SELECT A,B,C FROM mytable GROUP BY A,B,C;
GROUP BY 实际上在存在索引的情况下效果更好!
这是证明它有效的示例代码
mysql> drop database if exists cool_cs;
Query OK, 1 row affected (0.04 sec)
mysql> create database cool_cs;
Query OK, 1 row affected (0.00 sec)
mysql> use cool_cs
Database changed
mysql> create table mytable
-> (A int,B int,C int, key ABC (A,B,C));
Query OK, 0 rows affected (0.08 sec)
mysql> INSERt INTO mytable VALUES
-> (1,1,1),(1,1,1),(2,2,2),(2,2,2),(3,3,3),
-> (3,3,3),(4,4,4),(4,4,4),(5,5,5),(5,5,5),
-> (5,5,5),(6,6,6),(6,6,6);
Query OK, 13 rows affected (0.06 sec)
Records: 13 Duplicates: 0 Warnings: 0
mysql> select a,b,c FROM mytable group by a,b,c;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
| 5 | 5 | 5 |
| 6 | 6 | 6 |
+------+------+------+
6 rows in set (0.02 sec)
mysql>
试试看 !!!