创建SQL索引,查看SQL性能语句收集

sql中创建主键非聚集索引和聚集索引

Code
--排序(聚集索引)
create clustered index inx_entry_stock_bi on entry_stock_d(entry_stock_bi)

--创建非聚集索引

create nonclustered index inx_entry_stock_on entry_stock_d(entry_stock_bi)

--主键
alter table entry_stock_d add primary key nonclustered--主键且非聚集
(
 entry_stock_bi,aid
)

select * from sysobjects where parent_obj in(select id from sysobjects where xtype='U' and name='saleticket_currency_d')
alter table saleticket_currency_d drop constraint PK__saleticket_curre__1F8E9120
alter table saleticket_currency_d alter column card_id  varchar(20not null
alter table saleticket_currency_d add constraint PK__saleticket_curre__1F8E9120 primary key nonclustered(saleticket_bi,currency_id,card_id)

create clustered index  pk__tb_d_seq  on tb_d(tab_id,seq)


如何测试sql语句性能,提高执行效率
有时候我们经常为我们的sql语句执行效率低下发愁,反复优化后,可还是得不到提高
那么你就用这条语句找出你sql到底是在哪里慢了

Code
示例: 
        
SET STATISTICS io ON
        
SET STATISTICS time ON
        
go
         
---你要测试的sql语句
         select top 100 * from TBL_Cot_RecStaticList
        
go
        
SET STATISTICS profile OFF
        
SET STATISTICS io OFF
        
SET STATISTICS time OFF 


关于sql语句的执行效率测试
在网上找了好久都没有找到关于可以模拟测试数据和sql语句执行时间效率的测试工具,算了自己写一个吧,很简单的:
我们必须先得知道sql语句执行的开始时间和结束时间,比如我们要知道select * from article语句的执行效率:

Code
declare @d datetime   ---定义一个datetime的变量
set @d=getdate()   ---获取查询语句开始前的时间
select * from article   ---执行查询语句
select 语句的执行时间=datediff(ms,@d,getdate())  ---使用datediff()函数,计算精确到ms的执行时间


就这么简单。


 

posted @ 2009-07-03 17:55  stu_acer  阅读(716)  评论(0编辑  收藏  举报