SQL Server优化

遇到优化问题了就来记录一下,暂时不会深入研究数据库优化

查看语句执行效率

执行计划查看

可以直接使用显示预估的执行计划查看,选中语句,直接选择执行计划即可

语句查看

set statistics profile on 
set statistics io on 
set statistics time on 
go 

--两个go中间为你执行的sql语句
select * from Product p where exists (select id from JotrinDB.dbo.MarkingCode where PartNumber=p.ProductName and MarkingCode like '100%')

go 
set statistics profile off 
set statistics io off 
set statistics time off

最好不要使用in,*

这是我写的SQL,有两个不好的地方

select * from Product where productName in (select PartNumber from JotrinDB.dbo.MarkingCode where MarkingCode like '100%')
  1. 在程序中,切记不能使用*
  2. 最好不要使用in,改成exists,而且最好exists的字段是id这类有索引的

改完之后的sql语句如下,要什么数据写什么,使用exists判断id

select p.Name,p.Data from Product p where exists (select id from JotrinDB.dbo.MarkingCode where PartNumber=p.ProductName and MarkingCode like '100%')

猜你喜欢

转载自www.cnblogs.com/yunquan/p/11598854.html
今日推荐