search
HomeDatabaseMysql TutorialSQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好? 今天遇到某人在我以前写的一篇文章里问到 如果统计信息没来得及更新的话,那岂不是统计出来的数据时错误的了 这篇文章的地址: SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇) 之前

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

今天遇到某人在我以前写的一篇文章里问到

如果统计信息没来得及更新的话,那岂不是统计出来的数据时错误的了

这篇文章的地址:SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇)

之前我以为SELECT COUNT(*)是根据统计信息来的,但是后来想了一下,这个肯定不是

那么SQLSERVER怎麽统计SELECT COUNT(*)的呢??

其实SQLSERVER也是使用扫描的方法

大家也可以先看一下:SQLSERVER中的ALLOCATION SCAN和RANGE SCAN

但是这里不讨论是ALLOCATION SCAN还是RANGE SCAN,大家知道SQLSERVER使用的是扫描的方式就可以了


聚集索引表

SQL脚本如下:

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

<span> 1</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span>
<span> 2</span> <span>GO</span>
<span> 3</span> 
<span> 4</span> <span>--</span><span>建立聚集索引表</span>
<span> 5</span> <span>CREATE</span> <span>TABLE</span> ct1(c1 <span>INT</span>, c2 <span>VARCHAR</span> (<span>2000</span><span>));
</span><span> 6</span> <span>GO</span>
<span> 7</span> <span>--</span><span>建立聚集索引</span>
<span> 8</span> <span>CREATE</span> <span>CLUSTERED</span> <span>INDEX</span> t1c1 <span>ON</span><span> ct1(c1);
</span><span> 9</span> <span>GO</span>
<span>10</span>  
<span>11</span> <span>--</span><span>插入测试数据</span>
<span>12</span> <span>DECLARE</span> <span>@a</span> <span>INT</span><span>;
</span><span>13</span> <span>SELECT</span> <span>@a</span> <span>=</span> <span>1</span><span>;
</span><span>14</span> <span>WHILE</span> (<span>@a</span> <span> <span>12</span><span>)
</span><span>15</span> <span>BEGIN</span>
<span>16</span>     <span>INSERT</span> <span>INTO</span> ct1 <span>VALUES</span> (<span>@a</span>, <span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>2000</span><span>))
</span><span>17</span>     <span>SELECT</span> <span>@a</span> <span>=</span> <span>@a</span> <span>+</span> <span>1</span>
<span>18</span> <span>END</span>
<span>19</span> <span>GO</span>
<span>20</span> 
<span>21</span> 
<span>22</span> 
<span>23</span> 
<span>24</span> <span>--</span><span>查询数据</span>
<span>25</span> <span>SELECT</span> <span>*</span> <span>FROM</span> ct1 </span>
View Code

看一下执行计划

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片一)

<span>1</span> <span>SET</span> <span>STATISTICS</span> PROFILE <span>ON</span>
<span>2</span> <span>GO</span>
<span>3</span> <span>SELECT</span> <span>COUNT</span>(<span>*</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

 SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片二) 

这里需要了解流聚合运算符

 MSDN对于流聚合运算符的解释

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片三)

 

宋沄剑的文章里也有对流聚合运算符的解释

SQL Server中的执行引擎入门

 

重点是理解:Stream Aggregate 运算符按一列或多列对行分组,然后计算由查询返回的一个或多个聚合表达式

Stream Aggregate 运算符按一列对行分组,然后计算由查询返回的一个聚合表达式

我们用下面两个图会清楚一些

 SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片四)

 SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片五)

SQLSERVER对表中的行分组进行扫描,但是SQLSERVER以多少行为一组来进行扫描呢??这个不得而知了

为什麽要使用流聚合?

大家一定会自然而然地想到分组统计提高性能,特别是表中数据量非常大的时候,分组统计特别有用

 

计算标量运算符只是把聚合的结果隐式转换为int类型

 

大家知道ct1表只有两列,但是SELECT COUNT(3) FROM [dbo].[ct1]也能够返回表中的行数

<span>1</span> <span>SELECT</span> <span>COUNT</span>(<span>1</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>

<span>1</span> <span>SELECT</span> <span>COUNT</span>(<span>3</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片六)

 就算用列名都是一样的执行计划

<span>1</span> <span>SELECT</span> <span>COUNT</span>(c1) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>
<span>2</span> <span>SELECT</span> <span>COUNT</span>(c2) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片七)

 

