网站性能优化实践-for循环中的数据库操作

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} h3 {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; mso-outline-level:3; font-size:13.5pt; font-family:宋体; mso-bidi-font-family:宋体; font-weight:bold;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

核桃博客 转载,http://www.hetaoblog.com/?p=42

网站性能优化实践-for循环中的数据库操作

网站性能优化实践-数据库分页和轻量级Session 提到,数据库操作是较为耗时的IO操作,

在某企业级应用项目中,某用户操作后台可能对应N条数据库记录,在原有业务下,通常N<200,后来,随着业务扩展,出现了N>1000甚至更多的情况,原有代码的一段大概逻辑是

for(int i =0; i <n;++i)

{

select  …. from tableA where id = id[i];

}

虽然sql很简单直接,一个主键的where条件,可是当N较大时,数据库操作的影响实在太大了,虽然在oracle9i下,这样一条sql不过5毫秒左右,可是for循环一下,5秒时间就浪费在这里了。类似这样的代码段还有一些,极大的影响了该操作的时间,

将代码改成如下

select … from tableA  where id in (id0,id1,id2,id3…,id999);

这里,由于oracle的in语句有限制,不能超过1000个,所以如果N较大,仍然需要将N拆成N/1000,然后循环调用;通过这样的改进,

节省了(N/1000+1) * 5秒时间,效果是较为可观的;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值