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

来源:互联网 发布:计算机算法流程图 编辑:程序博客网 时间:2024/05/01 14:28

<!-- /* 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秒时间,效果是较为可观的;

原创粉丝点击