SQLSERVER究竟以哪一列来进行表的行数统计的呢??????

答案就在

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

Stream Aggregate 运算符要求输入的数据要按某列进行排序,如果由于前面的 Sort 运算符或已排序的索引查找或扫描导致数据尚未排序,

则优化器将在此运算符前面使用一个 Sort 运算符,使表的某列是有序排序的。

<span>1</span> <span>SELECT</span>  <span>COUNT</span>(<span>*</span><span>)
</span><span>2</span> <span>SELECT</span>  <span>count</span>(<span>3</span><span>)
</span><span>3</span> <span>SELECT</span>  <span>count</span>(c2)

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片八)

上面三个SQL语句都是按照聚集索引的第一个字段(ct1表中的c1列)来进行统计的

因为聚集索引的第一个字段是根据建立聚集索引的时候的排序顺序预先排好序

Stream Aggregate 运算符要求输入的数据要按某列进行排序

所以无论是指定字段名、*还是数字,都是根据聚集索引的第一个字段来统计


堆表

SQL脚本如下:

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

<span> 1</span> <span>CREATE</span> <span>TABLE</span> t1(c1 <span>INT</span>, c2 <span>VARCHAR</span> (<span>8000</span><span>));
</span><span> 2</span> <span>GO</span>
<span> 3</span> 
<span> 4</span> 
<span> 5</span> <span>--</span><span>插入测试数据</span>
<span> 6</span> 
<span> 7</span> 
<span> 8</span> 
<span> 9</span> <span>DECLARE</span> <span>@a</span> <span>INT</span><span>;
</span><span>10</span> <span>SELECT</span> <span>@a</span> <span>=</span> <span>1</span><span>;
</span><span>11</span> <span>WHILE</span> (<span>@a</span> <span> <span>12</span><span>)
</span><span>12</span> <span>BEGIN</span>
<span>13</span>     <span>INSERT</span> <span>INTO</span> t1 <span>VALUES</span> (<span>@a</span>, <span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>5000</span><span>))
</span><span>14</span>     <span>SELECT</span> <span>@a</span> <span>=</span> <span>@a</span> <span>+</span> <span>1</span>
<span>15</span> <span>END</span>
<span>16</span> <span>GO</span>
<span>17</span>  
<span>18</span> 
<span>19</span> 
<span>20</span> <span>--</span><span>查询数据</span>
<span>21</span> <span>SELECT</span> <span>*</span> <span>FROM</span> t1</span>
View Code

 SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片九)

 SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

 (图片十)

堆表这里使用的是ALLOCATION SCAN

因为分配页面的时候是根据c1列的值从1~12进行分配的

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十一)

109页面存放的c1值是1

120页面存放的c1值是2

174页面存放的c1值是3

193页面存放的c1值是4

8316页面存放的c1值是5

8340页面存放的c1值是6

8351页面存放的c1值是7

8353页面存放的c1值是8

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十二)

这里执行计划在流聚合之前并没有进行排序的原因:因为建表进行页面分配的时候已经按照C1列的值进行有序的页面分配

所以当ALLOCATION SCAN的时候,C1列已经是有序的了

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十三)

不明白的童鞋可以再看一下:SQLSERVER中的ALLOCATION SCAN和RANGE SCAN

为什麽SQLSERVER选择统计C1列的值,因为C1列的值是可以排序的,C2列不能排序,统计不了

那么如果一个表中没有可以用来排序的列呢????

先drop掉t1表,再建立t1表,脚本如下:

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

<span> 1</span> <span>CREATE</span> <span>TABLE</span> t1(c1 <span>VARCHAR</span> (<span>2</span>), c2 <span>VARCHAR</span> (<span>8000</span><span>));
</span><span> 2</span> <span>GO</span>
<span> 3</span> 
<span> 4</span> 
<span> 5</span> <span>--</span><span>插入测试数据</span>
<span> 6</span> <span>DECLARE</span> <span>@a</span> <span>INT</span><span>;
</span><span> 7</span> <span>SELECT</span> <span>@a</span> <span>=</span> <span>1</span><span>;
</span><span> 8</span> <span>WHILE</span> (<span>@a</span> <span> <span>12</span><span>)
</span><span> 9</span> <span>BEGIN</span>
<span>10</span>     <span>INSERT</span> <span>INTO</span> t1 <span>VALUES</span> (<span>'</span><span>a</span><span>'</span>, <span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>5000</span><span>))
</span><span>11</span>     <span>SELECT</span> <span>@a</span> <span>=</span> <span>@a</span> <span>+</span> <span>1</span>
<span>12</span> <span>END</span>
<span>13</span> <span>GO</span>
<span>14</span>  
<span>15</span> 
<span>16</span> <span>--</span><span>查询数据</span>
<span>17</span> <span>SELECT</span> <span>*</span> <span>FROM</span> t1</span>
View Code

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

