select count 时 *速度快还是ID速度快
select count(*) from list 还是 select count(ID) from list
这两天查询语句哪个速度更快? list表大概有800多万行数据, ID是主键!
我试了一下,感觉没什么区别! 请各位大虾,亲们解答!
统计时,牵扯的表多了,有时需要10多秒才能执行完毕,纠结啊。。。! 必须优化!
附上存储过程,各位大虾看看有什么可以优化的
USE [MetenSite]
GO
/****** 对象: StoredProcedure [dbo].[GetCurrentMonthRegisterNum] 脚本日期: 06/22/2011 10:15:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[GetCurrentMonthRegisterNum](@beginTime dateTime,@endTime dateTime, @ids nvarchar(1000))
as
IF @ids IS NOT NULL
BEGIN
select
(select count(*) from dbo.Member where registerTime BETWEEN @beginTime AND @endTime AND [SupportID] IN (SELECT IDS FROM f_split(@ids, ',') )) as monthRegisterUser,
(select count(*) from dbo.CustomerList where ViewDate BETWEEN @beginTime AND @endTime AND [TID] IN (SELECT IDS FROM f_split(@ids, ',') )) as monthIp,
(select Sum(pv) from dbo.CustomerList where ViewDate BETWEEN @beginTime AND @endTime AND [TID] IN (SELECT IDS FROM f_split(@ids, ',') )) as monthPV,
(select count(*) from dbo.Member where supportId<>2 and registerTime BETWEEN @beginTime AND @endTime AND [SupportID] IN (SELECT IDS FROM f_split(@ids, ',') )) as monthSupportRegisterUser,
(select count(*) from dbo.CustomerList where TID !=-1 and ViewDate BETWEEN @beginTime AND @endTime AND [TID] IN (SELECT IDS FROM f_split(@ids, ',') )) as monthSupportIp,
(select sum(pv) from dbo.CustomerList where TID !=-1 and ViewDate BETWEEN @beginTime AND @endTime AND [TID] IN (SELECT IDS FROM f_split(@ids, ',') )) as monthSupportPV
END
ELSE
BEGIN
select
(select count(*) from dbo.Member where registerTime BETWEEN @beginTime AND @endTime) as monthRegisterUser,
(select count(*) from dbo.CustomerList where ViewDate BETWEEN @beginTime AND @endTime) as monthIp,
(select Sum(pv) from dbo.CustomerList where ViewDate BETWEEN @beginTime AND @endTime) as monthPV,
(select count(*) from dbo.Member where supportId<>2 and registerTime BETWEEN @beginTime AND @endTime ) as monthSupportRegisterUser,
(select count(*) from dbo.CustomerList where TID !=-1 and ViewDate BETWEEN @beginTime AND @endTime) as monthSupportIp,
(select sum(pv) from dbo.CustomerList where TID !=-1 and ViewDate BETWEEN @beginTime AND @endTime) as monthSupportPV
END
------解决方案--------------------10多秒才执行完毕,不排除是锁争的问题.可考虑降低事务隔离级别,
SQL code
select count(ID) from list (nolock)
------解决方案--------------------
把(SELECT IDS FROM f_split(@ids, ',') 的内容放到一个临时表或者表变量,至少函数不必调用多次,这些语句的优化不做只能说明编写者根本没有优化意识,而这时候去探究count(*)、count(id)、count(1)这些微小的性能差别,窃以为不可取。
------解决方案--------------------
一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的
假如表沒有主键(Primary key), 那么count(1)比count(*)快,
如果有主键的話,那主键作为count的条件时候count(主键)最快
如果你的表只有一个字段的话那count(*)就是最快的
count(*) 跟 count(1) 的结果一样,都包括对NULL的统计,而count(column) 是不包括NULL的统计
1、select 1 与 select *的区别
selelct 常量 from ... 对应所有行,返回的永远只有一个值,即常量 。所以正常只会用来判断是否有还是没有(比如exists子句)。而select * from ... 是返回所有行的所有列。
性能上的差异,关键看你的from和where子句。比如说如果你的where条件中可以通过索引,那显然 select 1 from ... 的性能比 select * from ... 好。
2、select sum(1)的使用
select count(*)返回所有满足条件的记录数,此时同select sum(1)
但是sum()可以传任意数字,负数、浮点数都可以,返回的值是传入值n*满足条件记录数m