设为首页 加入收藏

TOP

sql server 表别名性能低 临时表性能高
2014-11-24 02:52:45 来源: 作者: 【 】 浏览:1
Tags: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 ......

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇从性能的角度谈SQL Server聚集索.. 下一篇SQL性能优化

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)