SQL常用语句整理
SQL清空数据表
truncate table [tablename]
SQL重置主键种子
dbcc checkident ([tablename],reseed,1)
查询触发器
select * from sysobjects where xtype = 'TR'
快速统计
select rowcnt from sysindexes where id=object_id('tablename')
如果返回多条记录使用以下(因为有多列做为索引)
select rowcnt from sysindexes where id=object_id('tablename') where indid=1
indid
索引 ID:
0 = 堆
1 = 聚集索引
> 1 = 非聚集索引
EXEC sp_spaceused @TableName
你要啥有啥……
sql2005以上可以:count(-1),count(abc)这样是最快的.
sql server2005有个功能叫数据表的分区,你看一下就行了。
select count(1) from table
结论
count(*)和count(1)执行的效率是完全一样的。
count(*)的执行效率比count(col)高,因此可以用count(*)的时候就不要去用count(col)。
count(col)的执行效率比count(distinct col)高,不过这个结论的意义不大,这两种方法也是看需要去用。
如果是对特定的列做count的话建立这个列的非聚集索引能对count有很大的帮助。
如果经常count(*)的话则可以找一个最小的col建立非聚集索引以避免全表扫描而影响整体性能。
--1 没有索引,?count(*,1,列) 都相同
--2 非聚集索引 count(*,1,列) 都相同 索引= 非聚集索引
--3 聚集索引 count(*,1,列) 都相同 索引= 聚集索引
--4 聚集索引+非聚集索引 count(*,1,索引列) 都相同 索引=非聚集索引
--5 聚集索引+非聚集索引(单列索引+组合索引) count(*,1,索引列(是count*的默认索引里包含的