﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-★yesjoy★-文章分类-数据库的查询及性能优化</title><link>http://www.blogjava.net/yesjoy/category/22388.html</link><description>&lt;font color="red"&gt;★&lt;/font&gt;&lt;font color="blue"&gt;总在爬山 所以艰辛;总在寻梦 所以苦痛&lt;/font&gt;&lt;font color="red"&gt;★&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 31 Aug 2010 10:29:11 GMT</lastBuildDate><pubDate>Tue, 31 Aug 2010 10:29:11 GMT</pubDate><ttl>60</ttl><item><title>怎样改进数据库的查询性能? </title><link>http://www.blogjava.net/yesjoy/articles/117036.html</link><dc:creator>★yesjoy★</dc:creator><author>★yesjoy★</author><pubDate>Sat, 12 May 2007 13:00:00 GMT</pubDate><guid>http://www.blogjava.net/yesjoy/articles/117036.html</guid><wfw:comment>http://www.blogjava.net/yesjoy/comments/117036.html</wfw:comment><comments>http://www.blogjava.net/yesjoy/articles/117036.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yesjoy/comments/commentRss/117036.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yesjoy/services/trackbacks/117036.html</trackback:ping><description><![CDATA[<p>转自：<a href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html">http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html</a><br></p>
<p>数据库的查询功能，其性能终究是有限的。即使我们对数据库进行了最优配置，对数据表设计再三斟酌，然而一旦面临海量数据，且返回结果集较大的时候，常规的查询语句就无能为力了。一般说来，当返回的结果集超过总数量的40%时，数据库层面上的优化就显得束手无策了。此时，我们应该考虑从sql语句和程序业务上着手。<br>在我参与开发的业务里，主要是在通讯行业，如移动、电信或网通，其中数据表数量最多的就是话单记录。通常都会在每个月达到百万级的数量，一年合计就达到千万级了。在这种情况下，除了进行定期备份和清除无效数据等措施，以减少话单总量。在进行话单的查询设计时，仍然需要进行设计上的改进，以满足客户的需求。<br><br><strong>1．&nbsp;总体思路<br><br></strong>通过SQL语句&#8220;set rowcount 每页记录数&#8221;，并指定每页记录数，每次只查询符合条件记录集中指定的记录数，以达到分页的目的。由于查询功能一般应用在平台界面中，如果通过分页的方式，可以使得单位查询的速度显著提高。同时，返回的结果集也显著减少，这降低了一次查询消耗内存的容量，对于界面的刷新速度也有明显的提高。由于分页查询将原来一次查询的总时间，通过分页的方式，分割为每个小段，因此对于用户而言，每次获得结果的时间就很短了，这在界面与交互设计中，从考虑用户体验的角度出发，也是非常合理的。<br>由于该方法需要指定每页记录数，因此需要被查询的目的表必须具备一个标识唯一值的字段，并将该字段建立索引，以作为查询和排序的条件。在数据库设计中，有很多种创建标识字段的方法。最简单地莫过于创建Identity字段。当然这种方式的问题也多多，这里不再赘述。也可以写一个存储过程，负责生成唯一标识的ID。<br><br><strong>2．&nbsp;实现方案<br></strong><br>要进行分页查询，首先需要确定每页的记录数。根据各种业务和局方的不同需求，同时各个局方话单量也各有不同，所以，每页记录数值应放到AAA.ini配置文件中，便于灵活配置。<br>在分页查询之前，我们需要知道每个月的话单应该的总页数，可以先获得查询目的表的总记录数（以Ctsi业务 (固网点对点短信)为例，下同），SQL语句如下：<br>select count(1) from CtsiInfoRecord where 条件<br>注：后面的查询语句中均应包括查询条件，为清楚表现sql语句，本文一律省略该条件。<br>然后通过总记录数和每页记录数，获得每个月分页查询的总页数。<br>由于我们的业务主要使用微软的Sql Server2000和sybase。因此，实现分页查询有两种方式。具体实现方案如下：<br><strong>2.1&nbsp; 方案一：通过建立临时表结合分页查询<br></strong><br>在微软的Sql Server中，在其T-SQL中引入了top语法，通过该语法可以非常方便的实现分页查询，sql语句为（以Ctsi业务为例）：<br>select top 每页记录数 * from CtsiInfoRecord01 where IdCdr not in<br>(select top 页数*每页记录数 IdCdr from CtsiInfoRecord01 order by IdCdr)<br>order by IdCdr<br>在实际查询时，只需要修改子查询的top记录数即可。<br>遗憾的是，该top语法在sybase中并不支持。相对应的语法为set rowcount 记录数。但该语法不能放在子查询语句中，因此，上述的方法无法实现。<br>根据该方法的实现思路，引入临时表，并结合分页查询来实现，sql语句如下：<br>set rowcount页数*每页记录数<br>select IdCdr into #ctsitable from CtsiInfoRecord01 order by IdCdr<br>set rowcount 每页记录数<br>select * from CtsiInfoRecord01 where IdCdr not in <br>(select IdCdr from #ctsitable ) order by IdCdr<br>drop table #ctsitable<br>注：#ctsitable为临时库tempdb中的临时表；<br>&nbsp;&nbsp;&nbsp; 在sybase中，不支持在子查询中引入order by；<br>&nbsp;&nbsp;&nbsp; 如果查询第一页，则不需要建立临时表，直接查询即可：<br>&nbsp;&nbsp; set rowcount 每页记录数 select * from CtsiInfoRecord01 order by IdCdr</p>
<p><strong>2.2 方案二：直接根据IdCdr条件分页查询<br></strong><br>假定话单表的唯一标识字段为IdCdr。如果通过order by进行排序（默认升序），在每页记录数固定以及查询条件相同的前提下，下一页查询的所有记录，其IdCdr值必然大于上一页末记录的IdCdr。如果我们每次查询后，获得了末记录的IdCdr值，然后在下一次查询时，引入该条件，得到的结果必然是根据条件查询出来的下一页结果。方法如下：<br>set rowcount 每页记录数<br>select * from CtsiInfoRecord where IdCdr &gt; 上一页末记录IdCdr值 order by IdCdr<br>如果是上一页查询，则刚好相反，需要获得下一页首记录的IdCdr值：<br>set rowcount 每页记录数<br>select * from CtsiInfoRecord where IdCdr &lt; 下一页首记录IdCdr值<br>注：如果查询首页，则将IdCdr值条件删掉。<br>&nbsp;&nbsp;&nbsp; 如果查询末页，在删掉IdCdr值条件的同时，将排序改为降序的方式。</p>
<p><strong>2.3 两种方案实现方式的比较<br></strong><br>从Sql语句的角度来看，方案二更简单，也更容易理解。不过相对麻烦的就是需要每次去获得上一页末记录的IdCdr值（或下一页首记录IdCdr值）。前一次查询时，还需要记录首记录和末记录值。另外，方案二是根据上页首记录（或末记录）IdCdr值作为查询条件，它与具体的页数无关，因此，无法直接定位显示某页的结果，除非在之前将各页的首、末记录放到数组中保存下来，但这就要耗费一定的时间。一旦改变了查询条件，数组中保存的值，还需要更新。<br>方案一，Sql语句较复杂，但并不影响查询的程序。同时，由于其引入了临时表机制，该临时表是放到tempdb数据库中。如果多次查询，则必然会多次删除和创建临时表，带来的结果是tempdb数据库的日志会不段增长。同时由于日志的增长，也会影响使用临时表的性能。如果要具体实现，必须在上述的sql语句中，实时地清除tempdb库中的日志。<br>总体说来，方案一，Sql语句复杂，但程序设计简单；而方案二则刚刚相反。</p>
<p><strong>2.4 两种方案性能的比较</strong><br><br>由于上述两种方案都是对sql语句进行改进，因此我在测试时，直接运行sql语句来计算其查询所消耗的时间。如果是在具体的业务界面中，还应加上一些前置、后置操作的耗时，尤其是界面显示结果集的时间。但由于每页记录数相对较小，返回的结果集也较小，因此这些耗时可以忽略不计。<br>另外，测试记录的时间只包括了查询语句的时间（方案一还包括了建立临时表，并插入记录的时间），没有包含计算符合条件的总记录数时间。</p>
<p><strong>2.4.1 测试环境<br></strong><br>操作系统：Windows<br>数据库：Sql Server 2000<br>访问方式：本机直接访问数据库（非客户端访问方式）<br>总记录数：9,001,789条<br>每页记录数：2,000条</p>
<p><strong>2.4.2 测试结果<br><br>
<table cellSpacing=0 cellPadding=0 border=1>
    <tbody>
        <tr>
            <td vAlign=top width=211>
            <p>&nbsp;</p>
            </td>
            <td vAlign=top width=180>
            <p align=center><strong><span>方案一（耗时：秒）</span></strong><strong></strong></p>
            </td>
            <td vAlign=top width=177>
            <p align=center><strong><span>方案二（耗时：秒）</span></strong><strong></strong></p>
            </td>
        </tr>
        <tr>
            <td vAlign=top width=211>
            <p><span>第</span><span>1</span><span>页</span></p>
            </td>
            <td vAlign=top width=180>
            <p align=center><span>0.1~0.2</span></p>
            </td>
            <td vAlign=top width=177>
            <p align=center><span>0.1~0.2</span></p>
            </td>
        </tr>
        <tr>
            <td vAlign=top width=211>
            <p><span>第</span><span>3</span><span>页（</span><span>4,000</span><span>条记录后）</span></p>
            </td>
            <td vAlign=top width=180>
            <p align=center><span>11</span></p>
            </td>
            <td vAlign=top width=177>
            <p align=center><span>0.1~0.2</span></p>
            </td>
        </tr>
        <tr>
            <td vAlign=top width=211>
            <p><span>第</span><span>10</span><span>页（</span><span>20,000</span><span>条记录后）</span></p>
            </td>
            <td vAlign=top width=180>
            <p align=center><span>12</span></p>
            </td>
            <td vAlign=top width=177>
            <p align=center><span>0.1~0.2</span></p>
            </td>
        </tr>
        <tr>
            <td vAlign=top width=211>
            <p><span>第</span><span>50</span><span>页（</span><span>100,000</span><span>条记录后）</span></p>
            </td>
            <td vAlign=top width=180>
            <p align=center><span>14</span></p>
            </td>
            <td vAlign=top width=177>
            <p align=center><span>0.1~0.2</span></p>
            </td>
        </tr>
        <tr>
            <td vAlign=top width=211>
            <p><span>第</span><span>100</span><span>页（</span><span>200,000</span><span>条记录后）</span></p>
            </td>
            <td vAlign=top width=180>
            <p align=center><span>15</span></p>
            </td>
            <td vAlign=top width=177>
            <p align=center><span>0.1~0.2</span></p>
            </td>
        </tr>
        <tr>
            <td vAlign=top width=211>
            <p><span>第</span><span>1000</span><span>页（</span><span>2,000,000</span><span>条记录后）</span></p>
            </td>
            <td vAlign=top width=180>
            <p align=center><span>47</span></p>
            </td>
            <td vAlign=top width=177>
            <p align=center><span>0.1~0.2</span></p>
            </td>
        </tr>
    </tbody>
