mysql> select 1 from t; +---+ | 1 | +---+ | 1 | | 1 | | 1 | | 1 | +---+ 4 rows in set (0.00 sec)
mysql> select count(1) from t; +----------+ | count(1) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec)
mysql> select count(*) from t; +----------+ | count(*) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec)
mysql> select count(a) from t; +----------+ | count(a) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec)
如果表中没有主键,那么count(1)比count(*)快
如果有主键,那么count(主键,联合主键)比count(*)快
如果表中只有一个字段,count(*)最快
时间: 2024-12-28 08:57:58