---
title: "count(*)，count(1)和count（字段）的区别"
author: "uule"
date: "2019-04-04T11:19:51Z"
tags: []
categories: ["数据库"]
slug: "count-count-1-count"
draft: false
lastmod: "2019-04-04T11:22:03Z"
id: 2439762
url: "https://uule.iteye.com/blog/2439762"
---

很多人认为count(1)执行的效率会比count(\*)高，原因是count(\*)会存在全表扫描，而count(1)可以针对一个字段进行查询。其实不然，**count(1)和count(\*)都会对全表进行扫描，统计所有记录的条数，包括那些为null的记录**，因此，它们的效率可以说是相差无几。而**count(字段)则与前两者不同，它会统计该字段不为null的记录条数。**

 

**type = index 全索引扫描**

 

下面它们之间的一些对比：

1）在表没有主键时，count(1)比count(\*)快；

2）有主键时，主键作为计算条件，count(主键)效率最高；

3）若表格只有一个字段，则count(\*)效率较高。