</table>
</strong></p>
<p>&nbsp;从测试结果看，方案二在性能上有非常大的优势。由于IdCdr建立了索引，且该值为int类型，因此，查询条件中，IdCdr具体的值对查询没有影响。而方案一由于是通过临时表方式，且临时表的记录数会根据页数的增加而增加，这在一定程度上影响了查询性能。（注：如果是在Sql Server中，且数据量不太大，选择方案一并采用top的方法还是比较优秀的。一般的网页设计时，分页查询均采用这种方式）不过，如果我们不仅是实现上、下页翻页，还要实现指定页查询，则第二种方案由于需要获得所有页首、末记录的IdCdr值，故在查询之前的初始化过程需要耗费较长的时间。</p>
<p>&nbsp;&nbsp;&nbsp; 两种方案，各有优势。另外，对于分页查询时，我们还可以使用游标来实现。但是如果是多种数据库，使用游标的方式不便于数据库脚本的移植，应该慎用</p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140637"><font color=#0066aa>#</font></a>&nbsp;<a name=140637></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 17:13 </font></span><a id=AjaxHolder_Comments_CommentList_ctl00_NameLink target=_blank>none</a> </h4>
<p>请问： <br><br>数据查询的时候数据会更新吗？ <br><br>如果更新的话，更新数据的入口点，你可以控制吗？也即使你是否能让所有更新数据的入口都在某个程序中？ <br><br>最重要的一点，需要对所有数据进行动态排序吗？（就是用户会更换排序条件）如果会，那么提供排序的列有几个？&nbsp;&nbsp;<a onclick='return SetReplyAuhor("none")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=none" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl00_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl00$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl00_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140660"><font color=#0066aa>#</font></a>&nbsp;<a name=140660></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 17:33 </font></span><a id=AjaxHolder_Comments_CommentList_ctl01_NameLink href="http://www.cnblogs.com/tintown" target=_blank><font color=#0066aa>听棠.NET</font></a> </h4>
<p>楼主的思考是有一定的道理的。 <br>我一般是使用GUID做主键的。那怎么办呢？？&nbsp;&nbsp;<a onclick='return SetReplyAuhor("听棠.NET")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=%e5%90%ac%e6%a3%a0.NET" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl01_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl01$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl01_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140669"><font color=#0066aa>#</font></a>&nbsp;<a name=140669></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 17:47 </font></span><a id=AjaxHolder_Comments_CommentList_ctl02_NameLink target=_blank>lay</a> </h4>
<p>方案2确实效率高，但对表设计有要求，不光是程序上的处理&nbsp;&nbsp;<a onclick='return SetReplyAuhor("lay")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=lay" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl02_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl02$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl02_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140684"><font color=#0066aa>#</font></a>&nbsp;<a name=140684></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 18:10 </font></span><a id=AjaxHolder_Comments_CommentList_ctl03_NameLink href="http://www.cnblogs.com/austinleng" target=_blank><font color=#0066aa>Austin leng</font></a> </h4>
<p>方案2的确可以达到较高的效率，也是可行的。 <br>但是，使用频率应该是不高，道理其实只有一个，因为大多数的排序都不是按主键来排序的。 <br>所以，分页存储过程，我认为，使用得最多的倒是SQL语句拼凑的方法来分页，和使用表变量（或临时表）的方法来分页的最多。 <br>&nbsp;&nbsp;<a onclick='return SetReplyAuhor("Austin leng")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=Austin+leng" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl03_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl03$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl03_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140771"><font color=#0066aa>#</font></a>&nbsp;<a name=140771></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 20:44 </font></span><a id=AjaxHolder_Comments_CommentList_ctl04_NameLink href="http://www.cnblogs.com/Xrinehart" target=_blank><font color=#0066aa>黄金狮子旗下</font></a> </h4>
<p>插临时表的办法分页效率不好。还是SQL语句嵌套方式好。&nbsp;&nbsp;<a onclick='return SetReplyAuhor("黄金狮子旗下")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=%e9%bb%84%e9%87%91%e7%8b%ae%e5%ad%90%e6%97%97%e4%b8%8b" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl04_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl04$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl04_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140794"><font color=#0066aa>#</font></a>&nbsp;<a name=140794></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 21:22 </font></span><a id=AjaxHolder_Comments_CommentList_ctl05_NameLink href="http://www.cnblogs.com/dudu" target=_blank><font color=#0066aa>dudu</font></a> </h4>
<p>博客园采用的是方案一, 看来需要改进一下。&nbsp;&nbsp;<a onclick='return SetReplyAuhor("dudu")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=dudu" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl05_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl05$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl05_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140826"><font color=#0066aa>#</font></a>&nbsp;<a name=140826></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 22:23 </font></span><a id=AjaxHolder_Comments_CommentList_ctl06_NameLink href="http://www.cnblogs.com/wayfarer" target=_blank><font color=#0066aa>wayfarer</font></a> </h4>
<p>None问得很有道理。在查询的时候，如果有最新的更新，是否能显示，要看这条新记录插入的时机。然而由于每页查询耗时比较少，且每次查询都会根据条件和排序进行select，因此影响不会太大。 <br><br>然而方案二的排序方式，必然要受到排序的影响。至少在进行排序时，IdCdr必须是排序的主字段。 <br><br>另外，GUID使用这种方式是可以的。虽然GUID是随机产生的，但它仍然有顺序。只是比起Identity字段，要慢一些。而且如果有实时插入的新纪录的话，可能会在查询的时候会漏掉。 <br><br>@dudu <br>如果记录不时太多，比如达到百万级。且使用Sql Server的话，我觉采用方案一结合top就够了。&nbsp;&nbsp;<a onclick='return SetReplyAuhor("wayfarer")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=wayfarer" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl06_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl06$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl06_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140868"><font color=#0066aa>#</font></a>&nbsp;<a name=140868></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-19 23:39 </font></span><a id=AjaxHolder_Comments_CommentList_ctl07_NameLink href="http://www.cnblogs.com/jimmyhsu" target=_blank><font color=#0066aa>湘南和也</font></a> </h4>
<p>楼主的第二个方案和我用的完全一样。这个写起来很方便，我就一直这么用了。 <br><br>但如果真从数据库性能来讲它并不是很好的，特别是用到了not in的时候，最好连in都不要用。 <br><br>我目前正在思考怎样优化这种sql语句。&nbsp;&nbsp;<a onclick='return SetReplyAuhor("湘南和也")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=%e6%b9%98%e5%8d%97%e5%92%8c%e4%b9%9f" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl07_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl07$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl07_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#140954"><font color=#0066aa>#</font></a>&nbsp;<a name=140954></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-20 08:45 </font></span><a id=AjaxHolder_Comments_CommentList_ctl08_NameLink target=_blank>none</a> </h4>
<p>楼主是否考虑过在程序中实现缓存呢？ <br><br>比如说：把IdCdr索引完全的读到程序中，缓存起来（比如ArrayList，当然有泛型更好），下次要查，在这里得到ID号，然后根据ID号直接到数据库查出数据，数据总记录数量也可以统计缓存中的索引数量得出，并且做任何更新的时候，都需要同步这个索引，如果有多个排序，那就给每个排序维护一个索引，楼主是否也考虑到内存的问题了，是的，这样相当消耗内存，我做过，大约1000W的ID存于ArrayList中会使用大约160M的内存，如果有泛型就好很多了！不过就便是160M，对于1000W数据的应用来说又算得了什么呢？这样做的话，在1600+512内存的情况下，任何一页数据的获取都只需要15-60ms就够了，而对用户来说，完全察觉不到。。。&nbsp;&nbsp;<a onclick='return SetReplyAuhor("none")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=none" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl08_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl08$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl08_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#141024"><font color=#0066aa>#</font></a>&nbsp;<a name=141024></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-20 09:27 </font></span><a id=AjaxHolder_Comments_CommentList_ctl09_NameLink href="http://www.cnblogs.com/wayfarer" target=_blank><font color=#0066aa>wayfarer</font></a> </h4>
<p>@None <br><br>正如你说的，还是内存的问题。消耗这么多内存，以换取几毫秒，或者几秒钟的性能，不划算。 <br><br>因为程序是作为客户端安装在客户机上，机器的配置也不会太高。&nbsp;&nbsp;<a onclick='return SetReplyAuhor("wayfarer")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=wayfarer" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl09_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl09$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl09_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#141050"><font color=#0066aa>#</font></a>&nbsp;<a name=141050></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-20 09:52 </font></span><a id=AjaxHolder_Comments_CommentList_ctl10_NameLink target=_blank>none</a> </h4>
<p>恩那，我觉得如果数据库在服务器上呢，就比较适合，如果数据库在客户端确实就没有必要了，如果客户端只负责显示，而服务器上负责业务逻辑，可能也比较适合实现。&nbsp;&nbsp;<a onclick='return SetReplyAuhor("none")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=none" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl10_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl10$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl10_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#141051"><font color=#0066aa>#</font></a>&nbsp;<a name=141051></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-20 09:53 </font></span><a id=AjaxHolder_Comments_CommentList_ctl11_NameLink target=_blank>none</a> </h4>
<p>对了，关键在于，这样实现之后，数据库服务器的开销将会大大降低，我记得我测试的时候CPU基本没超过2%过，如果不用，则一直在40%以上&nbsp;&nbsp;<a onclick='return SetReplyAuhor("none")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=none" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl11_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl11$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl11_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#141063"><font color=#0066aa>#</font></a>&nbsp;<a name=141063></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-20 10:11 </font></span><a id=AjaxHolder_Comments_CommentList_ctl12_NameLink target=_blank>cmoremore</a> </h4>
<p>这个对数据库表又要求吧？我的表没有唯一标示的字段怎么办？&nbsp;&nbsp;<a onclick='return SetReplyAuhor("cmoremore")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=cmoremore" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl12_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl12$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl12_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#141195"><font color=#0066aa>#</font></a>&nbsp;<a name=141195></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-20 12:00 </font></span><a id=AjaxHolder_Comments_CommentList_ctl13_NameLink target=_blank>none</a> </h4>
<p>呵呵，这个是有假设基础的，假设基础就是肯定有唯一标识&nbsp;&nbsp;<a onclick='return SetReplyAuhor("none")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=none" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl13_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl13$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl13_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#141383"><font color=#0066aa>#</font></a>&nbsp;<a name=141383></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-04-20 16:14 </font></span><a id=AjaxHolder_Comments_CommentList_ctl14_NameLink target=_blank>Da.Feng</a> </h4>
<p>哎，偶看来已经固步自封了&nbsp;&nbsp;<a onclick='return SetReplyAuhor("Da.Feng")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=Da.Feng" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl14_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl14$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl14_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#171263"><font color=#0066aa>#</font></a>&nbsp;<a name=171263></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-06-09 18:01 </font></span><a id=AjaxHolder_Comments_CommentList_ctl15_NameLink target=_blank>BOBO</a> </h4>
<p>我的表也没有唯一的标识符，我准备在方法二的基础上＋一个identity的临时表来实现，不知道可不可以？&nbsp;&nbsp;<a onclick='return SetReplyAuhor("BOBO")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=BOBO" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl15_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl15$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl15_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#184209"><font color=#0066aa>#</font></a>&nbsp;<a name=184209></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-06-30 21:23 </font></span><a id=AjaxHolder_Comments_CommentList_ctl16_NameLink target=_blank>er</a> </h4>
<p>er&nbsp;&nbsp;<a onclick='return SetReplyAuhor("er")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=er" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl16_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl16$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl16_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#184213"><font color=#0066aa>#</font></a>&nbsp;<a name=184213></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-06-30 21:32 </font></span><a id=AjaxHolder_Comments_CommentList_ctl17_NameLink target=_blank>iww</a> </h4>
<p>各位大侠： <br>我有一个问题，请问在sql server中百万数据量的表上 <br><br>针对一个或两个字段进行LIKE条件检索的时间大概是多少亚？ <br><br>谢谢！ <br><br>例如： <br>select title,author,abstract <br>from books <br>where ((books.abstract like '%你好%') <br>and (books.titile like '%题目%')) <br><br><br>我这里没有这种规模的数据库，望各位大侠帮忙一下。 <br>能把结果发到我的信箱吗？ happyiww◎126.com&nbsp;&nbsp;<a onclick='return SetReplyAuhor("iww")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=iww" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl17_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl17$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl17_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#218330"><font color=#0066aa>#</font></a>&nbsp;<a name=218330></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-08-19 11:41 </font></span><a id=AjaxHolder_Comments_CommentList_ctl18_NameLink target=_blank>爱天</a> </h4>
<p>我是初学者,请作者帮帮忙,我现在做了一个简单的客户资料档案系统,请问怎样才能让他有外部连接呀,谢谢指教.&nbsp;&nbsp;<a onclick='return SetReplyAuhor("爱天")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=%e7%88%b1%e5%a4%a9" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl18_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl18$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl18_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#218332"><font color=#0066aa>#</font></a>&nbsp;<a name=218332></a>re: 怎样改进数据库的查询性能? <span><font color=#999999>2005-08-19 11:42 </font></span><a id=AjaxHolder_Comments_CommentList_ctl19_NameLink target=_blank>爱天</a> </h4>
<p>我的QQ:42575475 邮箱:wang7261712@126.com&nbsp;&nbsp;<a onclick='return SetReplyAuhor("爱天")' href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#post"><font color=#0066aa>回复</font></a>&nbsp;&nbsp;<a title=查看该作者发表过的评论 href="http://www.cnblogs.com/comment?author=%e7%88%b1%e5%a4%a9" target=_blank><font color=#0066aa>更多评论</font></a> <a id=AjaxHolder_Comments_CommentList_ctl19_DeleteLink href="javascript:__doPostBack('AjaxHolder$Comments$CommentList$ctl19$DeleteLink','')"></a>&nbsp;&nbsp;<a id=AjaxHolder_Comments_CommentList_ctl19_EditLink></a> </p>
<h4><a title="permalink: re: 怎样改进数据库的查询性能?" href="http://www.cnblogs.com/wayfarer/archive/2005/04/19/140609.html#593868"><font color=#0066aa>#</font></a>&nbsp;<a name=593868></a>re: 怎样改进数据库的查询性能?<a name=Post></a> <span><font color=#999999>2006-12-15 23:26 </font></span><a id=AjaxHolder_Comments_CommentList_ctl20_NameLink href="http://yunhuasheng.cnblogs.com/" target=_blank><font color=#0066aa>yunhuasheng</font></a> </h4>
<p>写的好<br></p>
<img src ="http://www.blogjava.net/yesjoy/aggbug/117036.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yesjoy/" target="_blank">★yesjoy★</a> 2007-05-12 21:00 <a href="http://www.blogjava.net/yesjoy/articles/117036.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>