现在的位置: 首页 > 综合 > 正文

学习sql

2012年09月16日 ⁄ 综合 ⁄ 共 1362字 ⁄ 字号 评论关闭

 

sql语句 关键字等

--打开IO统计
set statistics io on
--打开时间统计
set statistics time on
select * from  Product where[name] like 'p%'
--关闭IO统计
set statistics io off
--关闭时间统计
set statistics time off

dbcc loginfo--查看日志信息

--非聚集索引覆盖
create nonclustered index test_coverage_soh on xxx(customerid,adressid)
--include的魔力 前边俩个是有序的后边total无序的
create nonclustered index test_coverage_soh on xxx(customerid,adressid) include (total)
--非聚集索引的交叉
--非聚集索引的交叉看以看作是覆盖索引的扩展!
create nonclustered index test_coverage_soh on xxx(customerid)
create nonclustered index test_coverage_soh on xxx(adressid)

select * from xxx where customerid=? and adressid=?
--非聚集索引的连接
--非聚集索引的连接实际上是非聚集索引的交叉的一种特例。使得多个非聚集索引交叉后可以覆盖所要查询的数据,从而使得从减少查询基本表变成了完全不用查询基本表:

create nonclustered index test_coverage_soh on xxx(customerid)
create nonclustered index test_coverage_soh on xxx(adressid)

select customerid,adressid from xxx where customerid=? and adressid=?

--非聚集索引的过滤
--很多时候,我们并不需要将基本表中索引列的所有数据全部索引,比如说含有NULL的值不希望被索引,或者根据具体的业务场景,有一些数据我们不想索引
create nonclustered  index test_coverage_soh on xxx(customerid,ordernumber)
where ordernumber is not null

select customerid,ordernumber from xxx where    ordernumber is not null

参考:牛人系列

 

每个人都会犯错,不过6个小时发现问题,这个时间成本也够高的,profiler应该可以比较快定位到耗cpu,耗带宽的语句的。

再说说 count() 的写法,不管是 count(0),count(1),还是count(*)的差异,实践才是硬道理,不要浮于理论。

关于查询行数mssql里面还有一种比较快的写法 
sql2000:
select rows from sysindexes 
where indid in(0,1) and id = object_id('你的表名称')

sql2005:
select rows from sys.partitions 
where index_id in(0,1) and object_id = object_id('你的表名称')

抱歉!评论已关闭.