结果是

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十四)

我觉得SQLSERVER应该会在表中加上一列,类似用来区分聚集索引页面重复值的UNIQUIFIER(KEY)

当查询完毕之后就删除掉这一列

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十五)

 


非聚集索引表

SQL脚本如下:

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

<span> 1</span> <span>CREATE</span> <span>TABLE</span> nct1(c1 <span>INT</span>, c2 <span>VARCHAR</span> (<span>8000</span><span>));
</span><span> 2</span> <span>GO</span>
<span> 3</span> <span>--</span><span>建立非聚集索引</span>
<span> 4</span> <span>CREATE</span>  <span>INDEX</span> nt1c1 <span>ON</span><span> nct1(c1);
</span><span> 5</span> <span>GO</span>
<span> 6</span>  
<span> 7</span> <span>--</span><span>插入数据</span>
<span> 8</span> <span>DECLARE</span> <span>@a</span> <span>INT</span><span>;
</span><span> 9</span> <span>SELECT</span> <span>@a</span> <span>=</span> <span>1</span><span>;
</span><span>10</span> <span>WHILE</span> (<span>@a</span> <span> <span>10</span><span>)
</span><span>11</span> <span>BEGIN</span>
<span>12</span>     <span>INSERT</span> <span>INTO</span> nct1 <span>VALUES</span> (<span>@a</span>, <span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>5000</span><span>))
</span><span>13</span>     <span>SELECT</span> <span>@a</span> <span>=</span> <span>@a</span> <span>+</span> <span>1</span>
<span>14</span> <span>END</span>
<span>15</span> <span>GO</span>
<span>16</span> 
<span>17</span> <span>--</span><span>查询数据</span>
<span>18</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct1</span><span>]</span>
<span>19</span>  </span>
View Code

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十六)

大家一定要记住:非聚集索引是建立在c1列上的!!!

下面两个SQL语句都是一样的,都是根据c1列的值进行统计,而SQLSERVER只扫描非聚集索引页面,而不扫描数据页面

<span>1</span> <span>SELECT</span>  <span>COUNT</span>(<span>*</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct1</span><span>]</span>
<span>2</span> 
<span>3</span> <span>SELECT</span>  <span>COUNT</span>(<span>3</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct1</span><span>]</span>

SELECT  COUNT(*) FROM [dbo].[nct1]是不需要到数据页面去读取c2列的数据的,只需要扫描非聚集索引页面(c1列)就可以了

SELECT  COUNT(3) FROM [dbo].[nct1]跟SELECT  COUNT(*) FROM [dbo].[nct1]也是一样

不知道大家还记得书签查找不,如果SQLSERVER扫描了非聚集索引页面之后还需要到数据页面去读取其他字段的数据的话,就需要RID查找运算符

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十七)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SELECT  COUNT(*) FROM [dbo].[nct1]和SELECT  COUNT(3) FROM [dbo].[nct1]的扫描方式跟前面说的聚集索引表是差不多的

这里就不一一叙述了~

 

而SELECT  COUNT(c2) FROM [dbo].[nct1]为什麽会用表扫描呢?

<span>1</span> <span>SELECT</span>  <span>COUNT</span>(c2) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct1</span><span>]</span>

c2列不在非聚集索引页面里,所以需要表扫描
SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十八)

SELECT  COUNT(c2) FROM [dbo].[nct1]跟前面说的堆表是差不多的,这里就不一一叙述了


总结

做了这麽多实验

可以总结出:select count(*)、count(数字)、count(字段名)是没有性能差别的!!

我说的没有差别是在相同的条件下,就像非聚集索引表,如果使用

SELECT  COUNT(c2) FROM [dbo].[nct1]

SELECT  COUNT(*) FROM [dbo].[nct1]、SELECT  COUNT(3) FROM [dbo].[nct1]相比肯定有差别

