sql server 表别名性能低 临时表性能高
看以下例子:
Java代码
select * from
( select * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx)
as a where a.xx=xx
select * from
( select * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx)
as a where a.xx=xx
由于a是一个很复杂的东西,关键a是别名出来的。
那这种写法将会非常耗时。
但是如果将select * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx放进一个临时表,再从临时表中加入where a.xx=xx,性能将提高的非常明显。
写法如下:
Java代码
select * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx into #tmpTable
select * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx into #tmpTable
因为临时表用过后要删除,所以考虑可以将整个过程放在一个存储过程里,如下:
Sql代码
写了一个存储过程对视图进行分页查询,但数据增多后发现基效率低得要命,三万多条数据要查询一个半小时都没出来,这不是要了命,于是想到了索引,应用过后仍无济于事。最后对sql进行分析和实践中得出,使用临时表可以大大加快视图的查询速度,见如下sql语句
性能超低的视图分页sql语句:
select top 100 * from
view_customerPayDetails where
( 1=1) and (payId not in
(select top 100 payId from
view_customerPayDetails where
( 1=1) order by payId desc))order by payId desc
使用临时表提升性能的sql语句:
select top 100 payId into #tmpTable
from view_customerPayDetails
order by payId desc
select top 100 * from view_customerPayDetails
where payId not in (select payId from #tmpTable )
order by payId desc
drop table #tmpTable ......
写了一个存储过程对视图进行分页查询,但数据增多后发现基效率低得要命,三万多条数据要查询一个半小时都没出来,这不是要了命,于是想到了索引,应用过后仍无济于事。最后对sql进行分析和实践中得出,使用临时表可以大大加快视图的查询速度,见如下sql语句
性能超低的视图分页sql语句:
select top 100 * from
view_customerPayDetails where
( 1=1) and (payId not in
(select top 100 payId from
view_customerPayDetails where
( 1=1) order by payId desc))order by payId desc
使用临时表提升性能的sql语句:
select top 100 payId into #tmpTable
from view_customerPayDetails
order by payId desc
select top 100 * from view_customerPayDetails
where payId not in (select payId from #tmpTable )
order by payId desc
drop table #tmpTable ......
相关新闻>>
- 发表评论
-
- 最新评论 更多>>