sql server2005查询优化建议

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->

查询优化的目的是提高数据检索速度,提高数据检索意味着减少磁盘 IO 读取或者逻辑内存读取次数,这需要从两个方面入手:数据要尽可能的缓存到内存、尽可能的使用索引。内存的问题可以参见 <!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->: http://msdn.microsoft.com/zh-cn/library/ms188284.aspx ,本文主要是体现如何使用索引来提高速度。具体方法:

<!-- [if !supportLists]-->1) <!-- [endif]-->养成良好的编程习惯,比如SARG 优化(这个好像来自于sybase

<!-- [if !supportLists]-->2) <!-- [endif]-->使用查询计划,看某一个查询是否是使用了索引,是否是使用了临时表,尽量使用索引,避免临时表

<!-- [if !supportLists]-->3) <!-- [endif]-->使用查询优化顾问,它可以帮助你判断查询是否优化,并提示你建立索引

<!-- [if !supportLists]-->4) <!-- [endif]-->使用SET STATISTICS IO 命令,查看磁盘读取次数,尽量物理磁盘和逻辑读取次数少

<!-- [if !supportLists]-->5) <!-- [endif]-->建立主键,主键是聚簇索引,数据行基于聚集索引键按顺序存储

<!-- [if !supportLists]-->6) <!-- [endif]-->使用组合索引,避免OR 操作符使用临时表,select * from table where a=1 or b=2, 可以考虑创建a and b 的组合索引

<!-- [if !supportLists]-->7) <!-- [endif]-->大数据量表使用已分区表,具体参见:

http://msdn.microsoft.com/zh-cn/library/ms345146 SQL.90 .aspx

<!-- [if !supportLists]-->8) <!-- [endif]-->使用存储过程代替复杂sql 语句

<!-- [if !supportLists]-->9) <!-- [endif]-->存储过程中使用表变量而不使用临时表,通常,表变量可提供更有效的查询 处理: table 变量的行为类似于局部变量,有明确定义的作用域;在存储过程中使用 table 变量与使用临时表相比,减少了存储过程的重新编译量;涉及 table 变量的事务只在 table 变量更新期间存在。因此减少了 table 变量对锁定和记录资源的需求。但不能显式创建 table 变量的索引,也不保留 table 变量的任何统计信息。在某些情况下,可以通过改用支持索引和统计信息的临时表来改善性能。具体参见:

http://msdn.microsoft.com/zh-cn/library/ms175010.aspx

<!-- [if !supportLists]-->10) <!-- [endif]-->当必须对临时表显式地创建索引时,或多个存储过程或函数必须使用表值时,临时表很有用。使用大容量日志模式可以提高临时表大量插入数据的效率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值