﻿<?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-风人园-文章分类-Database</title><link>http://www.blogjava.net/wuxufeng8080/category/10539.html</link><description>弱水三千，只取一瓢，便能解渴；佛法无边，奉行一法，便能得益。</description><language>zh-cn</language><lastBuildDate>Sun, 29 Jul 2012 00:05:06 GMT</lastBuildDate><pubDate>Sun, 29 Jul 2012 00:05:06 GMT</pubDate><ttl>60</ttl><item><title>查询数据库中表的数据行数</title><link>http://www.blogjava.net/wuxufeng8080/articles/383527.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Thu, 19 Jul 2012 10:10:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/383527.html</guid><wfw:comment>http://www.blogjava.net/wuxufeng8080/comments/383527.html</wfw:comment><comments>http://www.blogjava.net/wuxufeng8080/articles/383527.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wuxufeng8080/comments/commentRss/383527.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wuxufeng8080/services/trackbacks/383527.html</trackback:ping><description><![CDATA[<!--StartFragment -->

<div>create&nbsp;proc&nbsp;ss<br />as<br />set&nbsp;nocount&nbsp;on<br />create&nbsp;table&nbsp;TableSpace<br />(<br />TableName&nbsp;varchar(50),<br />RowsCount&nbsp;char(11),<br />Reserved&nbsp;varchar(18),<br />Data&nbsp;varchar(18),<br />Index_size&nbsp;varchar(18),<br />Unused&nbsp;varchar(18)&nbsp;<br />)<br />declare&nbsp;@sql&nbsp;varchar(500)<br />declare&nbsp;@TableName&nbsp;varchar(50)<br />declare&nbsp;Cursor1&nbsp;Cursor<br />for&nbsp;<br />select&nbsp;name&nbsp;from&nbsp;sysobjects&nbsp;where&nbsp;xtype='U'<br />open&nbsp;Cursor1<br />fetch&nbsp;next&nbsp;from&nbsp;Cursor1&nbsp;into&nbsp;@TableName<br />while&nbsp;@@fetch_status&nbsp;=&nbsp;0<br />begin<br />set&nbsp;@sql&nbsp;=&nbsp;'insert&nbsp;into&nbsp;TableSpace&nbsp;'<br />set&nbsp;@sql&nbsp;=&nbsp;@sql&nbsp;+&nbsp;'&nbsp;exec&nbsp;sp_spaceused&nbsp;'''+&nbsp;@TableName&nbsp;+&nbsp;'''&nbsp;'<br />exec&nbsp;(@sql)<br />fetch&nbsp;next&nbsp;from&nbsp;Cursor1&nbsp;into&nbsp;@TableName<br />end<br />close&nbsp;Cursor1<br />deallocate&nbsp;Cursor1<br />select&nbsp;tableName,rowscount&nbsp;from&nbsp;TableSpace&nbsp;where&nbsp;tableName&lt;&gt;'TableSpace'<br />order&nbsp;by&nbsp;rowscount&nbsp;desc<br />drop&nbsp;table&nbsp;TableSpace<br />go<br />exec&nbsp;ss </div><img src ="http://www.blogjava.net/wuxufeng8080/aggbug/383527.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2012-07-19 18:10 <a href="http://www.blogjava.net/wuxufeng8080/articles/383527.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sql server2000数据分页的两种方法</title><link>http://www.blogjava.net/wuxufeng8080/articles/279502.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Tue, 02 Jun 2009 00:30:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/279502.html</guid><wfw:comment>http://www.blogjava.net/wuxufeng8080/comments/279502.html</wfw:comment><comments>http://www.blogjava.net/wuxufeng8080/articles/279502.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wuxufeng8080/comments/commentRss/279502.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wuxufeng8080/services/trackbacks/279502.html</trackback:ping><description><![CDATA[如果一个查询的结果数据量巨大，就需要数据查询的时候就分页，每次只返回其中一页<br />
<br />
<strong>第一种方法：</strong><br />
思路是根据页号取出两部分数据，比如取出前90条，然后取出前100条，然后比较取出两次结果的差集。<br />
<br />
在30万条记录的情况下，如果只分100页（结果有10000条记录），大约需要1分半钟。索引建得好的话，1分钟左右。<br />
<br />
<br />
//select * from //这一句是不能修改的了，因为它是从结果中读取，所以必须要用*<br />
//(select top @h_count (@filedlist) from @tableName .....) as big //取出符合条件的上限的记录<br />
//where <br />
//big.guid&nbsp;&nbsp; //这里是关键，根据主键从下限结果中过滤掉重复的记录（只留下不同的数据，也就是求交集）<br />
//not in <br />
//(select top @l_count guid from @table .....)//下限<br />
//order @orderby&nbsp; //原来的格式，这里只保留了orderby之后的，应该保留条件之后所有的，包括gruopby什么的<br />
<br />
函数类似如此：<br />
&nbsp;&nbsp;public string MakeSqlPager(string sourceSql,int pageIndex)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;//使用默认页面大小<br />
&nbsp;&nbsp;&nbsp;string orderbyStr=sourceSql.Substring(sourceSql.ToLower().IndexOf("order by"));<br />
&nbsp;&nbsp;&nbsp;int index=sourceSql.ToLower().IndexOf("select");<br />
&nbsp;&nbsp;&nbsp;string bigRes="("+ sourceSql.Insert(index+6," top "+((pageIndex+1)*_pageSize).ToString()+" ")+") as big";<br />
&nbsp;&nbsp;&nbsp;string smallRes="("+ sourceSql.Insert(index+6," top "+(pageIndex*_pageSize).ToString()+" ")+")";<br />
&nbsp;&nbsp;&nbsp;return "select * from "+bigRes+" where big.guid not in "+smallRes+" "+orderbyStr;<br />
&nbsp;&nbsp;}<br />
<br />
这种方法还可以改进，就是第二次取过滤时从第一个的结果里面过滤。<br />
<br />
<strong>第二种方法：</strong><br />
掐头去尾，程序还没写<br />
SELECT * FROM<br />
(<br />
&nbsp; SELECT TOP 100 * FROM<br />
&nbsp; (<br />
&nbsp;&nbsp;&nbsp; SELECT TOP 100000 * FROM pagetest ORDER BY regt ASC<br />
&nbsp;&nbsp; ) as a<br />
&nbsp; ORDER BY regt desc<br />
) as b<br />
&nbsp;ORDER BY regt ASC<br />
<br />
测试了一下，大约用时间29秒。<br />
<br />
<strong>比较：<br />
</strong>第一种方法的效率很低，猜测是因为多次需要循环比较，时间复杂度要高一个等级。比如，这种方法的响应时间和所取得的页号有很大关系。<br />
第二种方法还是可以接受的，和页号无关，但是也需要两次比较<br />
<br />
网上还有使用比较ID的方法的，但是不是所有的表都有ID，即使有，也不一定是int类型的。<br />
还有用存储过程创建临时表的，我还没有测试效率如何<br />
<br />
sqlserver里面没有rownum 这个功能（最新的2005beta2版本据说有了，Oracle里面有），所以一次比较就能分页的算法还真不好写
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/279502.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2009-06-02 08:30 <a href="http://www.blogjava.net/wuxufeng8080/articles/279502.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL SERVER定时备份数据库</title><link>http://www.blogjava.net/wuxufeng8080/articles/213835.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Wed, 09 Jul 2008 23:39:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/213835.html</guid><wfw:comment>http://www.blogjava.net/wuxufeng8080/comments/213835.html</wfw:comment><comments>http://www.blogjava.net/wuxufeng8080/articles/213835.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wuxufeng8080/comments/commentRss/213835.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wuxufeng8080/services/trackbacks/213835.html</trackback:ping><description><![CDATA[<p align="left"><span>点击<span> "</span>管理<span>"--"sql server</span>代理<span>"--"</span>作业<span>"</span></span></p>
<p align="left"><span>新建作业：</span></p>
<p align="left"><span>常规选项卡里<span>,"</span>名称<span>"</span>填写<span>"</span>定时备份数据库<span>"</span>，<span>"</span>启用<span>"</span>前面选<span>"</span>对勾<span>"</span>，<span>"</span>以本地服务器为目标<span>"</span>，</span></p>
<p align="left"><span>&nbsp;"</span><span>分类<span>"</span>选择<span>"</span>数据库服务<span>"</span>，</span></p>
<p align="left"><span>步骤选项卡里，新建步骤，步骤名：备份数据库；类型：<span>Transact-SQL</span>脚本<span>(TSQL);</span></span></p>
<p align="left"><span>&nbsp;</span><span>数据库：要备份的数据库；</span></p>
<p align="left"><span>&nbsp;</span><span>命令：<span> </span></span></p>
<p align="left"><span>declare @filename varchar (1000)</span></p>
<p align="left"><span>set @filename='E:\</span><span>数据库备份<span>\book'+convert(char(10),getdate(),112)</span></span></p>
<p align="left"><span>print @filename</span></p>
<p align="left"><span>BACKUP DATABASE [book] TO DISK = @filename WITH NOINIT , NOUNLOAD , NAME = N'</span><span>备份<span>', NOSKIP , STATS = 10, </span></span></p>
<p align="left"><span>NOFORMAT </span></p>
<p align="left"><span>调度选项卡里，新建调度，名称<span>:</span>定时备份数据库<span>; </span>调度类型<span>:</span>反复出现；</span></p>
<p><span>通知选项卡里，写入<span>windows</span>应用程序事件日至。当作业失败时。</span></p>
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/213835.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2008-07-10 07:39 <a href="http://www.blogjava.net/wuxufeng8080/articles/213835.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL Server 2005新增功能--Pivot &amp; UnPivot</title><link>http://www.blogjava.net/wuxufeng8080/articles/110072.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Thu, 12 Apr 2007 01:33:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/110072.html</guid><wfw:comment>http://www.blogjava.net/wuxufeng8080/comments/110072.html</wfw:comment><comments>http://www.blogjava.net/wuxufeng8080/articles/110072.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wuxufeng8080/comments/commentRss/110072.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wuxufeng8080/services/trackbacks/110072.html</trackback:ping><description><![CDATA[PIVOT会将剩下的字段，做GROUP BY 的动作<br>如下示例<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">SELECT</span><span style="COLOR: #000000">&nbsp;DATE,&nbsp;A.G,&nbsp;A.I,&nbsp;A.V,&nbsp;A.O,&nbsp;A.SC,&nbsp;A.LR,&nbsp;A.PR,&nbsp;A.HL&nbsp;</span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000">&nbsp;<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>(<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000">&nbsp;date,PNLID,&nbsp;judge&nbsp;</span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000">&nbsp;dbo.RCI_LOI2MOVEMENT<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>)B<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>PIVOT&nbsp;(</span><span style="COLOR: #ff00ff">COUNT</span><span style="COLOR: #000000">(PNLID)&nbsp;</span><span style="COLOR: #0000ff">FOR</span><span style="COLOR: #000000">&nbsp;judge&nbsp;</span><span style="COLOR: #808080">IN</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>(G,&nbsp;I,&nbsp;V,&nbsp;O,&nbsp;SC,&nbsp;LR,&nbsp;PR,&nbsp;HL&nbsp;))&nbsp;A<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">WHERE</span><span style="COLOR: #000000">&nbsp;DATE&nbsp;</span><span style="COLOR: #808080">&gt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">2007-4-5</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #808080">AND</span><span style="COLOR: #000000">&nbsp;DATE&nbsp;</span><span style="COLOR: #808080">&lt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">2007-4-11</span><span style="COLOR: #ff0000">'</span></div>
自动会以date作为GROUP BY 的栏位
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/110072.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2007-04-12 09:33 <a href="http://www.blogjava.net/wuxufeng8080/articles/110072.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL Server 2005新增功能--Row_NUMBER() 分页查询</title><link>http://www.blogjava.net/wuxufeng8080/articles/110050.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Thu, 12 Apr 2007 00:43:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/110050.html</guid><wfw:comment>http://www.blogjava.net/wuxufeng8080/comments/110050.html</wfw:comment><comments>http://www.blogjava.net/wuxufeng8080/articles/110050.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wuxufeng8080/comments/commentRss/110050.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wuxufeng8080/services/trackbacks/110050.html</trackback:ping><description><![CDATA[我的测试代码<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">SELECT</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>(<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>&nbsp;</span><span style="COLOR: #0000ff">SELECT</span><span style="COLOR: #000000">&nbsp;ROW_NUMBER()&nbsp;</span><span style="COLOR: #0000ff">OVER</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">ORDER</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">BY</span><span style="COLOR: #000000">&nbsp;DATE)&nbsp;ROWNUM,&nbsp;</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000">&nbsp;BAS_CALENDAR<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top>)&nbsp;CAL<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">WHERE</span><span style="COLOR: #000000">&nbsp;ROWNUM&nbsp;</span><span style="COLOR: #808080">BETWEEN</span><span style="COLOR: #000000">&nbsp;(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">50</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #808080">+</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #808080">AND</span><span style="COLOR: #000000">&nbsp;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">50</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">2</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">ORDER</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">BY</span><span style="COLOR: #000000">&nbsp;DATE&nbsp;</span><span style="COLOR: #0000ff">ASC</span></div>
<br>开发圣经上的示例代码<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">CREATE</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">PROC</span><span style="COLOR: #000000">&nbsp;spGetPages2&nbsp;</span><span style="COLOR: #008000">@iRowCount</span><span style="COLOR: #000000">&nbsp;</span><span style="FONT-WEIGHT: bold; COLOR: #000000">INT</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #008000">@iRowPageNo</span><span style="COLOR: #000000">&nbsp;</span><span style="FONT-WEIGHT: bold; COLOR: #000000">INT</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">AS</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">SELECT</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000">&nbsp;(<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">SELECT</span><span style="COLOR: #000000">&nbsp;ROW_NUMBER()&nbsp;</span><span style="COLOR: #0000ff">OVER</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">ORDER</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">BY</span><span style="COLOR: #000000">&nbsp;ProductID&nbsp;</span><span style="COLOR: #0000ff">ASC</span><span style="COLOR: #000000">)&nbsp;RowNum,&nbsp;</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000">&nbsp;Production.Product&nbsp;)&nbsp;OrderData<br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">WHERE</span><span style="COLOR: #000000">&nbsp;RowNum&nbsp;</span><span style="COLOR: #808080">BETWEEN</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">@iRowCount</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #008000">@iPageNo</span><span style="COLOR: #808080">-</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #808080">+</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #808080">and</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #008000">@iRowCount</span><span style="COLOR: #808080">*</span><span style="COLOR: #008000">@iPageNo</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">ORDER</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">BY</span><span style="COLOR: #000000">&nbsp;ProductID&nbsp;</span><span style="COLOR: #0000ff">ASC</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">GO</span><span style="COLOR: #000000"><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.blogjava.net/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">EXEC</span><span style="COLOR: #000000">&nbsp;spGetPages2&nbsp;</span><span style="FONT-WEIGHT: bold; COLOR: #800000">10</span><span style="COLOR: #000000">,</span><span style="FONT-WEIGHT: bold; COLOR: #800000">20</span></div>
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/110050.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2007-04-12 08:43 <a href="http://www.blogjava.net/wuxufeng8080/articles/110050.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最优化的ms sql server分页sql语句(ZT)</title><link>http://www.blogjava.net/wuxufeng8080/articles/104651.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Mon, 19 Mar 2007 01:25:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/104651.html</guid><description><![CDATA[特点：一次查询，数据库只返回一页的数据。而不是取出所有的数据。<br />说明：
<p>pagesize： 每页显示记录数<br />cureentpage：当前页数</p><p>select * from (  select TOP pagesize * FROM ( SELECT TOP pagesize*cureentpage  * from user_table  ORDER BY id ASC ) as aSysTable  ORDER BY id DESC ) as bSysTable  ORDER BY id ASC</p><p><br />例子说明：</p><p>假如数据库表如下：</p><p>user_table:</p><p>id:主键，自增<br />username:字符<br />password:字符</p><p>假设有80条记录，每页显示10条记录，id 从1到80</p><p><br />现在按照id升序排列取出第三页的数据应该为：所取得记录的id 应该为 21到30。</p><p>这时该语句应该为：</p><p>select * from (  select TOP 10 * FROM ( SELECT TOP 30  * from user_table  ORDER BY id ASC ) as aSysTable  ORDER BY id DESC ) as bSysTable  ORDER BY id ASC</p><p>原理如下：</p><p>先按照id从小到大升序取出30条记录（3*10）,也就是：id 在 1-30 之间的记录 （SELECT TOP 30  * from user_table  ORDER BY id ASC）</p><p>然后按照ID降序排列这30条记录，得到记录为id 在：从30到 1  </p><p>然后在这些30条记录中取出前10条记录：取得的记录为：id 在30-21之间。这就是我们需要的数据，但这时是按照降序排列的，不符合要求。</p><p>最后在重新排序得到最终我们需要的数据。id在21-30之间。</p><img src ="http://www.blogjava.net/wuxufeng8080/aggbug/104651.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2007-03-19 09:25 <a href="http://www.blogjava.net/wuxufeng8080/articles/104651.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Paging (MS SQL Server/Oracle)</title><link>http://www.blogjava.net/wuxufeng8080/articles/99471.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Mon, 12 Feb 2007 07:57:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/99471.html</guid><description><![CDATA[
		<p>虽然 DataGrid 控件自己带了一个分页处理机制，但它是将符合查询条件的所有记录读入内存，然后进行分页显示的。随着符合条件的记录数目增多，就会出现运行效率问题，或者至少是资源的利用率下降。</p>
		<p>下面的代码示例都以下面的表结构为准：</p>
		<p align="center">　 
</p>
		<table style="BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1">
				<tbody>
						<tr>
								<td width="30" bgcolor="#99ccff">　</td>
								<td width="100" bgcolor="#99ccff">Articles 表</td>
								<td width="150" bgcolor="#99ccff">SQL Server 类型</td>
								<td width="150" bgcolor="#99ccff">Oracle 类型</td>
						</tr>
						<tr>
								<td>PK</td>
								<td bgcolor="#dfefff">Id</td>
								<td>int (自增)</td>
								<td>number(9) (插入时在当前最大值上加1)</td>
						</tr>
						<tr>
								<td>　</td>
								<td bgcolor="#dfefff">Author</td>
								<td>nvarchar(10)</td>
								<td>nvarchar2(10)</td>
						</tr>
						<tr>
								<td>　</td>
								<td bgcolor="#dfefff">Title</td>
								<td>nvarchar(50)</td>
								<td>nvarchar2(50)</td>
						</tr>
						<tr>
								<td>　</td>
								<td bgcolor="#dfefff">PubTime</td>
								<td>datetime</td>
								<td>date</td>
						</tr>
				</tbody>
		</table>
		<p>SQL Server / Access 等微软产品中，我们通常的自定义分页有两种思路：</p>
		<p>一种是以 ASP.NET Forum 为代表的、“临时表”方法：即在存储过程中建立一个临时表，该临时表包含一个序号字段（1，2，3，....）以及表的主键（其他能够唯一确定一行记录的字段也是可以的）字段。存储过程可能如下：<font color="#0000ff">（编号 SS1）</font></p>
		<div class="pbcode">CREATE Procedure GetAllArticles_Paged<br />(<br />     @PageIndex int,<br />     @PageSize int,<br />     @TotalRecords out int,<br />     @TotalPages out int<br />)<br />AS<br /><br />DECLARE @PageLowerBound int<br />DECLARE @PageUpperBound int<br /><br />-- Set the page bounds<br />SET @PageLowerBound = @PageSize * @PageIndex<br />SET @PageUpperBound = @PageLowerBound + @PageSize + 1<br /><br />-- Create a temp table to store the select results<br />CREATE TABLE #tmp<br />(<br />     RecNo int IDENTITY (1, 1) NOT NULL,<br />     ArticleID int<br />)<br /><br />INSERT INTO #tmp<br />     SELECT [ID]<br />     FROM Articles<br />     ORDER BY PubTime DESC<br /><br />SELECT A.*<br />FROM Articles A (nolock), #tmp T<br />WHERE A.ID = T.ArticleID AND<br />     T.RecNo &gt; @PageLowerBound AND<br />     T.RecNo &lt; @PageUpperBound<br />ORDER BY T.RecNo<br /><br />GO </div>
		<p>另一种可能更适合程序中“拼凑” SQL 语句：用两次 TOP 命令取得我们所要的分页数据，例如：<font color="#0000ff">（编号 SS2）</font></p>
		<div class="pbcode">SELECT * FROM<br />     (<br />     SELECT TOP(<i><font color="#0000ff">PageSize</font></i>) * FROM<br />     (<br />          SELECT TOP (<font color="#0000ff"><i>PageSize</i></font> * <font color="#0000ff"><i>PageIndex</i></font>) *<br />          FROM Articles<br />          ORDER BY PubTime DESC<br />     )<br />     ORDER BY PubTime ASC<br />)<br />ORDER BY PubTime DESC </div>
		<p>这个的想法就是“掐头去尾”，还有不少分页的方法，这里就不一一列出了。</p>
		<p>对于 Oracle 数据库，有几处不同严重妨碍了上面几个方法的实施，比如，Oracle 不支持 TOP 关键字：不过这个好像并不十分严重，因为它提供了 rownum 这个隐式游标，可以实现与 TOP 类似的功能，如：</p>
		<div class="pbcode">SELECT TOP 10 ... FROM WHERE ... </div>
		<p>要写成</p>
		<div class="pbcode">SELECT ... FROM ... WHERE ... AND rownum &lt;= 10 </div>
		<p>rownum 是记录序号（1，2，3...），但有一个比较麻烦的事情是：如果 SQL 语句中有 ORDER BY ... 排序的时候，rownum 居然是先“标号”后排序！这样，这个序号如果不加处理是不合乎使用需求的。</p>
		<p>至于临时表，Oracle 的临时表和 SQL Server 的有很大不同，我还没搞懂这个东西，就不妄加揣测了。</p>
		<p>国内网站中介绍 Oracle 分页的资料很少，我找到了一个国外站点（www.faqts.com）的<a href="http://www.faqts.com/knowledge_base/view.phtml/aid/16331/fid/148" target="_blank">一篇 FAQ</a>，根据这篇文章的介绍，可以如下分页：<font color="#0000ff">（编号 Ora1）</font></p>
		<div class="pbcode">SELECT * FROM<br />     (<br />     SELECT A.*, rownum r<br />     FROM<br />          (<br /><font color="#0000ff">          SELECT *<br />          FROM Articles<br />          ORDER BY PubTime DESC</font><br />          ) A<br />     WHERE rownum &lt;= PageUpperBound<br />     ) B<br />WHERE r &gt; PageLowerBound; </div>
		<p>其中蓝色部分可以改为任意的、需要的 SQL SELECT 语句，这点倒是挺方便的。</p>
		<p>　</p>
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/99471.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2007-02-12 15:57 <a href="http://www.blogjava.net/wuxufeng8080/articles/99471.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL Server 索引结构及其使用（ZT）</title><link>http://www.blogjava.net/wuxufeng8080/articles/91018.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Sat, 30 Dec 2006 06:56:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/91018.html</guid><description><![CDATA[
		<p align="center">
				<b>SQL Server 索引结构及其使用（一）<br /></b>
				<br />作者：<a href="http://www.vckbase.com/bbs/userinfo.asp?id=freedk" target="_blank">freedk</a></p>
		<p>
				<b>一、深入浅出理解索引结构</b>
				<br />
				<br />　　实际上，您可以把索引理解为一种特殊的目录。微软的SQL SERVER提供了两种索引：聚集索引（clustered index，也称聚类索引、簇集索引）和非聚集索引（nonclustered index，也称非聚类索引、非簇集索引）。下面，我们举例来说明一下聚集索引和非聚集索引的区别：<br />　　其实，我们的汉语字典的正文本身就是一个聚集索引。比如，我们要查“安”字，就会很自然地翻开字典的前几页，因为“安”的拼音是“an”，而按照拼音排序汉字的字典是以英文字母“a”开头并以“z”结尾的，那么“安”字就自然地排在字典的前部。如果您翻完了所有以“a”开头的部分仍然找不到这个字，那么就说明您的字典中没有这个字；同样的，如果查“张”字，那您也会将您的字典翻到最后部分，因为“张”的拼音是“zhang”。也就是说，字典的正文部分本身就是一个目录，您不需要再去查其他目录来找到您需要找的内容。我们把这种正文内容本身就是一种按照一定规则排列的目录称为“聚集索引”。<br />　　如果您认识某个字，您可以快速地从自动中查到这个字。但您也可能会遇到您不认识的字，不知道它的发音，这时候，您就不能按照刚才的方法找到您要查的字，而需要去根据“偏旁部首”查到您要找的字，然后根据这个字后的页码直接翻到某页来找到您要找的字。但您结合“部首目录”和“检字表”而查到的字的排序并不是真正的正文的排序方法，比如您查“张”字，我们可以看到在查部首之后的检字表中“张”的页码是672页，检字表中“张”的上面是“驰”字，但页码却是63页，“张”的下面是“弩”字，页面是390页。很显然，这些字并不是真正的分别位于“张”字的上下方，现在您看到的连续的“驰、张、弩”三字实际上就是他们在非聚集索引中的排序，是字典正文中的字在非聚集索引中的映射。我们可以通过这种方式来找到您所需要的字，但它需要两个过程，先找到目录中的结果，然后再翻到您所需要的页码。我们把这种目录纯粹是目录，正文纯粹是正文的排序方式称为“非聚集索引”。<br />　　通过以上例子，我们可以理解到什么是“聚集索引”和“非聚集索引”。进一步引申一下，我们可以很容易的理解：每个表只能有一个聚集索引，因为目录只能按照一种方法进行排序。<br /><br /><b>二、何时使用聚集索引或非聚集索引</b><br /><br />下面的表总结了何时使用聚集索引或非聚集索引（很重要）：</p>
		<table id="AutoNumber1" style="COLOR: #000000; BORDER-COLLAPSE: collapse" height="107" cellspacing="0" cellpadding="0" width="62%" border="1">
				<tbody>
						<tr>
								<td align="middle" width="40%" height="23">动作描述</td>
								<td align="middle" width="29%" height="23">使用聚集索引</td>
								<td align="middle" width="31%" height="23">使用非聚集索引</td>
						</tr>
						<tr>
								<td width="40%" height="20">列经常被分组排序</td>
								<td width="29%" height="20">应</td>
								<td width="31%" height="20">应</td>
						</tr>
						<tr>
								<td width="40%" height="20">返回某范围内的数据</td>
								<td width="29%" height="20">应</td>
								<td width="31%" height="20">不应</td>
						</tr>
						<tr>
								<td width="40%" height="20">一个或极少不同值</td>
								<td width="29%" height="20">不应</td>
								<td width="31%" height="20">不应</td>
						</tr>
						<tr>
								<td width="40%" height="20">小数目的不同值</td>
								<td width="29%" height="20">应</td>
								<td width="31%" height="20">不应</td>
						</tr>
						<tr>
								<td width="40%" height="20">大数目的不同值</td>
								<td width="29%" height="20">不应</td>
								<td width="31%" height="20">应</td>
						</tr>
						<tr>
								<td width="40%" height="20">频繁更新的列</td>
								<td width="29%" height="20">不应</td>
								<td width="31%" height="20">应</td>
						</tr>
						<tr>
								<td width="40%" height="20">外键列</td>
								<td width="29%" height="20">应</td>
								<td width="31%" height="20">应</td>
						</tr>
						<tr>
								<td width="40%" height="20">主键列</td>
								<td width="29%" height="20">应</td>
								<td width="31%" height="20">应</td>
						</tr>
						<tr>
								<td width="40%" height="20">频繁修改索引列</td>
								<td width="29%" height="20">不应</td>
								<td width="31%" height="20">应</td>
						</tr>
				</tbody>
		</table>
		<p>　　事实上，我们可以通过前面聚集索引和非聚集索引的定义的例子来理解上表。如：返回某范围内的数据一项。比如您的某个表有一个时间列，恰好您把聚合索引建立在了该列，这时您查询2004年1月1日至2004年10月1日之间的全部数据时，这个速度就将是很快的，因为您的这本字典正文是按日期进行排序的，聚类索引只需要找到要检索的所有数据中的开头和结尾数据即可；而不像非聚集索引，必须先查到目录中查到每一项数据对应的页码，然后再根据页码查到具体内容。<br /><br /><b>三、结合实际，谈索引使用的误区</b><br /><br />　　理论的目的是应用。虽然我们刚才列出了何时应使用聚集索引或非聚集索引，但在实践中以上规则却很容易被忽视或不能根据实际情况进行综合分析。下面我们将根据在实践中遇到的实际问题来谈一下索引使用的误区，以便于大家掌握索引建立的方法。<br /><br />1、主键就是聚集索引<br />　　这种想法笔者认为是极端错误的，是对聚集索引的一种浪费。虽然SQL SERVER默认是在主键上建立聚集索引的。<br />　　通常，我们会在每个表中都建立一个ID列，以区分每条数据，并且这个ID列是自动增大的，步长一般为1。我们的这个办公自动化的实例中的列Gid就是如此。此时，如果我们将这个列设为主键，SQL SERVER会将此列默认为聚集索引。这样做有好处，就是可以让您的数据在数据库中按照ID进行物理排序，但笔者认为这样做意义不大。<br />　　显而易见，聚集索引的优势是很明显的，而每个表中只能有一个聚集索引的规则，这使得聚集索引变得更加珍贵。<br />　　从我们前面谈到的聚集索引的定义我们可以看出，使用聚集索引的最大好处就是能够根据查询要求，迅速缩小查询范围，避免全表扫描。在实际应用中，因为ID号是自动生成的，我们并不知道每条记录的ID号，所以我们很难在实践中用ID号来进行查询。这就使让ID号这个主键作为聚集索引成为一种资源浪费。其次，让每个ID号都不同的字段作为聚集索引也不符合“大数目的不同值情况下不应建立聚合索引”规则；当然，这种情况只是针对用户经常修改记录内容，特别是索引项的时候会负作用，但对于查询速度并没有影响。<br />　　在办公自动化系统中，无论是系统首页显示的需要用户签收的文件、会议还是用户进行文件查询等任何情况下进行数据查询都离不开字段的是“日期”还有用户本身的“用户名”。<br />　　通常，办公自动化的首页会显示每个用户尚未签收的文件或会议。虽然我们的where语句可以仅仅限制当前用户尚未签收的情况，但如果您的系统已建立了很长时间，并且数据量很大，那么，每次每个用户打开首页的时候都进行一次全表扫描，这样做意义是不大的，绝大多数的用户1个月前的文件都已经浏览过了，这样做只能徒增数据库的开销而已。事实上，我们完全可以让用户打开系统首页时，数据库仅仅查询这个用户近3个月来未阅览的文件，通过“日期”这个字段来限制表扫描，提高查询速度。如果您的办公自动化系统已经建立的2年，那么您的首页显示速度理论上将是原来速度8倍，甚至更快。<br />　　在这里之所以提到“理论上”三字，是因为如果您的聚集索引还是盲目地建在ID这个主键上时，您的查询速度是没有这么高的，即使您在“日期”这个字段上建立的索引（非聚合索引）。下面我们就来看一下在1000万条数据量的情况下各种查询的速度表现（3个月内的数据为25万条）：<br /><br />（1）仅在主键上建立聚集索引，并且不划分时间段：</p>
		<pre>Select gid,fariqi,neibuyonghu,title from tgongwen</pre>
		<p>用时：128470毫秒（即：128秒）<br /><br />（2）在主键上建立聚集索引，在fariq上建立非聚集索引：</p>
		<pre>select gid,fariqi,neibuyonghu,title from Tgongwen
where fariqi&gt; dateadd(day,-90,getdate())</pre>
		<p>用时：53763毫秒（54秒）<br /><br />（3）将聚合索引建立在日期列（fariqi）上：</p>
		<pre>select gid,fariqi,neibuyonghu,title from Tgongwen
where fariqi&gt; dateadd(day,-90,getdate())</pre>
		<p>用时：2423毫秒（2秒）<br /><br />　　虽然每条语句提取出来的都是25万条数据，各种情况的差异却是巨大的，特别是将聚集索引建立在日期列时的差异。事实上，如果您的数据库真的有1000万容量的话，把主键建立在ID列上，就像以上的第1、2种情况，在网页上的表现就是超时，根本就无法显示。这也是我摒弃ID列作为聚集索引的一个最重要的因素。得出以上速度的方法是：在各个select语句前加：</p>
		<pre>declare @d datetime
set @d=getdate()</pre>
		<p>并在select语句后加：</p>
		<pre>select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())</pre>
		<p>2、只要建立索引就能显著提高查询速度<br />　　事实上，我们可以发现上面的例子中，第2、3条语句完全相同，且建立索引的字段也相同；不同的仅是前者在fariqi字段上建立的是非聚合索引，后者在此字段上建立的是聚合索引，但查询速度却有着天壤之别。所以，并非是在任何字段上简单地建立索引就能提高查询速度。<br />　　从建表的语句中，我们可以看到这个有着1000万数据的表中fariqi字段有5003个不同记录。在此字段上建立聚合索引是再合适不过了。在现实中，我们每天都会发几个文件，这几个文件的发文日期就相同，这完全符合建立聚集索引要求的：“既不能绝大多数都相同，又不能只有极少数相同”的规则。由此看来，我们建立“适当”的聚合索引对于我们提高查询速度是非常重要的。<br /><br />3、把所有需要提高查询速度的字段都加进聚集索引，以提高查询速度<br />　　上面已经谈到：在进行数据查询时都离不开字段的是“日期”还有用户本身的“用户名”。既然这两个字段都是如此的重要，我们可以把他们合并起来，建立一个复合索引（compound index）。<br />　　很多人认为只要把任何字段加进聚集索引，就能提高查询速度，也有人感到迷惑：如果把复合的聚集索引字段分开查询，那么查询速度会减慢吗？带着这个问题，我们来看一下以下的查询速度（结果集都是25万条数据）：（日期列fariqi首先排在复合聚集索引的起始列，用户名neibuyonghu排在后列）：</p>
		<pre>（1）select gid,fariqi,neibuyonghu,title from Tgongwen where fariqi&gt;''2004-5-5'' </pre>
		<p>查询速度：2513毫秒</p>
		<pre>（2）select gid,fariqi,neibuyonghu,title from Tgongwen 
　　　　　　　　　　　　where fariqi&gt;''2004-5-5'' and neibuyonghu=''办公室''</pre>
		<p>查询速度：2516毫秒</p>
		<pre>（3）select gid,fariqi,neibuyonghu,title from Tgongwen where neibuyonghu=''办公室''</pre>
		<p>查询速度：60280毫秒<br /><br />　　从以上试验中，我们可以看到如果仅用聚集索引的起始列作为查询条件和同时用到复合聚集索引的全部列的查询速度是几乎一样的，甚至比用上全部的复合索引列还要略快（在查询结果集数目一样的情况下）；而如果仅用复合聚集索引的非起始列作为查询条件的话，这个索引是不起任何作用的。当然，语句1、2的查询速度一样是因为查询的条目数一样，如果复合索引的所有列都用上，而且查询结果少的话，这样就会形成“索引覆盖”，因而性能可以达到最优。同时，请记住：无论您是否经常使用聚合索引的其他列，但其前导列一定要是使用最频繁的列。<br /><br /><b>四、其他书上没有的索引使用经验总结</b><br /><br />1、用聚合索引比用不是聚合索引的主键速度快<br />　　下面是实例语句：（都是提取25万条数据）</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi=''2004-9-16''</pre>
		<p>使用时间：3326毫秒</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where gid&lt;=250000</pre>
		<p>使用时间：4470毫秒<br /><br />这里，用聚合索引比用不是聚合索引的主键速度快了近1/4。<br /><br />2、用聚合索引比用一般的主键作order by时速度快，特别是在小数据量情况下</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen order by fariqi</pre>
		<p>用时：12936</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen order by gid</pre>
		<p>用时：18843<br /><br />　　这里，用聚合索引比用一般的主键作order by时，速度快了3/10。事实上，如果数据量很小的话，用聚集索引作为排序列要比使用非聚集索引速度快得明显的多；而数据量如果很大的话，如10万以上，则二者的速度差别不明显。<br /><br />3、使用聚合索引内的时间段，搜索时间会按数据占整个数据表的百分比成比例减少，而无论聚合索引使用了多少个：</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi&gt;''2004-1-1''</pre>
		<p>用时：6343毫秒（提取100万条） </p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi&gt;''2004-6-6''</pre>
		<p>用时：3170毫秒（提取50万条）</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi=''2004-9-16''</pre>
		<p>用时：3326毫秒（和上句的结果一模一样。如果采集的数量一样，那么用大于号和等于号是一样的）</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen 
　　　　　　　　　　　　where fariqi&gt;''2004-1-1'' and fariqi&lt;''2004-6-6''</pre>
		<p>用时：3280毫秒<br /><br />4、日期列不会因为有分秒的输入而减慢查询速度<br />　　下面的例子中，共有100万条数据，2004年1月1日以后的数据有50万条，但只有两个不同的日期，日期精确到日；之前有数据50万条，有5000个不同的日期，日期精确到秒。</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen 
　　　　　　　　　　where fariqi&gt;''2004-1-1'' order by fariqi</pre>
		<p>用时：6390毫秒</p>
		<pre>select gid,fariqi,neibuyonghu,reader,title from Tgongwen 
　　　　　　　　　　　　where fariqi&lt;''2004-1-1'' order by fariqi</pre>
		<p>用时：6453毫秒<br /><br /><b>五、其他注意事项</b><br /><br />　　“水可载舟，亦可覆舟”，索引也一样。索引有助于提高检索性能，但过多或不当的索引也会导致系统低效。因为用户在表中每加进一个索引，数据库就要做更多的工作。过多的索引甚至会导致索引碎片。<br />　　所以说，我们要建立一个“适当”的索引体系，特别是对聚合索引的创建，更应精益求精，以使您的数据库能得到高性能的发挥。<br />　　当然，在实践中，作为一个尽职的数据库管理员，您还要多测试一些方案，找出哪种方案效率最高、最为有效。<br /></p>
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/91018.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2006-12-30 14:56 <a href="http://www.blogjava.net/wuxufeng8080/articles/91018.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>50种方法巧妙优化你的SQL Server数据库</title><link>http://www.blogjava.net/wuxufeng8080/articles/86029.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Thu, 07 Dec 2006 02:49:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/86029.html</guid><description><![CDATA[
		<p>
				<font face="Arial">查询速度慢的原因很多，常见如下几种：<br />　　<br />　　1、没有索引或者没有用到索引(这是查询慢最常见的问题，是程序设计的缺陷)<br />　　<br />　　2、I/O吞吐量小，形成了瓶颈效应。<br />　　<br />　　3、没有创建计算列导致查询不优化。<br />　　<br />　　4、内存不足<br />　　<br />　　5、网络速度慢<br />　　<br />　　6、查询出的数据量过大（可以采用多次查询，其他的方法降低数据量）<br />　　<br />　　7、锁或者死锁(这也是查询慢最常见的问题，是程序设计的缺陷)<br />　　<br />　　8、sp_lock,sp_who,活动的用户查看,原因是读写竞争资源。<br />　　<br />　　9、返回了不必要的行和列<br />　　<br />　　10、查询语句不好，没有优化</font>
		</p>
		<p>
				<font face="Arial">　　可以通过如下方法来优化查询 :<br />　　<br />　　1、把数据、日志、索引放到不同的I/O设备上，增加读取速度，以前可以将Tempdb应放在RAID0上，SQL2000不在支持。数据量（尺寸）越大，提高I/O越重要.<br />　　<br />　　2、纵向、横向分割表，减少表的尺寸(sp_spaceuse)<br />　　<br />　　3、升级硬件<br />　　<br />　　4、根据查询条件,建立索引,优化索引、优化访问方式，限制结果集的数据量。注意填充因子要适当（最好是使用默认值0）。索引应该尽量小，使用字节数小的列建索引好（参照索引的创建）,不要对有限的几个值的字段建单一索引如性别字段<br />　　<br />　　5、提高网速;<br />　　<br />　　6、扩大服务器的内存,Windows 2000和SQL server 2000能支持4-8G的内存。配置虚拟内存：虚拟内存大小应基于计算机上并发运行的服务进行配置。运行 Microsoft SQL Server? 2000 时，可考虑将虚拟内存大小设置为计算机中安装的物理内存的 1.5 倍。如果另外安装了全文检索功能，并打算运行 Microsoft 搜索服务以便执行全文索引和查询，可考虑：将虚拟内存大小配置为至少是计算机中安装的物理内存的 3 倍。将 SQL Server max server memory 服务器配置选项配置为物理内存的 1.5 倍（虚拟内存大小设置的一半）。<br />　　<br />　　7、增加服务器 CPU个数;但是必须明白并行处理串行处理更需要资源例如内存。使用并行还是串行程是MsSQL自动评估选择的。单个任务分解成多个任务，就可以在处理器上运行。例如耽搁查询的排序、连接、扫描和GROUP BY字句同时执行，SQL SERVER根据系统的负载情况决定最优的并行等级，复杂的需要消耗大量的CPU的查询最适合并行处理。但是更新操作Update,Insert， Delete还不能并行处理。<br />　　<br />　　8、如果是使用like进行查询的话，简单的使用index是不行的，但是全文索引，耗空间。 like 'a%' 使用索引 like '%a' 不使用索引用 like '%a%' 查询时，查询耗时和字段值总长度成正比,所以不能用CHAR类型，而是VARCHAR。对于字段的值很长的建全文索引。<br />　　<br />　　9、DB Server 和APPLication Server 分离；OLTP和OLAP分离<br />　　<br />　　10、分布式分区视图可用于实现数据库服务器联合体。联合体是一组分开管理的服务器，但它们相互协作分担系统的处理负荷。这种通过分区数据形成数据库服务器联合体的机制能够扩大一组服务器，以支持大型的多层 Web 站点的处理需要。有关更多信息，参见设计联合数据库服务器。（参照SQL帮助文件'分区视图'）<br />　　<br />　　a、在实现分区视图之前，必须先水平分区表<br />　　<br />　　b、在创建成员表后，在每个成员服务器上定义一个分布式分区视图，并且每个视图具有相同的名称。这样，引用分布式分区视图名的查询可以在任何一个成员服务器上运行。系统操作如同每个成员服务器上都有一个原始表的复本一样，但其实每个服务器上只有一个成员表和一个分布式分区视图。数据的位置对应用程序是透明的。<br />　　<br />　　11、重建索引 DBCC REINDEX ,DBCC INDEXDEFRAG,收缩数据和日志 DBCC SHRINKDB,DBCC SHRINKFILE. 设置自动收缩日志.对于大的数据库不要设置数据库自动增长，它会降低服务器的性能。在T-sql的写法上有很大的讲究，下面列出常见的要点：首先，DBMS处理查询计划的过程是这样的：<br />　　<br />　　1、 查询语句的词法、语法检查<br />　　<br />　　2、 将语句提交给DBMS的查询优化器<br />　　<br />　　3、 优化器做代数优化和存取路径的优化<br />　　<br />　　4、 由预编译模块生成查询规划<br />　　<br />　　5、 然后在合适的时间提交给系统处理执行<br />　　<br />　　6、 最后将执行结果返回给用户其次，看一下SQL SERVER的数据存放的结构：一个页面的大小为8K(8060)字节，8个页面为一个盘区，按照B树存放。<br />　　<br />　　12、Commit和rollback的区别 Rollback:回滚所有的事物。 Commit:提交当前的事物. 没有必要在动态SQL里写事物，如果要写请写在外面如： begin tran exec(@s) commit trans 或者将动态SQL 写成函数或者存储过程。<br />　　<br />　　13、在查询Select语句中用Where字句限制返回的行数,避免表扫描,如果返回不必要的数据，浪费了服务器的I/O资源，加重了网络的负担降低性能。如果表很大，在表扫描的期间将表锁住，禁止其他的联接访问表,后果严重。<br />　　<br />　　14、SQL的注释申明对执行没有任何影响<br />　　15、尽可能不使用光标，它占用大量的资源。如果需要row-by-row地执行，尽量采用非光标技术,如：在客户端循环，用临时表，Table变量，用子查询，用Case语句等等。游标可以按照它所支持的提取选项进行分类： 只进 必须按照从第一行到最后一行的顺序提取行。FETCH NEXT 是唯一允许的提取操作,也是默认方式。可滚动性可以在游标中任何地方随机提取任意行。游标的技术在SQL2000下变得功能很强大，他的目的是支持循环。有四个并发选项 READ_ONLY：不允许通过游标定位更新(Update)，且在组成结果集的行中没有锁。 OPTIMISTIC WITH valueS:乐观并发控制是事务控制理论的一个标准部分。乐观并发控制用于这样的情形，即在打开游标及更新行的间隔中，只有很小的机会让第二个用户更新某一行。当某个游标以此选项打开时，没有锁控制其中的行，这将有助于最大化其处理能力。如果用户试图修改某一行，则此行的当前值会与最后一次提取此行时获取的值进行比较。如果任何值发生改变，则服务器就会知道其他人已更新了此行，并会返回一个错误。如果值是一样的，服务器就执行修改。选择这个并发选项OPTIMISTIC WITH ROW VERSIONING:此乐观并发控制选项基于行版本控制。使用行版本控制，其中的表必须具有某种版本标识符，服务器可用它来确定该行在读入游标后是否有所更改。在 SQL Server 中，这个性能由 timestamp 数据类型提供，它是一个二进制数字，表示数据库中更改的相对顺序。每个数据库都有一个全局当前时间戳值：@@DBTS。每次以任何方式更改带有 timestamp 列的行时，SQL Server 先在时间戳列中存储当前的 @@DBTS 值，然后增加 @@DBTS 的值。如果某 个表具有 timestamp 列，则时间戳会被记到行级。服务器就可以比较某行的当前时间戳值和上次提取时所存储的时间戳值，从而确定该行是否已更新。服务器不必比较所有列的值，只需比较 timestamp 列即可。如果应用程序对没有 timestamp 列的表要求基于行版本控制的乐观并发，则游标默认为基于数值的乐观并发控制。 SCROLL LOCKS 这个选项实现悲观并发控制。在悲观并发控制中，在把数据库的行读入游标结果集时，应用程序将试图锁定数据库行。在使用服务器游标时，将行读入游标时会在其上放置一个更新锁。如果在事务内打开游标，则该事务更新锁将一直保持到事务被提交或回滚；当提取下一行时，将除去游标锁。如果在事务外打开游标，则提取下一行时，锁就被丢弃。因此，每当用户需要完全的悲观并发控制时，游标都应在事务内打开。更新锁将阻止任何其它任务获取更新锁或排它锁，从而阻止其它任务更新该行。然而，更新锁并不阻止共享锁，所以它不会阻止其它任务读取行，除非第二个任务也在要求带更新锁的读取。滚动锁根据在游标定义的 Select 语句中指定的锁提示，这些游标并发选项可以生成滚动锁。滚动锁在提取时在每行上获取，并保持到下次提取或者游标关闭，以先发生者为准。下次提取时，服务器为新提取中的行获取滚动锁，并释放上次提取中行的滚动锁。滚动锁独立于事务锁，并可以保持到一个提交或回滚操作之后。如果提交时关闭游标的选项为关，则 COMMIT 语句并不关闭任何打开的游标，而且滚动锁被保留到提交之后，以维护对所提取数据的隔离。所获取滚动锁的类型取决于游标并发选项和游标 Select 语句中的锁提示。锁提示 只读 乐观数值 乐观行版本控制 锁定无提示 未锁定 未锁定 未锁定 更新 NOLOCK 未锁定 未锁定未锁定 未锁定 HOLDLOCK 共享 共享 共享 更新 UPDLOCK 错误 更新 更新 更新 TABLOCKX 错误 未锁定 未锁定更新其它 未锁定 未锁定 未锁定 更新 *指定 NOLOCK 提示将使指定了该提示的表在游标内是只读的。<br />　　<br />　　16、用Profiler来跟踪查询，得到查询所需的时间，找出SQL的问题所在;用索引优化器优化索引<br />　　<br />　　17、注意UNion和UNion all 的区别。UNION all好<br />　　<br />　　18、注意使用DISTINCT，在没有必要时不要用，它同UNION一样会使查询变慢。重复的记录在查询里是没有问题的<br />　　<br />　　19、查询时不要返回不需要的行、列<br />　　<br />　　20、用sp_configure 'query governor cost limit'或者SET QUERY_GOVERNOR_COST_LIMIT来限制查询消耗的资源。当评估查询消耗的资源超出限制时，服务器自动取消查询,在查询之前就扼杀掉。 SET LOCKTIME设置锁的时间<br />　　<br />　　21、用select top 100 / 10 Percent 来限制用户返回的行数或者SET ROWCOUNT来限制操作的行<br />　　<br />　　22、在SQL2000以前，一般不要用如下的字句: "IS NULL", "&lt;&gt;", "!=", "!&gt;", "!&lt;", "NOT", "NOT EXISTS", "NOT IN", "NOT LIKE", and "LIKE '%500'"，因为他们不走索引全是表扫描。也不要在Where字句中的列名加函数，如Convert，substring等,如果必须用函数的时候，创建计算列再创建索引来替代.还可以变通写法：Where SUBSTRING(firstname,1,1) = 'm'改为Where firstname like 'm%'（索引扫描），一定要将函数和列名分开。并且索引不能建得太多和太大。NOT IN会多次扫描表，使用EXISTS、NOT EXISTS ，IN , LEFT OUTER JOIN 来替代，特别是左连接,而Exists比IN更快，最慢的是NOT操作.如果列的值含有空，以前它的索引不起作用，现在2000的优化器能够处理了。相同的是IS NULL，"NOT", "NOT EXISTS", "NOT IN"能优化她，而"&lt;&gt;"等还是不能优化，用不到索引。<br />　　<br />　　23、使用Query Analyzer，查看SQL语句的查询计划和评估分析是否是优化的SQL。一般的20%的代码占据了80%的资源，我们优化的重点是这些慢的地方。<br />　　<br />　　24、如果使用了IN或者OR等时发现查询没有走索引，使用显示申明指定索引： Select * FROM PersonMember (INDEX = IX_Title) Where processid IN ('男'，'女')<br />　　<br />　　25、将需要查询的结果预先计算好放在表中，查询的时候再Select。这在SQL7.0以前是最重要的手段。例如医院的住院费计算。<br />　　<br />　　26、MIN() 和 MAX()能使用到合适的索引。<br />　　<br />　　27、数据库有一个原则是代码离数据越近越好，所以优先选择Default,依次为Rules,Triggers, Constraint（约束如外健主健CheckUNIQUE……,数据类型的最大长度等等都是约束）,Procedure.这样不仅维护工作小，编写程序质量高，并且执行的速度快。<br />　　<br />　　28、如果要插入大的二进制值到Image列，使用存储过程，千万不要用内嵌Insert来插入(不知JAVA是否)。因为这样应用程序首先将二进制值转换成字符串（尺寸是它的两倍），服务器受到字符后又将他转换成二进制值.存储过程就没有这些动作: 方法：Create procedure p_insert as insert into table(Fimage) values (@image), 在前台调用这个存储过程传入二进制参数，这样处理速度明显改善。<br />　　<br />　　29、Between在某些时候比IN 速度更快,Between能够更快地根据索引找到范围。用查询优化器可见到差别。 select * from chineseresume where title in ('男','女') Select * from chineseresume where between '男' and '女' 是一样的。由于in会在比较多次，所以有时会慢些。<br />　　<br />　　30、在必要是对全局或者局部临时表创建索引，有时能够提高速度，但不是一定会这样，因为索引也耗费大量的资源。他的创建同是实际表一样。<br />　　<br />　　31、不要建没有作用的事物例如产生报表时，浪费资源。只有在必要使用事物时使用它。<br />　　<br />　　32、用OR的字句可以分解成多个查询，并且通过UNION 连接多个查询。他们的速度只同是否使用索引有关,如果查询需要用到联合索引，用UNION all执行的效率更高.多个OR的字句没有用到索引，改写成UNION的形式再试图与索引匹配。一个关键的问题是否用到索引。<br />　　<br />　　 33、尽量少用视图，它的效率低。对视图操作比直接对表操作慢,可以用stored procedure来代替她。特别的是不要用视图嵌套,嵌套视图增加了寻找原始资料的难度。我们看视图的本质：它是存放在服务器上的被优化好了的已经产生了查询规划的SQL。对单个表检索数据时，不要使用指向多个表的视图，直接从表检索或者仅仅包含这个表的视图上读，否则增加了不必要的开销,查询受到干扰.为了加快视图的查询，MsSQL增加了视图索引的功能。<br />　　<br />　　34、没有必要时不要用DISTINCT和ORDER BY，这些动作可以改在客户端执行。它们增加了额外的开销。这同UNION 和UNION ALL一样的道理。<br />　　<br />　　select top 20 ad.companyname,comid,position,ad.referenceid,worklocation, convert(varchar(10),ad.postDate,120) as postDate1,workyear,degreedescription FROM jobcn_query.dbo.COMPANYAD_query ad where referenceID in('JCNAD00329667','JCNAD132168','JCNAD00337748','JCNAD00338345',<br />　　'JCNAD00333138','JCNAD00303570','JCNAD00303569',<br />　　'JCNAD00303568','JCNAD00306698','JCNAD00231935','JCNAD00231933',<br />　　'JCNAD00254567','JCNAD00254585','JCNAD00254608',<br />　　'JCNAD00254607','JCNAD00258524','JCNAD00332133','JCNAD00268618',<br />　　'JCNAD00279196','JCNAD00268613') order by postdate desc<br />　　<br />　　35、在IN后面值的列表中，将出现最频繁的值放在最前面，出现得最少的放在最后面，减少判断的次数。<br />　　<br />　　36、当用Select INTO时，它会锁住系统表(sysobjects，sysindexes等等)，阻塞其他的连接的存取。创建临时表时用显示申明语句，而不是 select INTO. drop table t_lxh begin tran select * into t_lxh from chineseresume where name = 'XYZ' --commit 在另一个连接中Select * from sysobjects可以看到 Select INTO 会锁住系统表，Create table 也会锁系统表(不管是临时表还是系统表)。所以千万不要在事物内使用它！！！这样的话如果是经常要用的临时表请使用实表，或者临时表变量。<br />　　<br />　　37、一般在GROUP BY 个HAVING字句之前就能剔除多余的行，所以尽量不要用它们来做剔除行的工作。他们的执行顺序应该如下最优：select 的Where字句选择所有合适的行，Group By用来分组个统计行，Having字句用来剔除多余的分组。这样Group By 个Having的开销小，查询快.对于大的数据行进行分组和Having十分消耗资源。如果Group BY的目的不包括计算，只是分组，那么用Distinct更快<br />　　<br />　　38、一次更新多条记录比分多次更新每次一条快,就是说批处理好<br />　　<br />　　39、少用临时表，尽量用结果集和Table类性的变量来代替它,Table 类型的变量比临时表好<br />　　<br />　　40、在SQL2000下，计算字段是可以索引的，需要满足的条件如下：<br />　　<br />　　a、计算字段的表达是确定的<br />　　<br />　　b、不能用在TEXT,Ntext，Image数据类型<br />　　<br />　　c、必须配制如下选项 ANSI_NULLS = ON, ANSI_PADDINGS = ON, …….<br />　　<br />　　41、尽量将数据的处理工作放在服务器上，减少网络的开销，如使用存储过程。存储过程是编译好、优化过、并且被组织到一个执行规划里、且存储在数据库中的SQL语句，是控制流语言的集合，速度当然快。反复执行的动态SQL,可以使用临时存储过程，该过程（临时表）被放在Tempdb中。以前由于SQL SERVER对复杂的数学计算不支持，所以不得不将这个工作放在其他的层上而增加网络的开销。SQL2000支持UDFs,现在支持复杂的数学计算，函数的返回值不要太大，这样的开销很大。用户自定义函数象光标一样执行的消耗大量的资源，如果返回大的结果采用存储过程<br />　　<br />　　42、不要在一句话里再三的使用相同的函数，浪费资源,将结果放在变量里再调用更快<br />　　<br />　　43、Select COUNT(*)的效率教低，尽量变通他的写法，而EXISTS快.同时请注意区别： select count(Field of null) from Table 和 select count(Field of NOT null) from Table 的返回值是不同的！！！<br />　　<br />　　44、当服务器的内存够多时，配制线程数量 = 最大连接数+5，这样能发挥最大的效率；否则使用 配制线程数量&lt;最大连接数启用SQL SERVER的线程池来解决,如果还是数量 = 最大连接数+5，严重的损害服务器的性能。<br />　　<br />　　45、按照一定的次序来访问你的表。如果你先锁住表A，再锁住表B，那么在所有的存储过程中都要按照这个顺序来锁定它们。如果你（不经意的）某个存储过程中先锁定表B，再锁定表A，这可能就会导致一个死锁。如果锁定顺序没有被预先详细的设计好，死锁很难被发现<br />　　<br />　　46、通过SQL Server Performance Monitor监视相应硬件的负载 Memory: Page Faults / sec计数器如果该值偶尔走高，表明当时有线程竞争内存。如果持续很高，则内存可能是瓶颈。<br />　　Process:<br />　　<br />　　1、% DPC Time 指在范例间隔期间处理器用在缓延程序调用(DPC)接收和提供服务的百分比。(DPC 正在运行的为比标准间隔优先权低的间隔)。 由于 DPC 是以特权模式执行的，DPC 时间的百分比为特权时间百分比的一部分。这些时间单独计算并且不属于间隔计算总数的一部 分。这个总数显示了作为实例时间百分比的平均忙时。<br />　　<br />　　2、%Processor Time计数器　如果该参数值持续超过95%，表明瓶颈是CPU。可以考虑增加一个处理器或换一个更快的处理器。<br />　　<br />　　3、% Privileged Time 指非闲置处理器时间用于特权模式的百分比。(特权模式是为操作系统组件和操纵硬件驱动程序而设计的一种处理模式。它允许直接访问硬件和所有内存。另一种模式为用户模式，它是一种为应用程序、环境分系统和整数分系统设计的一种有限处理模式。操作系统将应用程序线程转换成特权模式以访问操作系统服务)。特权时间的 % 包括为间断和 DPC 提供服务的时间。特权时间比率高可能是由于失败设备产生的大数量的间隔而引起的。这个计数器将平均忙时作为样本时间的一部分显示。<br />　　<br />　　4、% User Time表示耗费CPU的数据库操作，如排序，执行aggregate functions等。如果该值很高，可考虑增加索引，尽量使用简单的表联接，水平分割大表格等方法来降低该值。 Physical Disk: Curretn Disk Queue Length计数器该值应不超过磁盘数的1.5~2倍。要提高性能，可增加磁盘。 SQLServer:Cache Hit Ratio计数器该值越高越好。如果持续低于80%，应考虑增加内存。 注意该参数值是从SQL Server启动后，就一直累加记数，所以运行经过一段时间后，该值将不能反映系统当前值。<br />　　<br />　　47、分析select emp_name form employee where salary &gt; 3000 在此语句中若salary是Float类型的，则优化器对其进行优化为Convert(float,3000)，因为3000是个整数，我们应在编程时使用3000.0而不要等运行时让DBMS进行转化。同样字符和整型数据的转换。<br />　　<br />　　48、查询的关联同写的顺序<br />　　<br />　　select a.personMemberID, * from chineseresume a,personmember b where personMemberID = b.referenceid and a.personMemberID = 'JCNPRH39681' （A = B ,B = '号码'）<br />　　<br />　　select a.personMemberID, * from chineseresume a,personmember b where a.personMemberID = b.referenceid and a.personMemberID = 'JCNPRH39681' and b.referenceid = 'JCNPRH39681' （A = B ,B = '号码'， A = '号码'）<br />　　<br />　　select a.personMemberID, * from chineseresume a,personmember b where b.referenceid = 'JCNPRH39681' and a.personMemberID = 'JCNPRH39681' （B = '号码'， A = '号码'）<br />　　<br />　　49、<br />　　<br />　　(1)IF 没有输入负责人代码 THEN code1=0 code2=9999 ELSE code1=code2=负责人代码 END IF 执行SQL语句为: Select 负责人名 FROM P2000 Where 负责人代码&gt;=:code1 AND负责人代码 &lt;=:code2<br />　　<br />　　(2)IF 没有输入负责人代码 THEN 　Select 负责人名 FROM P2000 ELSE code= 负责人代码 Select 负责人代码 FROM P2000 Where 负责人代码=:code END IF 第一种方法只用了一条SQL语句,第二种方法用了两条SQL语句。在没有输入负责人代码时,第二种方法显然比第一种方法执行效率高,因为它没有限制条件; 在输入了负责人代码时,第二种方法仍然比第一种方法效率高,不仅是少了一个限制条件,还因相等运算是最快的查询运算。我们写程序不要怕麻烦<br />　　<br />　　50、关于JOBCN现在查询分页的新方法（如下），用性能优化器分析性能的瓶颈，如果在I/O或者网络的速度上，如下的方法优化切实有效，如果在CPU或者内存上，用现在的方法更好。请区分如下的方法，说明索引越小越好。<br />　　<br />　　begin<br />　　<br />　　DECLARE @local_variable table (FID int identity(1,1),ReferenceID varchar(20))<br />　　<br />　　insert into @local_variable (ReferenceID)<br />　　<br />　　select top 100000 ReferenceID from chineseresume order by ReferenceID<br />　　<br />　　select * from @local_variable where Fid &gt; 40 and fid &lt;= 60<br />　　<br />　　end 和<br />　　<br />　　begin<br />　　<br />　　DECLARE @local_variable table (FID int identity(1,1),ReferenceID varchar(20))<br />　　<br />　　insert into @local_variable (ReferenceID)<br />　　<br />　　select top 100000 ReferenceID from chineseresume order by updatedate<br />　　<br />　　select * from @local_variable where Fid &gt; 40 and fid &lt;= 60<br />　　<br />　　end 的不同<br />　　<br />　　begin<br />　　<br />　　create table #temp (FID int identity(1,1),ReferenceID varchar(20))<br />　　<br />　　insert into #temp (ReferenceID)<br />　　<br />　　select top 100000 ReferenceID from chineseresume order by updatedate<br />　　<br />　　select * from #temp where Fid &gt; 40 and fid &lt;= 60 drop table #temp<br />　　<br />　　end</font>
		</p>
		<p>
				<font face="Arial">另附：存储过程编写经验和优化措施  From：网页教学网</font>
		</p>
		<p>
				<font face="Arial">　　一、适合读者对象：数据库开发程序员，数据库的数据量很多，涉及到对SP（存储过程）的优化的项目开发人员，对数据库有浓厚兴趣的人。 　</font>
		</p>
		<p>
				<font face="Arial">　　二、介绍：在数据库的开发过程中，经常会遇到复杂的业务逻辑和对数据库的操作，这个时候就会用SP来封装数据库操作。如果项目的SP较多，书写又没有一定的规范，将会影响以后的系统维护困难和大SP逻辑的难以理解，另外如果数据库的数据量大或者项目对SP的性能要求很，就会遇到优化的问题，否则速度有可能很慢，经过亲身经验，一个经过优化过的SP要比一个性能差的SP的效率甚至高几百倍。 　</font>
		</p>
		<p>
				<font face="Arial">　　三、内容： 　</font>
		</p>
		<p>
				<font face="Arial">　　1、开发人员如果用到其他库的Table或View，务必在当前库中建立View来实现跨库操作，最好不要直接使用“databse.dbo.table_name”，因为sp_depends不能显示出该SP所使用的跨库table或view，不方便校验。　　</font>
		</p>
		<p>
				<font face="Arial">　　2、开发人员在提交SP前，必须已经使用set showplan on分析过查询计划，做过自身的查询优化检查。 　</font>
		</p>
		<p>
				<font face="Arial">　　3、高程序运行效率，优化应用程序，在SP编写过程中应该注意以下几点： 　　</font>
		</p>
		<p>
				<font face="Arial">　　a)SQL的使用规范：</font>
		</p>
		<p>
				<font face="Arial">　　　i.　尽量避免大事务操作，慎用holdlock子句，提高系统并发能力。</font>
		</p>
		<p>
				<font face="Arial">　　　ii.　尽量避免反复访问同一张或几张表，尤其是数据量较大的表，可以考虑先根据条件提取数据到临时表中，然后再做连接。</font>
		</p>
		<p>
				<font face="Arial">　　　iii.　尽量避免使用游标，因为游标的效率较差，如果游标操作的数据超过1万行，那么就应该改写；如果使用了游标，就要尽量避免在游标循环中再进行表连接的操作。</font>
		</p>
		<p>
				<font face="Arial">　　　iv.　注意where字句写法，必须考虑语句顺序，应该根据索引顺序、范围大小来确定条件子句的前后顺序，尽可能的让字段顺序与索引顺序相一致，范围从大到小。</font>
		</p>
		<p>
				<font face="Arial">　　　v.　不要在where子句中的“=”左边进行函数、算术运算或其他表达式运算，否则系统将可能无法正确使用索引。</font>
		</p>
		<p>
				<font face="Arial">　　　vi.　尽量使用exists代替select count(1)来判断是否存在记录，count函数只有在统计表中所有行数时使用，而且count(1)比count(*)更有效率。</font>
		</p>
		<p>
				<font face="Arial">　　　vii.　尽量使用“&gt;=”，不要使用“&gt;”。</font>
		</p>
		<p>
				<font face="Arial">　　　viii.　注意一些or子句和union子句之间的替换</font>
		</p>
		<p>
				<font face="Arial">　　　ix.　注意表之间连接的数据类型，避免不同类型数据之间的连接。</font>
		</p>
		<p>
				<font face="Arial">　　　x.　注意存储过程中参数和数据类型的关系。</font>
		</p>
		<p>
				<font face="Arial">　　　xi.　注意insert、update操作的数据量，防止与其他应用冲突。如果数据量超过200个数据页面（400k），那么系统将会进行锁升级，页级锁会升级成表级锁。 　　</font>
		</p>
		<p>
				<font face="Arial">　　b)索引的使用规范：</font>
		</p>
		<p>
				<font face="Arial">　　　i.　索引的创建要与应用结合考虑，建议大的OLTP表不要超过6个索引。</font>
		</p>
		<p>
				<font face="Arial">　　　ii.　尽可能的使用索引字段作为查询条件，尤其是聚簇索引，必要时可以通过index index_name来强制指定索引</font>
		</p>
		<p>
				<font face="Arial">　　　iii.　避免对大表查询时进行table scan，必要时考虑新建索引。</font>
		</p>
		<p>
				<font face="Arial">　　　iv.　在使用索引字段作为条件时，如果该索引是联合索引，那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引，否则该索引将不会被使用。</font>
		</p>
		<p>
				<font face="Arial">　　　v.　要注意索引的维护，周期性重建索引，重新编译存储过程。　　</font>
		</p>
		<p>
				<font face="Arial">　　c)tempdb的使用规范：</font>
		</p>
		<p>
				<font face="Arial">　　　i.　尽量避免使用distinct、order by、group by、having、join、cumpute，因为这些语句会加重tempdb的负担。</font>
		</p>
		<p>
				<font face="Arial">　　　ii.　避免频繁创建和删除临时表，减少系统表资源的消耗。</font>
		</p>
		<p>
				<font face="Arial">　　　iii.　在新建临时表时，如果一次性插入数据量很大，那么可以使用select into代替create table，避免log，提高速度；如果数据量不大，为了缓和系统表的资源，建议先create table，然后insert。</font>
		</p>
		<p>
				<font face="Arial">　　　iv.　如果临时表的数据量较大，需要建立索引，那么应该将创建临时表和建立索引的过程放在单独一个子存储过程中，这样才能保证系统能够很好的使用到该临时表的索引。</font>
		</p>
		<p>
				<font face="Arial">　　　 v.　如果使用到了临时表，在存储过程的最后务必将所有的临时表显式删除，先truncate table，然后drop table，这样可以避免系统表的较长时间锁定。</font>
		</p>
		<p>
				<font face="Arial">　　　 vi.　慎用大的临时表与其他大表的连接查询和修改，减低系统表负担，因为这种操作会在一条语句中多次使用tempdb的系统表。　　</font>
		</p>
		<p>
				<font face="Arial">　　d)合理的算法使用： 　　</font>
		</p>
		<p>
				<font face="Arial">　　根据上面已提到的SQL优化技术和ASE Tuning手册中的SQL优化内容,结合实际应用,采用多种算法进行比较,以获得消耗资源最少、效率最高的方法。具体可用ASE调优命令：set statistics io on, set statistics time on , set showplan on 等。<br /></font>
		</p>
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/86029.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2006-12-07 10:49 <a href="http://www.blogjava.net/wuxufeng8080/articles/86029.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ibatis分页做法（ZT）</title><link>http://www.blogjava.net/wuxufeng8080/articles/56835.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Wed, 05 Jul 2006 09:53:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/56835.html</guid><description><![CDATA[以前用ibatis做分页的时候,是用的queryforList()方法，后面感觉不好，因为我看过一些ibatis的源码，感觉它好像是用resultset的滚动游标的方式实现的，这样如果数据量大会不会有问题呢？以前用jdbc的时候是用一个stringbuffer来构造oracle(我们用的是oracle,其它数据库有各自的方法)的三层钳套的sql语句的，做ibatis时语句都是写在xml配置文件里面的，不好做这种构造工作。后来想了葛简单的办法:<br />在domain包里面定义一个basedomain类：<br />public class Basedomain {<br /><br />private int start;<br />private int end;<br />/**<br />* @return Returns the end.<br />*/<br />public int getEnd() {<br />return end;<br />}<br />/**<br />* @param end The end to set.<br />*/<br />public void setEnd(int end) {<br />this.end = end;<br />}<br />/**<br />* @return Returns the start.<br />*/<br />public int getStart() {<br />return start;<br />}<br />/**<br />* @param start The start to set.<br />*/<br />public void setStart(int start) {<br />this.start = start;<br />}<br /><br />}<br />包含两个成员变量<br />start: 取数据时的起始位置<br />end： 取数据时的结束位置<br /><br />然后各个实体类将继承这个basedomain类<br />然后在要分页的sql语句里面加上三层钳套的sql语句，两个rownum参数分别就是上面的start和end。<br />以下是一个示例：<br />select * from (select my_table.*,rownum as my_rownum from (<br />select name,password from user<dynamic prepend="WHERE"><isnotempty prepend="AND" property="id"> </isnotempty></dynamic> order by id desc<br /><!--[CDATA[) my_table where rownum<#end# )where my_rownum>=#start#]]--><![CDATA[ ) my_table where rownum<#end# )where my_rownum>=#start# ]]&gt;&nbsp;&nbsp;&nbsp; <BR>这就是我的ibatis分页的办法，不知道大家觉得如何？呵呵!<BR>br /&gt;我的msn:luyongfugx@hotmail.com</html>]]&gt;<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/56835.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2006-07-05 17:53 <a href="http://www.blogjava.net/wuxufeng8080/articles/56835.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Paging of Large Resultsets in ASP.NET</title><link>http://www.blogjava.net/wuxufeng8080/articles/55843.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Thu, 29 Jun 2006 23:46:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/55843.html</guid><description><![CDATA[
		<p>The paging of a large database resultset in Web applications is a well known problem. In short, you don't want all the results from your query to be displayed on a single Web page, so some sort of paged display is more appropriate. While it was not an easy task in the old ASP, the <code>DataGrid</code> control in the ASP.NET simplifies this to a few lines of code. So, the paging is easy in ASP.NET, but the default behavior of the <code>DataGrid</code> is that all resulting records from your query will be fetched from SQL server to the ASP.NET application. If your query returns a million records this will cause some serious performance issues (if you need convincing, try executing such a query in your web application and see the memory consumption of the aspnet_wp.exe in the task manager). That's why a custom paging solution is required where desired behavior is to fetch only the rows from the current page. </p>
		<p>There are numerous articles and posts concerning this problem and several proposed solutions. My goal here is not to present you with an amazing solves-it-all procedure, but to optimize all the existing methods and provide you with a testing application so you can do evaluation on your own. Here is a good starting point article which describes many different approaches and provides some performance test results: <br /><br /><a href="http://www.aspfaq.com/show.asp?id=2120">How do I page through a recordset?</a><br /><br />I was not satisfied with the most of them. First, half of the methods use old ADO and are clearly written for the "old" ASP. The rest of the methods are SQL server stored procedures. Some of them yield poor response times as you can see from the author’s performance results at the bottom of the page, but several have caught my attention. </p>
		<h2>Generalization</h2>
		<p>The three methods I decided to closely look into are the ones the author calls <code>TempTable</code>, <code>DynamicSQL</code> and <code>Rowcount</code>. I'll refer to the second method as the <code>Asc-Desc</code> method in the rest of this text. I don't think <i>DynamicSQL</i> was a good name, because you can apply dynamic SQL logic to the other methods too. The general problem with all these stored procedures is that you have to assess which columns you'll allow sorting for and that won't probably be just the PK column(s). This leads to a new set of problems – for each query you want to display via paging you must have as many different paging queries as you have different sorting columns. This means that you will either have a different stored procedure (regardless of the paging method applied) for each sorting column or you'll try to generalize this to only one stored procedure with the help of dynamic SQL. This has a slight performance impact, but increases maintainability if you need to display many different queries using this approach. Thus, I’ll try to generalize all of the stored procedures in this text with dynamic SQL, but in some cases it will be possible to achieve only a certain level of generalization, so you’ll still have to write separate stored procedures for some complex queries. </p>
		<p>The second problem with allowing other sorting columns beside the PK column(s) is that if those columns are not indexed in some way, none of these methods will help. In all of them a paged source must be sorted first and the cost of using ordering by non-indexed column is immense for large tables. The response times are so high that all the procedures are practically unusable in this case (the response varies from couple of seconds to couple of minutes depending on the size of the tables and the starting record being fetched). The indexing of other columns brings more performance issues and may be undesirable, for example it might significantly slow you down in a situation where you have a lot of daily imports. </p>
		<h2>TempTable</h2>
		<p>The first one I would comment on is the <code>TempTable</code> method. This is actually a widely proposed solution and I encountered it several times. Here is another article that describes it along with the explanation and a sample how to use custom paging with the <code>DataGrid</code>: <br /><br /><a href="http://www.dotnetjunkies.com/Tutorial/EA868776-D71E-448A-BC23-B64B871F967F.dcik">ASP.NET DataGrid Paging Part 2 - Custom Paging</a><br /><br />The methods in both articles could be optimized with just the Primary Key data copied to the temp table and then doing the join with the main query. Therefore, the essence of this method would be the following </p>
		<pre lang="sql">
				<span class="vb-function">CREATE</span>
				<span class="vb-function">TABLE</span> #Temp (
    ID <span class="cpp-keyword">int</span><span class="vb-function">IDENTITY</span><span class="vb-function">PRIMARY</span><span class="vb-function">KEY</span>,
    PK  /* here goes PK type */
)

<span class="vb-function">INSERT</span><span class="vb-function">INTO</span> #Temp <span class="vb-function">SELECT</span> PK <span class="vb-function">FROM</span> Table <span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn

<span class="vb-function">SELECT</span> ... <span class="vb-function">FROM</span> Table <span class="vb-function">JOIN</span> #Temp temp <span class="vb-function">ON</span> Table.PK = temp.PK <span class="vb-function">ORDER</span><span class="vb-function">BY</span> temp.ID <br /><span class="vb-function">WHERE</span> ID &gt; @StartRow <span class="vb-function">AND</span> ID &lt; @EndRow
</pre>The method can be optimized further by copying the rows to the temp table until the end paging row is reached (<code>SELECT TOP EndRow...</code>), but the point is that in the worst case – for a table with 1 million records you end up with 1 million records in a temp table as well.  Considering all this and having looked upon the results in the article above, I decided to discard this method from my tests. 
<h2>Asc-Desc</h2><p>This method uses default ordering in a subquery and then applies the reverse ordering. The principle goes like this </p><pre lang="sql"><span class="vb-function">DECLARE</span> @temp <span class="vb-function">TABLE</span> (
    PK  /* PK Type */ <span class="vb-function">NOT</span><span class="vb-function">NULL</span><span class="vb-function">PRIMARY</span> 
)

<span class="vb-function">INSERT</span><span class="vb-function">INTO</span> @temp 
<span class="vb-function">SELECT</span> TOP @PageSize PK <span class="vb-function">FROM</span> (
    <span class="vb-function">SELECT</span> TOP (@StartRow + @PageSize) 
    PK, 
    SortColumn /*If sorting column <span class="vb-function">is</span> defferent <span class="vb-function">from</span> the PK, SortColumn must <br />                 be fetched as well, otherwise just the PK <span class="vb-function">is</span> necessary */ 
    <span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn /* default <span class="vb-function">order</span> – typically ASC */) 
<span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn /* reversed default <span class="vb-function">order</span> – typically DESC */

<span class="vb-function">SELECT</span> ... <span class="vb-function">FROM</span> Table <span class="vb-function">JOIN</span> @Temp temp <span class="vb-function">ON</span> Table.PK = temp.PK <br /><span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn /* default <span class="vb-function">order</span> */</pre>Full Code – <a href="http://www.codeproject.com/aspnet/PagingLarge/Paging_Asc_Desc.zip">Paging_Asc_Desc</a><br /><h2>RowCount</h2><p>The base logic of this method relies on the SQL <code>SET ROWCOUNT</code> expression to both skip the unwanted rows and fetch the desired ones: </p><pre lang="sql"><span class="vb-function">DECLARE</span> @Sort /* the type of the sorting column */
<span class="vb-function">SET</span> ROWCOUNT @StartRow
<span class="vb-function">SELECT</span> @Sort = SortColumn <span class="vb-function">FROM</span> Table <span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn
<span class="vb-function">SET</span> ROWCOUNT @PageSize
<span class="vb-function">SELECT</span> ... <span class="vb-function">FROM</span> Table <span class="vb-function">WHERE</span> SortColumn &gt;= @Sort <span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn</pre>Full Code – <a href="http://www.codeproject.com/aspnet/PagingLarge/Paging_RowCount.zip">Paging_RowCount</a><br /><h2>SubQuery</h2><p>There are 2 more methods I’ve taken into consideration, and they come from different resources. The first one is well known triple query or the <code>SubQuery</code> method. The most thorough approach is the one I’ve found in the following article <br /><br /><a href="http://www.winnetmag.com/Article/ArticleID/40505/40505.html">Server-Side Paging with SQL Server</a><br /><br />Although you'll need to be subscribed, a .zip file with the <code>SubQuery</code> stored procedure variations is available. The <i>Listing_04.SELECT_WITH_PAGINGStoredProcedure.txt</i> file contains the complete generalized dynamic SQL. I used a similar generalization logic with all other stored procedures in this text. Here is the principle followed by the link to the whole procedure (I shortened the original code a bit, because a recordcount portion was unnecessary for my testing purposes). </p><pre lang="sql"><span class="vb-function">SELECT</span> ... <span class="vb-function">FROM</span> Table <span class="vb-function">WHERE</span> PK IN 
    (<span class="vb-function">SELECT</span> TOP @PageSize PK <span class="vb-function">FROM</span> Table <span class="vb-function">WHERE</span> PK <span class="vb-function">NOT</span> IN
        (<span class="vb-function">SELECT</span> TOP @StartRow PK <span class="vb-function">FROM</span> Table <span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn)
    <span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn)
<span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn</pre>Full Code – <a href="http://www.codeproject.com/aspnet/PagingLarge/Paging_SubQuery.zip">Paging_SubQuery</a><br /><h2>Cursor</h2><p>I’ve found the last method while browsing through the Google groups, you can find the original thread <a href="http://groups.google.com/groups?hl=en&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;threadm=OzkQuI9%24CHA.2328%40TK2MSFTNGP10.phx.gbl&amp;rnum=5&amp;prev=/groups%3Fhl%3Dbg%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3DOzkQuI9%2524CHA.2328%2540TK2MSFTNGP10.phx.gbl%26rnum%3D5">here</a>. This method uses a server-side dynamic cursor. A lot of people tend to avoid cursors, they usually have poor performance because of their non-relational, sequential nature. The thing is that paging IS a sequential task and whatever method you use you have to somehow reach the starting row. In all the previous methods this is done by selecting all rows preceding the starting row plus the desired rows and then discarding all the preceding rows. Dynamic cursor has the <code>FETCH RELATIVE</code> option which does the “magic” jump. The base logic goes like this </p><pre lang="sql"><span class="vb-function">DECLARE</span> @PK /* PK Type */
<span class="vb-function">DECLARE</span> @tblPK <span class="vb-function">TABLE</span> (
    PK /* PK Type */ <span class="vb-function">NOT</span><span class="vb-function">NULL</span><span class="vb-function">PRIMARY</span><span class="vb-function">KEY</span>
)

<span class="vb-function">DECLARE</span> PagingCursor CURSOR DYNAMIC READ_ONLY FOR
<span class="vb-function">SELECT</span> @PK <span class="vb-function">FROM</span> Table <span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn

OPEN PagingCursor
FETCH RELATIVE @StartRow <span class="vb-function">FROM</span> PagingCursor <span class="vb-function">INTO</span> @PK

<span class="vb-function">WHILE</span> @PageSize &gt; <span class="vb-literal">0</span><span class="vb-function">AND</span> @@FETCH_STATUS = <span class="vb-literal">0</span><span class="vb-function">BEGIN</span><span class="vb-function">INSERT</span> @tblPK(PK) VALUES(@PK)
    FETCH NEXT <span class="vb-function">FROM</span> PagingCursor <span class="vb-function">INTO</span> @PK
    <span class="vb-function">SET</span> @PageSize = @PageSize - <span class="vb-literal">1</span><span class="vb-function">END</span>

CLOSE PagingCursor
DEALLOCATE PagingCursor

<span class="vb-function">SELECT</span> ... <span class="vb-function">FROM</span> Table <span class="vb-function">JOIN</span> @tblPK temp <span class="vb-function">ON</span> Table.PK = temp.PK <br /><span class="vb-function">ORDER</span><span class="vb-function">BY</span> SortColumn</pre>Full Code – <a href="http://www.codeproject.com/aspnet/PagingLarge/Paging_Cursor.zip">Paging_Cursor</a><br /><h2>Generalization of Complex Queries</h2><p>As pointed out before, all the procedures are generalized with dynamic SQL, thus, in theory, they can work with any kind of complex query. Here is a complex query sample that works with <code>Northwind</code> database. </p><pre lang="sql"><span class="vb-function">SELECT</span> Customers.ContactName <span class="vb-function">AS</span> Customer, 
       Customers.Address + ', ' + Customers.City + ', ' + <br />                                                Customers.Country <span class="vb-function">AS</span> Address, 
       SUM([Order Details].UnitPrice*[Order Details].Quantity) <span class="vb-function">AS</span><br />                                                          [Total <span class="cpp-keyword">money</span> spent] 
<span class="vb-function">FROM</span> Customers
<span class="vb-function">INNER</span><span class="vb-function">JOIN</span> Orders <span class="vb-function">ON</span> Customers.CustomerID = Orders.CustomerID
<span class="vb-function">INNER</span><span class="vb-function">JOIN</span> [Order Details] <span class="vb-function">ON</span> Orders.OrderID = [Order Details].OrderID
<span class="vb-function">WHERE</span> Customers.Country &lt;&gt; 'USA' <span class="vb-function">AND</span> Customers.Country &lt;&gt; 'Mexico'
<span class="vb-function">GROUP</span><span class="vb-function">BY</span> Customers.ContactName, Customers.Address, Customers.City, <br />         Customers.Country 
<span class="vb-function">HAVING</span> (SUM([Order Details].UnitPrice*[Order Details].Quantity))&gt;<span class="vb-literal">1000</span><span class="vb-function">ORDER</span><span class="vb-function">BY</span> Customer DESC, Address DESC</pre>The paging stored procedure call that returns the second page looks like this <pre lang="sql">EXEC ProcedureName
/* Tables */
'Customers
<span class="vb-function">INNER</span><span class="vb-function">JOIN</span> Orders <span class="vb-function">ON</span> Customers.CustomerID = Orders.CustomerID
<span class="vb-function">INNER</span><span class="vb-function">JOIN</span> [Order Details] <span class="vb-function">ON</span> Orders.OrderID = [Order Details].OrderID',
/* PK */
'Customers.CustomerID',
/* <span class="vb-function">ORDER</span><span class="vb-function">BY</span> */
'Customers.ContactName DESC, Customers.Address DESC',
/* PageNumber */
<span class="vb-literal">2</span>,
/* Page Size */
<span class="vb-literal">10</span>,
/* Fields */
'Customers.ContactName <span class="vb-function">AS</span> Customer,
Customers.Address + '', '' + Customers.City + '', '' + Customers.Country <br /><span class="vb-function">AS</span> Address, 
SUM([Order Details].UnitPrice*[Order Details].Quantity) <span class="vb-function">AS</span> [Total <span class="cpp-keyword">money</span> spent]',
/* Filter */
'Customers.Country &lt;&gt; ''USA'' <span class="vb-function">AND</span> Customers.Country &lt;&gt; ''Mexico''',
/*Group By*/
'Customers.CustomerID, Customers.ContactName, Customers.Address, <br /> Customers.City, Customers.Country 
<span class="vb-function">HAVING</span> (SUM([Order Details].UnitPrice*[Order Details].Quantity))&gt;<span class="vb-literal">1000</span>'</pre><p>Note that in the original query, aliases are used in the <code>ORDER BY</code> clause. You can't do that in paging procedures, because the most time-consuming task in all of them is skipping rows preceding the starting row. This is done in various ways, but the principle is not to fetch all the required fields at first, but only the PK column(s) (in case of <code>RowCount</code> method the sorting column), which speeds up this task. All required fields are fetched only for the rows that belong to the requested page. Therefore, field aliases don't exist until the final query, and sorting columns have to be used earlier (in row skipping queries). </p><p>The <code>RowCount</code> procedure has another problem, it is generalized to work with only one column in the <code>ORDER BY</code> clause. The same goes for <code>Asc-Desc</code> and <code>Cursor</code> methods, though they can work with several ordering columns, but require that only one column is included in the PK. I guess this could be solved with more dynamic SQL, but in my opinion it is not worth the fuss. Although these situations are highly possible, they are not that frequent. Even if they are, you can always write a separate paging procedure following the principles above. </p><h2>Performance Testing</h2><p>I used these 4 methods in my tests, if you have a better one, I’d be glad to know about it. Nevertheless, I wanted to compare these methods and measure their performance. The first thought was to write an ASP.NET test application with paged DataGrid and then measure page response. Still, this wouldn’t reflect the true response time of the stored procedures, so the console application seemed more appropriate. I also included a web application, not for performance testing, but rather as an example of how DataGrid custom paging works with these stored procedures. They are both incorporated in the <a href="http://www.codeproject.com/aspnet/PagingLarge/PagingTest.zip">PagingTest Solution</a>.</p><p>I used the auto generated large table for my tests and inserted around 500 000 records in it. If you don’t have a large table to experiment on, you can download the script for a table design and stored procedure for data generation <a href="http://www.codeproject.com/aspnet/PagingLarge/Paging_LargeTable.zip">here</a>. I didn't want an identity column for my PK, I used the <code>uniqueidentifier</code> instead. If you'll use this script, you may consider to add an identity after you generate the table. It will add numbers sorted by PK and you'll have an indication that correct page is fetched when you call a paging procedure with PK sorting. </p><p>The idea behind performance testing was to call a specific stored procedure many times through a loop and then measure the average response time. Also, in order to remove caching deviations and to model the real situation more accurately – multiple calls to a stored proc with the same page fetched each time seemed inappropriate. Thus, a random sequence of the same stored procedure with a set of different page numbers was required. Of course, a set of different page numbers assumes fixed number of pages (10 – 20) where each page would be fetched many times, but in a random sequence.</p><p>It’s not hard to notice that response times depend on the distance of the fetched page from the beginning of the resultset. The further the starting record is, more records need to be skipped. This is the reason I didn’t include first 20 pages in my random sequence. Instead I used the set of 2<sup>N</sup> pages. A loop was set to a (number of different pages)*1000. So, every page was fetched around 1000 times (more or less because of a random distribution). </p><h2>Results</h2>Here are the results I've got - <a href="http://www.codeproject.com/aspnet/PagingLarge/Paging_Results.zip">Paging_Results (MS Excell file)</a><br /><img alt="" src="http://www.codeproject.com/aspnet/PagingLarge/Paging_pk.gif" /><br /><img alt="" src="http://www.codeproject.com/aspnet/PagingLarge/Paging_pk-lower.gif" /><br /><img alt="" src="http://www.codeproject.com/aspnet/PagingLarge/Paging_indexed.gif" /><br /><img alt="" src="http://www.codeproject.com/aspnet/PagingLarge/Paging_indexed-lower.gif" /><br /><img alt="" src="http://www.codeproject.com/aspnet/PagingLarge/Paging_overall.gif" /><h2>Conclusion</h2><p>The methods performed in the following order, starting from the best one - <code>RowCount</code>, <code>Cursor</code>, <code>Asc-Desc</code> and <code>Subquery</code>. The behavior in the lower portion was especially interesting, because in many real situations you'll browse beyond the first five pages rarely, so the <code>Subquery</code> method might satisfy your needs in those cases. It all depends on the size of your resultset and the prediction how frequently will the distant pages be fetched. You might use the combination of methods as well. As for myself, I decided to use the <code>RowCount</code> method wherever possible. It beaves quite nice, even for the first page. The <i>"wherever possible"</i> part stands for some cases where it's hard to generalize this method, then I would use the <code>Cursor</code> (possibly combined with the <code>SubQuery</code> for the first couple of pages). </p><h2>Update 2004-05-05</h2><p>The main reason I wrote this article was the feedback from the vast programming community. In a couple of weeks I'll be starting work on a new project. The preliminary analysis showed that there's going to be a couple of very large tables involved. These tables will be used in many complex joined queries and their results will be displayed in the ASP.NET application (with sorting and paging enabled). That's why I invested some time in research and pursue for the best paging method. It wasn't just the performance that interested me, but also the usability and maintainability. 
</p><p>Now the invested time has started to pay off already. You can find a post by C. v. Berkel below (many thanks) in which he found a flaw in the <code>RowCount</code> method. It won't work correctly if the sorting column is not unique. The <code>RowCount</code> method performed the best in my tests, but now I am seriously considering not using it at all. In most cases sorting columns (besides the PK) won't be unique. This leaves me with the <code>Cursor</code> method as the fastest and applicable to most situations. It can be combined with the <code>SubQuery</code> method for the first couple of pages and possibly with the <code>RowCount</code> method for unique sorting columns. 
</p><p>Another thing which may be worth mentioning is that there's a tiny flaw in the <code>Asc-Desc</code> method as well. It always returns the <code>PageSize</code> number of records for the last page and not the actual number (which may be lower than the <code>PageSize</code>). The correct number can be calculated but since I don't intend to use this procedure (because of how it performed), I didn't want to improve it any further. <br /><br /><a href="http://www.codeproject.com/aspnet/PagingLarge.asp">http://www.codeproject.com/aspnet/PagingLarge.asp</a></p><img src ="http://www.blogjava.net/wuxufeng8080/aggbug/55843.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2006-06-30 07:46 <a href="http://www.blogjava.net/wuxufeng8080/articles/55843.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL 分页语句</title><link>http://www.blogjava.net/wuxufeng8080/articles/55805.html</link><dc:creator>风人园</dc:creator><author>风人园</author><pubDate>Thu, 29 Jun 2006 11:13:00 GMT</pubDate><guid>http://www.blogjava.net/wuxufeng8080/articles/55805.html</guid><description><![CDATA[
		<p>首页的算法很简单<br /> select top 页大小 * from 表名 order by 主键</p>
		<p>对于带滚动条的表格，数据是一页一页顺序滚动，即使拖动滚动条，也可以一页页滚动到所选的位置，当拖动到一个新页时的算法为<br />select top 页大小 * from 表名 where 主键 &gt; 上一页末记录的主键 order by 主键</p>
<img src ="http://www.blogjava.net/wuxufeng8080/aggbug/55805.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wuxufeng8080/" target="_blank">风人园</a> 2006-06-29 19:13 <a href="http://www.blogjava.net/wuxufeng8080/articles/55805.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>