MySQL Count Distinct值过程非常缓慢。如何扣紧?
要加快此过程,可以使用INDEX。让我们首先创建一个表-
mysql> create table DemoTable1905 ( FirstName varchar(20), LastName varchar(20) , INDEX F_L_Name(FirstName,LastName) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1905 values('John','Smith'); mysql> insert into DemoTable1905 values('John','Doe'); mysql> insert into DemoTable1905 values('Adam','Smith'); mysql> insert into DemoTable1905 values('John','Doe'); mysql> insert into DemoTable1905 values('Chris','Brown'); mysql> insert into DemoTable1905 values('Adam','Smith');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1905;
这将产生以下输出-
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | Adam | Smith | | Chris | Brown | | John | Doe | | John | Doe | | John | Smith | +-----------+----------+ 6 rows in set (0.00 sec)
这是查询不同值的查询-
mysql> select count(distinct FirstName) from DemoTable1905;
这将产生以下输出-
+---------------------------+ | count(distinct FirstName) | +---------------------------+ | 3 | +---------------------------+ 1 row in set (0.00 sec)