因为SELECT  COUNT(c2) FROM [dbo].[nct1]走的是表扫描

如果SELECT  COUNT(c1) FROM [dbo].[nct1]

SELECT  COUNT(*) FROM [dbo].[nct1]、SELECT  COUNT(3) FROM [dbo].[nct1]相比是没有差别的

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

(图片十九)

大家走的都是非聚集索引扫描

 

无论是聚集索引表、堆表、非聚集索引表都是扫描表中的记录来统计出表中的行数的

 

希望大家看完这篇文章之后,不再一知半解了,这是我的希望o(∩_∩)o

 

如有不对的地方,欢迎大家拍砖o(∩_∩)o

 

-----------------------------------------------------------------------

补上IO和时间的比较 2013-10-19

---------------------------------

聚集索引表

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(<span>*</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>

<span>1</span> <span>SQL Server 分析和编译时间: 
</span><span>2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>2</span><span> 毫秒。
</span><span>3</span> 
<span>4</span> (<span>1</span><span> 行受影响)
</span><span>5</span> 表 <span>'</span><span>ct1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>5</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>6</span> 
<span>7</span> <span>SQL Server 执行时间:
</span><span>8</span>    CPU 时间 <span>=</span> <span>15</span> 毫秒,占用时间 <span>=</span> <span>2</span> 毫秒。

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(<span>1</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>

<span>1</span> <span>SQL Server 分析和编译时间: 
</span><span>2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>2</span><span> 毫秒。
</span><span>3</span> 
<span>4</span> (<span>1</span><span> 行受影响)
</span><span>5</span> 表 <span>'</span><span>ct1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>5</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>6</span> 
<span>7</span> <span>SQL Server 执行时间:
</span><span>8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span> 毫秒。

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(c1) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>ct1</span><span>]</span>

<span>1</span> <span>SQL Server 分析和编译时间: 
</span><span>2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>1</span><span> 毫秒。
</span><span>3</span> 
<span>4</span> (<span>1</span><span> 行受影响)
</span><span>5</span> 表 <span>'</span><span>ct1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>5</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>6</span> 
<span>7</span> <span>SQL Server 执行时间:
</span><span>8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span> 毫秒。

---------------------------------------------------

堆表

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(<span>*</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>t1</span><span>]</span>

<span> 1</span> <span>SQL Server 分析和编译时间: 
</span><span> 2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 3</span> 
<span> 4</span> <span>SQL Server 执行时间:
</span><span> 5</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 6</span> 
<span> 7</span> <span>SQL Server 执行时间:
</span><span> 8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 9</span> <span>SQL Server 分析和编译时间: 
</span><span>10</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span>11</span> 
<span>12</span> (<span>1</span><span> 行受影响)
</span><span>13</span> 表 <span>'</span><span>t1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>12</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>14</span> 
<span>15</span> <span>SQL Server 执行时间:
</span><span>16</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span> 毫秒。

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(<span>1</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>t1</span><span>]</span>

<span> 1</span> <span>SQL Server 分析和编译时间: 
</span><span> 2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 3</span> 
<span> 4</span> <span>SQL Server 执行时间:
</span><span> 5</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 6</span> 
<span> 7</span> <span>SQL Server 执行时间:
</span><span> 8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 9</span> <span>SQL Server 分析和编译时间: 
</span><span>10</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>79</span><span> 毫秒。
</span><span>11</span> 
<span>12</span> (<span>1</span><span> 行受影响)
</span><span>13</span> 表 <span>'</span><span>t1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>12</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>14</span> 
<span>15</span> <span>SQL Server 执行时间:
</span><span>16</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span> 毫秒。

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(c1) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>t1</span><span>]</span>

<span> 1</span> <span>SQL Server 分析和编译时间: 
</span><span> 2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 3</span> 
<span> 4</span> <span>SQL Server 执行时间:
</span><span> 5</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 6</span> 
<span> 7</span> <span>SQL Server 执行时间:
</span><span> 8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 9</span> <span>SQL Server 分析和编译时间: 
</span><span>10</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>1</span><span> 毫秒。
</span><span>11</span> 
<span>12</span> (<span>1</span><span> 行受影响)
</span><span>13</span> 表 <span>'</span><span>t1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>12</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>14</span> 
<span>15</span> <span>SQL Server 执行时间:
</span><span>16</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span> 毫秒。

-----------------------------------------------------------------------------------------

