SQLSERVER 里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脚本如下:
<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>
看一下执行计划
(图片一)
<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>
(图片二)
这里需要了解流聚合运算符
MSDN对于流聚合运算符的解释
(图片三)
宋沄剑的文章里也有对流聚合运算符的解释
SQL Server中的执行引擎入门
重点是理解:Stream Aggregate 运算符按一列或多列对行分组,然后计算由查询返回的一个或多个聚合表达式
Stream Aggregate 运算符按一列对行分组,然后计算由查询返回的一个聚合表达式
我们用下面两个图会清楚一些
(图片四)
(图片五)
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>
(图片六)
就算用列名都是一样的执行计划
<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究竟以哪一列来进行表的行数统计的呢??????
答案就在
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)
(图片八)
上面三个SQL语句都是按照聚集索引的第一个字段(ct1表中的c1列)来进行统计的
因为聚集索引的第一个字段是根据建立聚集索引的时候的排序顺序预先排好序
Stream Aggregate 运算符要求输入的数据要按某列进行排序
所以无论是指定字段名、*还是数字,都是根据聚集索引的第一个字段来统计
堆表
SQL脚本如下:
<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>
(图片九)
(图片十)
堆表这里使用的是ALLOCATION SCAN
因为分配页面的时候是根据c1列的值从1~12进行分配的
(图片十一)
109页面存放的c1值是1
120页面存放的c1值是2
174页面存放的c1值是3
193页面存放的c1值是4
8316页面存放的c1值是5
8340页面存放的c1值是6
8351页面存放的c1值是7
8353页面存放的c1值是8
。
。
。
。
。
(图片十二)
这里执行计划在流聚合之前并没有进行排序的原因:因为建表进行页面分配的时候已经按照C1列的值进行有序的页面分配
所以当ALLOCATION SCAN的时候,C1列已经是有序的了
(图片十三)
不明白的童鞋可以再看一下:SQLSERVER中的ALLOCATION SCAN和RANGE SCAN
为什麽SQLSERVER选择统计C1列的值,因为C1列的值是可以排序的,C2列不能排序,统计不了
那么如果一个表中没有可以用来排序的列呢????
先drop掉t1表,再建立t1表,脚本如下:
<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>
结果是
(图片十四)
我觉得SQLSERVER应该会在表中加上一列,类似用来区分聚集索引页面重复值的UNIQUIFIER(KEY)列
当查询完毕之后就删除掉这一列
(图片十五)
非聚集索引表
SQL脚本如下:
<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>
(图片十六)
大家一定要记住:非聚集索引是建立在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(*) 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列不在非聚集索引页面里,所以需要表扫描
(图片十八)
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]相比是没有差别的
(图片十九)
大家走的都是非聚集索引扫描
无论是聚集索引表、堆表、非聚集索引表都是扫描表中的记录来统计出表中的行数的
希望大家看完这篇文章之后,不再一知半解了,这是我的希望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>
因为在创建非聚集索引前和创建非聚集索引后的行数值都是一样的,可以看出COUNT(*) COUNT(1) 和COUNT(ID)
的统计方式不一样,所以没有可比性
一般我们在统计行数的时候都会把NULL值统计在内的,所以这样的话,最好就是使用COUNT(*) 和COUNT(1) ,这样的速度最快!!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

The syntax differences between MySQL and SQL Server are mainly reflected in database objects, data types, SQL statements and other aspects. Database object differences include the storage engine and how filegroups are specified, and the creation of indexes and constraints. Data type differences involve differences in numeric types, character types, and date and time types. SQL statement differences are reflected in result set limitations, data insertion, update and delete operations, etc. Other differences include how identity columns, views, and stored procedures are created. Understanding these differences is important to avoid errors when using different database systems.

To delete a SQL Server database, please perform the following steps in sequence: 1. Log in to SQL Server Management Studio; 2. Expand the database node; 3. Right-click the database to be deleted; 4. Select "Delete"; 5. Confirm the deletion. Note: Deleting the database is irreversible, please make sure you have backed up important data and disconnected other objects.

Ollama is a super practical tool that allows you to easily run open source models such as Llama2, Mistral, and Gemma locally. In this article, I will introduce how to use Ollama to vectorize text. If you have not installed Ollama locally, you can read this article. In this article we will use the nomic-embed-text[2] model. It is a text encoder that outperforms OpenAI text-embedding-ada-002 and text-embedding-3-small on short context and long context tasks. Start the nomic-embed-text service when you have successfully installed o

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

The problem that SQL Server cannot be reinstalled due to incomplete deletion can be solved by following the following steps: manually delete files and registry entries; use SQL Server installation and uninstall tools; use third-party uninstall tools; check Windows Event Viewer; restart the computer; reinstall SQL Server.

The location where the Navicat database configuration files are stored varies by operating system: Windows: The user-specific path is %APPDATA%\PremiumSoft\Navicat\macOS: The user-specific path is ~/Library/Application Support/Navicat\Linux: The user-specific path is ~/ .config/navicat\The configuration file name contains the connection type, such as navicat_mysql.ini. These configuration files store database connection information, query history, and SSH settings.