非聚集索引表

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(<span>*</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct1</span><span>]</span>

<span> 1</span> <span>SQL Server 分析和编译时间: 
</span><span> 2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 3</span> 
<span> 4</span> <span>SQL Server 执行时间:
</span><span> 5</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 6</span> 
<span> 7</span> <span>SQL Server 执行时间:
</span><span> 8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 9</span> <span>SQL Server 分析和编译时间: 
</span><span>10</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>1</span><span> 毫秒。
</span><span>11</span> 
<span>12</span> (<span>1</span><span> 行受影响)
</span><span>13</span> 表 <span>'</span><span>nct1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>2</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>14</span> 
<span>15</span> <span>SQL Server 执行时间:
</span><span>16</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span> 毫秒。

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(<span>1</span>) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct1</span><span>]</span>

<span> 1</span> <span>SQL Server 分析和编译时间: 
</span><span> 2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 3</span> 
<span> 4</span> <span>SQL Server 执行时间:
</span><span> 5</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 6</span> 
<span> 7</span> <span>SQL Server 执行时间:
</span><span> 8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 9</span> <span>SQL Server 分析和编译时间: 
</span><span>10</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span>11</span> 
<span>12</span> (<span>1</span><span> 行受影响)
</span><span>13</span> 表 <span>'</span><span>nct1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>2</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>14</span> 
<span>15</span> <span>SQL Server 执行时间:
</span><span>16</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>49</span> 毫秒。

<span>1</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span>2</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span>3</span> <span>GO</span>
<span>4</span> <span>SELECT</span> <span>COUNT</span>(c1) <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct1</span><span>]</span>

<span> 1</span> <span>SQL Server 分析和编译时间: 
</span><span> 2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 3</span> 
<span> 4</span> <span>SQL Server 执行时间:
</span><span> 5</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 6</span> 
<span> 7</span> <span>SQL Server 执行时间:
</span><span> 8</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span> 9</span> <span>SQL Server 分析和编译时间: 
</span><span>10</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span>11</span> 
<span>12</span> (<span>1</span><span> 行受影响)
</span><span>13</span> 表 <span>'</span><span>nct1</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>2</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>14</span> 
<span>15</span> <span>SQL Server 执行时间:
</span><span>16</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>1</span> 毫秒。

 

2014-6-21补充:

<span>USE</span> <span>[</span><span>sss</span><span>]</span>
<span>--</span><span>建表</span>
<span>CREATE</span> <span>TABLE</span> counttb ( id <span>INT</span> <span>NULL</span><span> )

</span><span>--</span><span>插入数据</span>
<span>INSERT</span>  <span>INTO</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>counttb</span><span>]</span><span>
        ( </span><span>[</span><span>id</span><span>]</span><span> )
        </span><span>SELECT</span>  <span>1</span>
        <span>UNION</span> <span>ALL</span>
        <span>SELECT</span>  <span>NULL</span> 

<span>--</span><span>统计行数</span>
<span>SELECT</span>  <span>COUNT</span>(<span>1</span><span>) ,
        </span><span>COUNT</span>(<span>*</span><span>) ,
        </span><span>COUNT</span><span>(id)
</span><span>FROM</span>    <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>counttb</span><span>]</span>


<span>--</span><span>查询索引的统计值</span>
<span>SELECT</span>  a.<span>[</span><span>rowcnt</span><span>]</span><span> ,
        b.</span><span>[</span><span>name</span><span>]</span>
<span>FROM</span>    sys.<span>[</span><span>sysindexes</span><span>]</span> <span>AS</span><span> a
        </span><span>INNER</span> <span>JOIN</span> sys.<span>[</span><span>objects</span><span>]</span> <span>AS</span> b <span>ON</span> a.<span>[</span><span>id</span><span>]</span> <span>=</span> b.<span>[</span><span>object_id</span><span>]</span>
<span>WHERE</span>   b.<span>[</span><span>name</span><span>]</span> <span>=</span> <span>'</span><span>counttb</span><span>'</span>


<span>--</span><span>创建非聚集索引</span>
<span>CREATE</span> <span>INDEX</span> ix_counttb_id <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>counttb</span><span>]</span><span> (id)


</span><span>--</span><span>统计行数</span>
<span>SELECT</span>  <span>COUNT</span>(<span>1</span><span>) ,
        </span><span>COUNT</span>(<span>*</span><span>) ,
        </span><span>COUNT</span><span>(id)
</span><span>FROM</span>    <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>counttb</span><span>]</span>

SQLSERVER 里SELECT COUNT(1) 和SELECT COUNT(*)哪个性能好?

因为在创建非聚集索引前和创建非聚集索引后的行数值都是一样的,可以看出COUNT(*) COUNT(1) 和COUNT(ID)

的统计方式不一样,所以没有可比性

一般我们在统计行数的时候都会把NULL值统计在内的,所以这样的话,最好就是使用COUNT(*) 和COUNT(1) ,这样的速度最快!!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
sqlserver数据库中已存在名为的对象怎么解决sqlserver数据库中已存在名为的对象怎么解决Apr 05, 2024 pm 09:42 PM

对于 SQL Server 数据库中已存在同名对象,需要采取以下步骤:确认对象类型(表、视图、存储过程)。如果对象为空,可使用 IF NOT EXISTS 跳过创建。如果对象有数据,使用不同名称或修改结构。使用 DROP 删除现有对象(谨慎操作,建议备份)。检查架构更改,确保没有引用删除或重命名的对象。

sqlserver服务无法启动怎么办sqlserver服务无法启动怎么办Apr 05, 2024 pm 10:00 PM

当 SQL Server 服务无法启动时,可采取以下步骤解决:检查错误日志以确定根本原因。确保服务帐户具有启动服务的权限。检查依赖项服务是否正在运行。禁用防病毒软件。修复 SQL Server 安装。如果修复不起作用,重新安装 SQL Server。

怎么查看sqlserver端口号怎么查看sqlserver端口号Apr 05, 2024 pm 09:57 PM

要查看 SQL Server 端口号:打开 SSMS,连接到服务器。在对象资源管理器中找到服务器名称,右键单击它,然后选择“属性”。在“连接”选项卡中,查看“TCP 端口”字段。

sqlserver数据库在哪里sqlserver数据库在哪里Apr 05, 2024 pm 08:21 PM

SQL Server 数据库文件通常存储在以下默认位置:Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data可通过修改数据库文件路径设置来自定义数据库文件位置。

Java连接SqlServer错误如何解决Java连接SqlServer错误如何解决May 01, 2023 am 09:22 AM

问题发现这次使用的是SqlServer数据库,之前并没有使用过,但是问题不大,我按照需求文档的步骤连接好SqlServer之后,启动SpringBoot项目,发现了一个报错,如下:刚开始我以为是SqlServer连接问题呢,于是便去查看数据库,发现数据库一切正常,我首先第一时间问了我的同事,他们是否有这样的问题,发现他们并没有,于是我便开始了我最拿手的环节,面向百度编程。开始解决具体报错信息是这样,于是我便开始了百度报错:ERRORc.a.d.p.DruidDataSource$CreateCo

sqlserver英文安装怎么更改中文sqlserver英文安装怎么更改中文Apr 05, 2024 pm 10:21 PM

SQL Server 英文安装可通过以下步骤更改为中文:下载相应语言包;停止 SQL Server 服务;安装语言包;更改实例语言;更改用户界面语言;重启应用程序。

Win11无法安装SQL Server的原因及解决方案Win11无法安装SQL Server的原因及解决方案Dec 27, 2023 pm 07:48 PM

有网友反馈,在win11上无法安装sqlserver这款软件,不知道是怎么回事,根据目前的测试来看,win11存在硬盘问题,部分接口硬盘无法安装这款软件。win11为啥不能安装sqlserver:答:win11不能安装sqlserver是硬盘的问题。1、据了解,win11存在对于硬盘的检测bug。2、这导致sqlserver无法在“三星m.2接口”硬盘上安装。3、因此,如果我们要安装的话,需要准备一块其他硬盘。4、然后将该硬盘安装到电脑里,如果没有额外插槽的话就要换掉之前的硬盘。5、安装完成后,

sqlserver数据库日志怎么查询sqlserver数据库日志怎么查询Apr 05, 2024 pm 09:06 PM

可以通过以下步骤查询 SQL Server 数据库日志:1. 打开 SQL Server Management Studio,连接到数据库服务器;2. 展开“管理”节点,导航到“SQL Server 日志”;3. 选择要查询的日志文件,右键单击并选择“查看日志文件”;4. 浏览日志记录。其他查询日志方法:使用 Transact-SQL 查询、PowerShell Cmdlet。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),