﻿<?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-志当存高远,功到自然成!-随笔分类-Sybase</title><link>http://www.blogjava.net/shanben/category/32687.html</link><description>少年强则中国强,少年进步则中国进步!</description><language>zh-cn</language><lastBuildDate>Thu, 04 Sep 2008 08:16:32 GMT</lastBuildDate><pubDate>Thu, 04 Sep 2008 08:16:32 GMT</pubDate><ttl>60</ttl><item><title>Sybase ASE表分區的使用 </title><link>http://www.blogjava.net/shanben/archive/2008/09/04/226863.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Thu, 04 Sep 2008 02:52:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/09/04/226863.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/226863.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/09/04/226863.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/226863.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/226863.html</trackback:ping><description><![CDATA[最近想對一個表進行分區,在sybase版轉了半天,沒找著詳細介紹表分區有關的帖子. 終於找著一篇比較詳細的,故貼之:<br />
<br />
Sybase ASE表分區的使用 <br />
<br />
表分區是目前各主流數據庫都提供的常用技術，各大數據庫基於不同的體系結構提供了各具特色的實現，如果能合理使用這種技術，將為系統性能的提升帶來意想不到的效果，本文以Sybase ASE數據庫為例來說明這種技術的使用方法。 <br />
<br />
<br />
在Sybase ASE中，未分區的不帶聚簇索引的表有一個雙向的數據庫頁鏈鏈接。在插入數據行時，將查找並鎖定頁鏈的最後一頁，如果最後一頁空間用盡，Sybase ASE將分配新的一頁，如果對表進行大量頻繁的數據插入，將引起對最後一頁鎖資源的競爭，如果一個事務（Transaction）正在使用最後一頁的排它鎖，那麼其他向同一張表插入數據的事務必須等待，直到該事務結束釋放資源，解除排它鎖。 <br />
<br />
<br />
Sybase ASE從11.5開始提供新的特性表分區。表分區將產生附加的頁鏈，每個頁鏈有自己的最後一頁，這樣插入操作可以獲得多個最後一頁，實現並發操作，從而提高數據庫插入性能。若包含表的段分佈在多個物理設備上，表分區通過降低服務器從高速緩存向磁盤進行數據刷新的I/O衝突而提高數據插入性能。對頻繁地追加數據到表中或進行大塊數據的拷入、拷出操作，採用表分區技術，將能極大地提升系統性能。 <br />
<br />
<br />
劃分表分區的操作步驟如下： <br />
<br />
<br />
1. 在同一數據庫設備上劃分表分區時,先創建表，再用alter table 的 partition 語句劃分表分區。 <br />
<br />
<br />
create table opt_plnexrcd ( per_date datetime, per_sndstn char(4), ... ) <br />
<br />
<br />
alter table opt_plnexrcd partition 5 <br />
<br />
<br />
2. 在多個數據庫設備上劃分表分區時，先建一個數據庫段在多個數據庫上，然後再將表建在該段上，最後對表分區。 <br />
<br />
<br />
sp_addsegment seg1, oil, oil_data1 <br />
<br />
<br />
sp_extendsement seg1, oil, oil_data2 <br />
<br />
<br />
... <br />
<br />
<br />
sp_extendsement seg1, oil, oil_data5 <br />
<br />
<br />
create table opt_plnexrcd ( per_date datetime, per_sndstn char(4), ... ) on seg1 <br />
<br />
<br />
或建完表後執行sp_placeobject: <br />
<br />
<br />
sp_placeobject seg1, opt_plnexrcd <br />
<br />
<br />
最後對表分區: <br />
<br />
<br />
alter table opt_plnexrcd partition 5 <br />
<br />
<br />
如果表分為5個分區，表所在的段又分跨5個數據庫設備，那麼每個分區將分別映射到該段所在的5個數據庫設備上。一般劃分表分區的原則是：表分區數等於或大於表所在段分跨的數據庫設備數。但要注意有些表不能分區，它們是系統表、正在使用的表、臨時表和有聚簇索引的表。 <br />
<br />
<br />
Sybase ASE使用保存在分區控制頁上的統計數據，來評測基於分區的並行掃瞄方式，以挑選最優執行計劃。如果表中數據在多個分區上是分佈均衡的，表分區統計數據是精確的，那麼掃瞄效率就比較高，並行處理就有很好的性能，所以表分區的維護對於並行查詢處理十分重要。Sybase ASE的異常停止和某些情況下的事務回退都可能造成分區統計數據不精確，Sybase ASE提供了兩個系統函數data_pgs和ptn_data_pgs,用它們可分別計算數據頁面數量。如果兩者不相同，說明分區統計不精確，需要維護，Sybase ASE 提供如下命令來更新分區和頁面統計數據： <br />
<br />
<br />
1.更新每個分區的頁面統計數據： <br />
<br />
<br />
update partition statistics <br />
<br />
<br />
2.更新表上索引的分佈頁面，同時更新各個表分區的控制頁面的統計數據： <br />
<br />
<br />
update all statistics table_name <br />
<br />
<br />
3.顯示表分區的當前信息： <br />
<br />
<br />
sp_helpartition <br />
<br />
<br />
如果對分區的表進行了大量的插入、更新和刪除後，數據有可能分佈不均勻，使用BCP加載大量數據後，也可能會出現數據分佈不均勻，此外一些其他原因也可能導致數據分佈不均勻。平衡分區數據有兩種方法：一是對有數據的表建聚簇索引，二是使用並行塊拷貝，指定數據加載到某個分區的方法，Sybase ASE 新的BCP允許用戶指定分區，把數據加載到指定的分區中。
<img src ="http://www.blogjava.net/shanben/aggbug/226863.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-09-04 10:52 <a href="http://www.blogjava.net/shanben/archive/2008/09/04/226863.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用DM水平分区优化系统</title><link>http://www.blogjava.net/shanben/archive/2008/09/02/226451.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Tue, 02 Sep 2008 09:38:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/09/02/226451.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/226451.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/09/02/226451.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/226451.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/226451.html</trackback:ping><description><![CDATA[为了使用户的大量的数据在读写操作和查询中速度更快，DM提供了对表和索引进行分区的技术，以改善超大规模应用的性能并方便数据管理。
<p>　　<strong>实现水平分区以后，可以达到以下目的：</strong></p>
<p>　　1. 增强可用性：如果表的某个分区出现故障，表在其他分区的数据仍然可用;</p>
<p>　　2. 维护方便：如果表的某个分区出现故障，需要修复数据，只修复该分区即可;</p>
<p>　　3. 均衡I/O：可以把不同的分区映射到磁盘以平衡I/O，改善整个系统性能;</p>
<p>　　4. 改善查询性能：对分区对象的查询可以仅搜索自己关心的分区，提高检索速度。</p>
<p>　　<strong>DM数据库提供对表的分区方法有两种：</strong></p>
<p>　　1、范围分区：范围分区就是对表中的某个值的范围进行分区，根据某个值的范围，决定将该数据存储在哪个分区上。</p>
<p>　　2、HASH分区：HASH分区是通过指定分区编号来均匀分布数据的一种分区类型，因为通过在I/O设备上进行散列分区，使得这些分区大小一致。</p>
<p>　<strong>　除了对表进行分区，还可以对索引进行分区。DM支持下面三种类型的分区索引：</strong></p>
<p>　　1、本地索引：其分区方式与其所在基础表的分区方式一模一样的索引。本地索引的每个分区仅对应于其所在基础表的一个分区。</p>
<p>　　2、全局分区索引：使用不同于其所在表的分区键进行分区的索引，其所在表可以是分区表或非分区表。</p>
<p>　　3、全局非分区索引：全局非分区索引基本上和非分区表的索引一样。索引结构是不分区的。</p>
<p>　　<strong>1.1 水平分区表的创建</strong></p>
<p>　　在介绍水平分区表的创建之前。先要了解分区键的概念。</p>
<p>　　分区键：分区键是分区表的某一列，它决定了分区表中每一行数据划分到哪个分区。该列的数据类型为DM常用数据类型，不支持BLOB、TEXT、ROWID、BIT、BINARY、VARBINARY、时间间隔等类型和用户自定义类型。分区键不可以删除或修改。</p>
<p>　　范围分区通过LEFT或RIGHT关键字来指定每个值是第一个分区的上边界 (LEFT) 还是第二个分区的下边界 (RIGHT)。边界值不要求是递增的序列，但是不能重复，边界值不可以为NULL，支持使用常量表达式或返回常量值的函数来指定分区的边界值。</p>
<p>　　如：建立范围分区表CREATE TABLE TAB1(C1 INT, C2 INT, C3 VARCHAR(100)) PARTITION BY RANGE (C1) LEFT FOR VALUES (1, 100, 10000);</p>
<p>　　PARTITION NO1234</p>
<p>　　VALUESVAL&lt;=11
<p>　　建立HASH分区表CREATE TABLE TAB2(C1 INT, C2 INT, C3 VARCHAR(100)) PARTITION BY HASH(C1) PARTITIONS 3; TAB2以C1为分区键，且分区数为3。</p>
<p>　　范围区间或HASH分区数目不能超过64个。不同的分区可以指定到不同的文件组。。 </p>
<p>　　<strong>1.2 分区索引的创建</strong></p>
<p>　　本地索引、全局非分区索引、全局分区索引可以建立在范围分区表或HASH分区表上，普通表上可以建立全局分区索引。</p>
<p>　　如：CREATE TABLE T(C1 INT, C2 INT, C3 VARCHAR(100)) PARTITION BY RANGE(C1) LEFT FOR VALUES(1, 100);</p>
<p>　　CREATE INDEX T_IND1 ON T (C2); -- 本地索引，系统将自动创建3个分区子索引，这些分区子索引按照C1进行分区。</p>
<p>　　CREATE NOT PARTIAL INDEX T_IND2 ON T (C2); -- 全局非分区索引，索引不分区。</p>
<p>　　CREATE INDEX T_IND3 ON T (C2) PARTITION BY RANGE(C2) LEFT FOR VALUES(5, 55); -- 全局分区索引，索引按照C2列分区</p>
<p>　　<strong>1.3 水平分区表和分区索引的维护</strong></p>
<p>　　DM支持对范围分区表和范围分区表上的索引进行合并和拆分。HASH分区表和HASH分区表上的索引不支持合并和拆分，本地分区索引不允许合并和拆分。</p>
<p>　　分区合并通过指定分区号将相邻的两个分区合并成一个分区。MERGE分区时，相邻的两个分区可以是相同的文件组，也可以是不同的文件组。</p>
<p>　　分区拆分通过指定常量表达式值将某一个分区拆分成两个分区，指定的常量表达式值不能是原有的分区范围值。SPLIT分区时，可以指定新的文件组，或者不指定文件组。</p>
<p>　　<strong>1.4 水平分区的使用</strong></p>
<p>　　水平分区表、分区索引的使用和普通表、普通索引的使用一样。用户通过SQL语句建立范围分区和HASH分区表，以及水平分区索引以后，普通的INSERT、DELETE、UPDATE、SELECT语句的使用不受任何影响。</p>
<p>　　分区拆分或合并以后，普通的INSERT、DELETE、UPDATE、SELECT语句的使用不受任何影响。</p>
<img src ="http://www.blogjava.net/shanben/aggbug/226451.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-09-02 17:38 <a href="http://www.blogjava.net/shanben/archive/2008/09/02/226451.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sybase数据库存储过程编写经验以及方法</title><link>http://www.blogjava.net/shanben/archive/2008/09/02/226399.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Tue, 02 Sep 2008 07:35:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/09/02/226399.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/226399.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/09/02/226399.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/226399.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/226399.html</trackback:ping><description><![CDATA[<p style="text-indent: 2em"><strong>一、前言</strong>：在经过一段时间的存储过程开发之后，写下了一些开发时候的小结和经验与大家共享，希望对大家有益，主要是针对Sybase和SQL Server数据库，但其它数据库应该有一些共性。
<p style="text-indent: 2em">
<p style="text-indent: 2em"><strong>二、适合读者对象</strong>：数据库开发程序员，数据库的数据量很多，涉及到对SP（存储过程）的优化的项目开发人员，对数据库有浓厚兴趣的人。
<p style="text-indent: 2em">
<p style="text-indent: 2em"><strong>三、介绍</strong>：在数据库的开发过程中，经常会遇到复杂的业务逻辑和对数据库的操作，这个时候就会用SP来封装数据库操作。如果项目的SP较多，书写又没有一定的规范，将会影响以后的系统维护困难和大SP逻辑的难以理解，另外如果数据库的数据量大或者项目对SP的性能要求很，就会遇到优化的问题，否则速度有可能很慢，经过亲身经验，一个经过优化过的SP要比一个性能差的SP的效率甚至高几百倍。
<p style="text-indent: 2em">
<p style="text-indent: 2em"><strong>四、内容</strong>：
<p style="text-indent: 2em">
<p style="text-indent: 2em">1、开发人员如果用到其他库的Table或View，务必在当前库中建立View来实现跨库操作，最好不要直接使用&#8220;databse.dbo.table_name&#8221;，因为sp_depends不能显示出该SP所使用的跨库table或view，不方便校验。
<p style="text-indent: 2em">
<p style="text-indent: 2em">2、开发人员在提交SP前，必须已经使用set showplan on分析过查询计划，做过自身的查询优化检查。
<p style="text-indent: 2em">
<p style="text-indent: 2em">3、高程序运行效率，优化应用程序，在SP编写过程中应该注意以下几点：
<p style="text-indent: 2em">
<p style="text-indent: 2em">a)SQL的使用规范：
<p style="text-indent: 2em">
<p style="text-indent: 2em">i.尽量避免大事务操作，慎用holdlock子句，提高系统并发能力。
<p style="text-indent: 2em">
<p style="text-indent: 2em">ii.尽量避免反复访问同一张或几张表，尤其是数据量较大的表，可以考虑先根据条件提取数据到临时表中，然后再做连接。
<p style="text-indent: 2em">
<p style="text-indent: 2em">iii.尽量避免使用游标，因为游标的效率较差，如果游标操作的数据超过1万行，那么就应该改写；如果使用了游标，就要尽量避免在游标循环中再进行表连接的操作。
<p style="text-indent: 2em">
<p style="text-indent: 2em">iv.注意where字句写法，必须考虑语句顺序，应该根据索引顺序、范围大小来确定条件子句的前后顺序，尽可能的让字段顺序与索引顺序相一致，范围从大到小。
<p style="text-indent: 2em">
<p style="text-indent: 2em">v.不要在where子句中的&#8220;=&#8221;左边进行函数、算术运算或其他表达式运算，否则系统将可能无法正确使用索引。 vi.尽量使用exists代替select count(1)来判断是否存在记录，count函数只有在统计表中所有行数时使用，而且count(1)比count(*)更有效率。
<p style="text-indent: 2em">
<p style="text-indent: 2em">vii.尽量使用&#8220;&gt;=&#8221;，不要使用&#8220;&gt;&#8221;。
<p style="text-indent: 2em">
<p style="text-indent: 2em">viii.注意一些or子句和union子句之间的替换
<p style="text-indent: 2em">
<p style="text-indent: 2em">ix.注意表之间连接的数据类型，避免不同类型数据之间的连接。
<p style="text-indent: 2em">
<p style="text-indent: 2em">x.注意存储过程中参数和数据类型的关系。
<p style="text-indent: 2em">
<p style="text-indent: 2em">xi.注意insert、update操作的数据量，防止与其他应用冲突。如果数据量超过200个数据页面（400k），那么系统将会进行锁升级，页级锁会升级成表级锁。
<p style="text-indent: 2em">
<p style="text-indent: 2em">b)索引的使用规范：
<p style="text-indent: 2em">
<p style="text-indent: 2em">i.索引的创建要与应用结合考虑，建议大的OLTP表不要超过6个索引。
<p style="text-indent: 2em">
<p style="text-indent: 2em">ii.尽可能的使用索引字段作为查询条件，尤其是聚簇索引，必要时可以通过index index_name来强制指定索引
<p style="text-indent: 2em">iii.避免对大表查询时进行table scan，必要时考虑新建索引。 iv.在使用索引字段作为条件时，如果该索引是联合索引，那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引，否则该索引将不会被使用。 v.要注意索引的维护，周期性重建索引，重新编译存储过程。
<p style="text-indent: 2em">
<p style="text-indent: 2em">c)tempdb的使用规范：
<p style="text-indent: 2em">
<p style="text-indent: 2em">i.尽量避免使用distinct、order by、group by、having、join、cumpute，因为这些语句会加重tempdb的负担。
<p style="text-indent: 2em">
<p style="text-indent: 2em">ii.避免频繁创建和删除临时表，减少系统表资源的消耗。
<p style="text-indent: 2em">
<p style="text-indent: 2em">iii.在新建临时表时，如果一次性插入数据量很大，那么可以使用select into代替create table，避免log，提高速度；如果数据量不大，为了缓和系统表的资源，建议先create table，然后insert。
<p style="text-indent: 2em">
<p style="text-indent: 2em">iv.如果临时表的数据量较大，需要建立索引，那么应该将创建临时表和建立索引的过程放在单独一个子存储过程中，这样才能保证系统能够很好的使用到该临时表的索引。
<p style="text-indent: 2em">
<p style="text-indent: 2em">v.如果使用到了临时表，在存储过程的最后务必将所有的临时表显式删除，先truncate table，然后drop table，这样可以避免系统表的较长时间锁定。
<p style="text-indent: 2em">
<p style="text-indent: 2em">vi.慎用大的临时表与其他大表的连接查询和修改，减低系统表负担，因为这种操作会在一条语句中多次使用tempdb的系统表。
<p style="text-indent: 2em">
<p style="text-indent: 2em">d)合理的算法使用：
<p style="text-indent: 2em">
<p style="text-indent: 2em">根据上面已提到的SQL优化技术和ASE Tuning手册中的SQL优化内容,结合实际应用,采用多种算法进行比较,以获得消耗资源最少、效率最高的方法。
<p style="text-indent: 2em">具体可用ASE调优命令：set statistics io on, set statistics time on , set showplan on 等。 </p>
<img src ="http://www.blogjava.net/shanben/aggbug/226399.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-09-02 15:35 <a href="http://www.blogjava.net/shanben/archive/2008/09/02/226399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sybase數據庫性能調優的一些小方面</title><link>http://www.blogjava.net/shanben/archive/2008/09/02/226397.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Tue, 02 Sep 2008 07:29:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/09/02/226397.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/226397.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/09/02/226397.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/226397.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/226397.html</trackback:ping><description><![CDATA[1．1 性能指標<br />
&nbsp; &nbsp; &nbsp; &nbsp; 數據庫性能一般用兩個方面的指標來衡量：響應時間和吞吐量。響應越快，吞吐量越大，數據庫性能越好。響應時間和吞吐量有些情況下不能一起得到改善。<br />
<br />
1．2 調優級別<br />
對Sybase數據庫性能調優，可以從四個方面進行：<br />
一)&nbsp; &nbsp; &nbsp; &nbsp; 操作系統級：對網絡性能、操作系統參數、硬件性能等作改進。<br />
二)&nbsp; &nbsp; &nbsp; &nbsp; SQL Server級：調整存取方法，改善內存管理和鎖管理等。<br />
三)&nbsp; &nbsp; &nbsp; &nbsp; 數據庫設計級：採用降範式設計，合理設計索引，分佈存放數據等。<br />
四)&nbsp; &nbsp; &nbsp; &nbsp; 應用程序級：採用高效SQL語句，合理安排事務，應用游標，處理鎖。<br />
本文對第一、第三、第四方面的內容不做討論，第二方面提到的概念只適用於Sybase數據庫。<br />
<br />
1．3 調優工具<br />
&nbsp; &nbsp; &nbsp; &nbsp; 在分析Sybase數據庫的性能時，要用到一些數據庫系統本身提供的性能調優工具，包括幾個系統存儲過程：<br />
名稱&nbsp; &nbsp; &nbsp; &nbsp; 功能簡要介紹<br />
sp_sysmon&nbsp; &nbsp; &nbsp; &nbsp; 企業級系統性能報告工具<br />
sp_lock&nbsp; &nbsp; &nbsp; &nbsp; 查看鎖的情況<br />
sp_who&nbsp; &nbsp; &nbsp; &nbsp; 查看線程的活動情況<br />
sp_procqmode&nbsp; &nbsp; &nbsp; &nbsp; 存儲過程的查詢處理模式<br />
sp_configure&nbsp; &nbsp; &nbsp; &nbsp; 配置SQL Server系統級參數<br />
sp_estspace&nbsp; &nbsp; &nbsp; &nbsp; 估計創建一個表需要的空間和時間<br />
sp_spaceused&nbsp; &nbsp; &nbsp; &nbsp; 估計表的總行數及表和索引佔用的空間<br />
sp_monitor&nbsp; &nbsp; &nbsp; &nbsp; 監視CPU、I/O的統計活動情況<br />
<br />
在利用isql等一些工具時，還可以設置查詢會話中的幾個選項，來顯示SQL語句執行時的各種統計分析結果：<br />
指令&nbsp; &nbsp; &nbsp; &nbsp; On 的含義<br />
set noexec on/off&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;分析SQL語句後，還要執行<br />
set statistics io on/off&nbsp; &nbsp; &nbsp; &nbsp; 統計SQL執行所需I/O<br />
set statistics time on/off&nbsp; &nbsp; &nbsp; &nbsp; 統計SQL語句執行耗時<br />
set showplan on/off&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;顯示查詢計劃<br />
<br />
1．4 sp_sysmon 的使用<br />
企業級性能報告工具、系統存儲過程 sp_sysmon 的使用方法：<br />
在isql 下，首先輸入&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; sp_sysmon 'begin_sample'&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 啟動一個報告採樣過一段時間後，再輸入&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; sp_sysmon 'end_sample'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 結束上次報告採樣<br />
或者緊跟一參數&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sp_sysmon 'end_sample', "dcache" 結束上次報告採樣， 但只顯示數據緩衝（Data Cache Management）這一部分的情況。<br />
能替換dcache的可選參數如下表所示：<br />
參數&nbsp; &nbsp; &nbsp; &nbsp; 參數全稱，內容範圍解釋<br />
Dcache&nbsp; &nbsp; &nbsp; &nbsp; Data Cache Management，數據緩衝<br />
Kernel&nbsp; &nbsp; &nbsp; &nbsp; Kernel Utilization，有關引擎、網絡和I/O等情況<br />
Wpm&nbsp; &nbsp; &nbsp; &nbsp; Worker Process Management<br />
Parallel&nbsp; &nbsp; &nbsp; &nbsp; Parallel Query Management<br />
Taskmgmt&nbsp; &nbsp; &nbsp; &nbsp; Task Management<br />
Appmgmt&nbsp; &nbsp; &nbsp; &nbsp; Application Management<br />
Esp&nbsp; &nbsp; &nbsp; &nbsp; ESP Management<br />
Housekeeper&nbsp; &nbsp; &nbsp; &nbsp; Housekeeper Task Activity<br />
Monaccess&nbsp; &nbsp; &nbsp; &nbsp; Monitor Access to Executing SQL<br />
Xactsum&nbsp; &nbsp; &nbsp; &nbsp; Transaction Profile<br />
Xactmgmt&nbsp; &nbsp; &nbsp; &nbsp; Transaction Management<br />
Indexmgmt&nbsp; &nbsp; &nbsp; &nbsp; Index Management，索引管理<br />
Mdcache&nbsp; &nbsp; &nbsp; &nbsp; Metadata Cache Management<br />
Locks&nbsp; &nbsp; &nbsp; &nbsp; Lock Management，鎖管理<br />
Pcache&nbsp; &nbsp; &nbsp; &nbsp; Procedure Cache Management<br />
Memory&nbsp; &nbsp; &nbsp; &nbsp; Memory Management<br />
Recovery&nbsp; &nbsp; &nbsp; &nbsp; Recovery Management<br />
Diskio&nbsp; &nbsp; &nbsp; &nbsp; Disk I/O Management，磁盤I/O管理<br />
Netio&nbsp; &nbsp; &nbsp; &nbsp; Network I/O Management<br />
<br />
1．5 <br />
用sp_sysmon可以得到數據庫系統的性能基準報告，但要在比較穩定的狀態下產生，方可作為參考和對照的依據。<br />
<br />
1．6 理解存儲方法<br />
只有清楚數據庫存儲數據的底層細節，如數據頁、索引頁的物理結構，每一行的大小計算，不同類型列佔用的寬度等等問題，才能對各種調優措施有個深入領會。關於這個問題，比較複雜和細緻，請自行參閱有關書籍。<br />
一般地，對於更改數據的操作，要盡量促進數據庫進行直接更新（ Direct Updates ），所以要遵守以下幾條原則：<br />
1）除非必要，避免使用允許null值的列和可變長度的列。<br />
2）如果varchar 和 varbinary 列填充得比較滿，毫不猶豫轉成 char 和 binary 列。對於建表時指定的頁填充率（page fillfactor）參數，要權衡確定數值大小。一般：小值，適合於有許多隨機插入的表，該表的數據經常被刪除，又經常被增加；大值，適合於大多數的數據被增加到表末尾，如客票系統的售票存根和退票存根表。<br />
<br />
2 SQL Server級的調優<br />
2．1 管理共享內存<br />
&nbsp; &nbsp; &nbsp; &nbsp; 數據庫性能優化的首要方面是最優管理內存。數據庫佔用的共享內存分成數據緩衝（data cache）、存儲過程緩衝（Procedure cache）等幾塊。在isql 下使用 sp_configure 'cache' 可以看到存儲過程緩衝所佔百分比（procedure cache percent），整個數據緩衝大小（total data cache size） 等參數。<br />
<br />
2．1．1 存儲過程緩衝（Procedure cache）<br />
存儲過程緩衝保持以下對象的查詢計劃：<br />
Procedures&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;：存儲過程<br />
Triggers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;：觸發器<br />
Views&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ：視圖<br />
Rules&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ：規則<br />
Defaults&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;：缺省<br />
Cursors&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;：游標<br />
存儲過程不可重入，意即每個並發用戶調用都會在內存中產生一個拷貝。<br />
Procedure, triggers, and views 當它們被裝載到procedure cache中時，被查詢優化器優化，建立查詢計劃。如果存儲過程在緩衝中，被調用時就不需要重新編譯。如果procedure cache太小，存儲過程就會經常被其他調入內存的存儲過程沖洗掉，當再次被調用時，存儲過程又被調入內存，再重新編譯，用戶請求因此不得不等待。最嚴重的情況，如果procedure cache不夠，存儲過程甚至都不能運行。所以在內存足夠的情況下，procedure cache percent 參數盡可能大一些。<br />
<br />
2．1．2 數據緩衝（Data Cache）<br />
&nbsp; &nbsp; &nbsp; &nbsp; 數據緩衝用來緩存數據頁和索引頁，是除去存儲過程緩衝，系統其他佔用的緩衝外的剩餘內存空間。通過給服務器增加物理內存擴大數據緩衝，是最有效的方法。當然，如果不能加內存，就只能通過減少存儲過程緩衝的比例等方法來擴大數據緩衝了。通過 sp_configure "extent I/O buffers", 20（可調） 命令，在Data Cache中保留一些頁專用於創建索引時使用，可以顯著提高創建索引的性能。但要注意每開闢一個緩衝佔用16K 字節的系統內存。<br />
<br />
2．1．3 命名緩衝<br />
&nbsp; &nbsp; &nbsp; &nbsp; 通過如下的命令：<br />
1&gt;; sp_helpcache<br />
2&gt;; go<br />
查看某客票數據庫中命名緩衝，得到的結果如下：<br />
Cache Name&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Config Size&nbsp; &nbsp;&nbsp;&nbsp;Run Size&nbsp; &nbsp;&nbsp; &nbsp; Overhead<br />
<br />
------------------------ -------------&nbsp; &nbsp;----------&nbsp; &nbsp;&nbsp;&nbsp;----------<br />
DS30_Tran_Log&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;20.00 Mb&nbsp; &nbsp;&nbsp; &nbsp; 20.00 Mb&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;2.05 Mb<br />
Systemtable&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;20.00 Mb&nbsp; &nbsp;&nbsp; &nbsp; 20.00 Mb&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;2.05 Mb<br />
default data cache&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 0.00 Mb&nbsp; &nbsp;&nbsp;&nbsp;4462.86 Mb&nbsp; &nbsp;&nbsp; &nbsp;464.97 Mb<br />
left_base_center&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;16.00 Mb&nbsp; &nbsp;&nbsp; &nbsp; 16.00 Mb&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;1.57 Mb<br />
price_cache&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;8.00 Mb&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;8.00 Mb&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0.85 Mb<br />
可以看出有4個命名緩衝，分別綁定客票系統的應用日誌表、一些重要且常用的系統表、余票表、票價系列表，另外1個是缺省數據緩衝。這種配置還不是最合理，應該進一步把Systemtable這個命名緩衝細分成很多個，每一個單獨存放一張系統表。<br />
<br />
2．1．4 緩衝策略<br />
緩衝策略是指把數據提前讀入內存的機制，分預取策略(Prefetch rategy，即大I/O策略)和取後馬上丟棄策略(Fetch-and-Discard)、提示策(Hints)等幾種。可以在三個級別上設置表數據的預取策略(Prefetch Strategy，即大I/O策略)於：對像級，會話級，查詢級。如果三個級別上都有設置，它們發生作用的優先順序是：對像級 &gt;; 會話級 &gt;; 查詢級。對於如何在查詢級利用指定的緩衝池，可以查看下面例子（使用4K緩衝池）：<br />
select au_fname, au_lname<br />
&nbsp;&nbsp;from authers (prefetch 4)<br />
where au_id in ( A372020631, ..., A1887081515 )<br />
go<br />
DSS應用往往得益於大的I/O，應該放開large I/O strategy預取策略。<br />
如果一個應用傾向於OLTP特徵，用戶能在會話級關掉Prefetch來提高性能。對於OLTP應用，關閉large I/O strategy預取策略。對於所取到的頁不會有重用的情況，放開fetch-and-discard策略。客票系統對存根數據進行統計的應用，如財收日結賬，營銷分析數據整理模塊和綜合查詢等，都可以利用這一結論。查看幾個操作頻繁且較大的表上的緩衝策略，用如下命令：<br />
sp_cachestrategy center,seat_area<br />
sp_cachestrategy center,sale_record0505<br />
<br />
2．2 管理鎖<br />
2．2．1 頁鎖升級閥限<br />
優化鎖的重要考慮是設置頁級鎖升級升級成表級鎖的閥限。要盡量避免頁鎖很快升級成表級鎖。在某客票數據庫中，用sp_configure 『lock』可以看到如下結果：<br />
deadlock checking period&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;500&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;1000&nbsp; &nbsp;&nbsp; &nbsp;1000<br />
number of locks&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;5000&nbsp; &nbsp;&nbsp; &nbsp; 46875&nbsp; &nbsp;&nbsp; &nbsp;200000&nbsp;&nbsp;200000<br />
page lock promotion HWM&nbsp; &nbsp;&nbsp; &nbsp;200&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0&nbsp; &nbsp;&nbsp; &nbsp; 10000&nbsp; &nbsp;&nbsp; &nbsp; 10000<br />
page lock promotion LWM&nbsp; &nbsp;&nbsp; &nbsp; 200&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;200&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;200<br />
page lock promotion PCT&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;100&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 90&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 90<br />
可以看到頁鎖升級的閥限有三個：HWM（最高點） 為10000，LWM（最低點）為200，PCT為90。Sybase數據庫內部根據PCT值按公式PCT*TAB_SZ/100得出計算閥限，如果計算閥限 &lt; LWM, 鎖升級發生在LWM值；如果計算閥限 &lt; HWM，鎖升級發生在HWM值。如果 LWM &lt; 計算閥限 &lt; HWM ，鎖升級發生在PCT*TAB_SZ/100值。<br />
鎖升級閥限設置分對像級和服務器級兩種。<br />
針對對像級設置（數據庫上的表或表上的索引），配置命令是：<br />
sp_setpglockpromote {"database" | "table"}, objname, new_lwm,new_hwm, new_pct<br />
針對服務器級設置，配置命令是：<br />
sp_setpglockpromote server, NULL, new_lwm, new_hwm, new_pct。<br />
如果要刪除掉對像級上的頁鎖升級閥限，用：<br />
sp_dropglockpromote {"database" | "table"}, objname<br />
<br />
2．2．2 減少鎖爭奪的方法：<br />
1）降範式設計數據庫，創建冗余表。<br />
2）把堆表（沒有聚族索引的表）分區。<br />
3）對於小表，使用fillfactor和max_rows_per_page來減少行密度，從而使各行數據分佈到許多頁（此方法適用於SQL Server 11版，對於11.9.2版以後的Sybase數據，有了行級鎖，此方法必要性不大）。<br />
<br />
2．3 管理臨時庫(tempdb)<br />
管理臨時庫一個重要原則是要避免臨時表跨多個設備，可以把tempdb從master設備中分離出來，放到一個單獨的設備上去。這樣可以減少存取系統表時對I/O資源的爭奪。用sp_dropsegment 存儲過程從master設備中移除tempdb的default段和system段。為了進一步提高tempdb的I/O速度，可以考慮把tempdb整個放在RAM 驅動器或固態存儲設備上，存取速度是一般磁盤的1000倍。一般情況下，tempdb會非常頻繁地爭奪和佔用缺省數據緩衝，因為查詢會話中有許多臨時表要創建、計算和刪除。所以推薦把tempdb綁定到它自己的命名緩衝，這樣可以防止臨時對像在內存中的活動沖洗掉缺省數據緩衝中的其他對象，利於在多個緩衝間展開I/O。在使用臨時表的時候，還有一個原則：盡量縮小表規模和行的寬度，每一行只包括必要的列。例如在用select * into生成臨時表時，如果只需要幾個列的數值，就不要用這樣的語句，而直接選取需要的列。<br />
<br />
2．4 使用多引擎（Multiple Network Engines）<br />
如果操作系統使用了多個CPU，那麼用sp_configure 配置數據庫的參數：在線引擎數(max online engines)，可以擴展系統的網絡I/O容量，分佈網絡I/O到各個引擎，從而提高性能，允許更多的用戶連接。<br />
在用戶登錄數據庫時，總是先登錄到引擎0，由引擎0在可用引擎隊列中選擇一個掛最少連接的引擎來傳遞socket描述符，從而重定向連接到那個引擎，由該引擎去處理跟此用戶連接相關的所有網絡活動。<br />
對於多引擎SMP結構，SQL Server引入了自旋鎖(spinlock)的一種數據結構，在多個引擎間共享。對於不同類型的任務，在哈希表上分配不同的自旋鎖，有頁鎖自旋鎖、表鎖自旋鎖和地址自旋鎖。<br />
自旋鎖的配置：<br />
sp_configure "page lock spinlock ratio", newval<br />
sp_configure "table lock spinlock ratio", newval<br />
sp_configure "address lock spinlock ratio", newval<br />
增大數值，可以減少碰撞，提高並發操作度。但是每一個自旋鎖結構要佔用256字節的內存。<br />
如果數據庫發生1279錯，可能原因：<br />
1）不允許足夠的鎖，解決辦法是用sp_configure 調大 number of locks 數值<br />
2）在engine freelock 緩衝中沒有足夠的鎖，解決辦法是用sp_configure調大 max engine freelocks 數值。<br />
如果數據庫系統使用了4個引擎，那麼每個引擎的自由鎖緩衝中包含：each engine-specific freelock cache 包含 5000 * .20 /4 = 250 個鎖。<br />
在平時，經常用sp_monitor和sp_sysmon監視CPU使用率，如果所有CPU的利用率高於85%，增加CPU，然後增大數據庫的引擎數，可以改善性能。<br />
<br />
2．5 設備使用的優化<br />
把最常插入的表分區，放在多個設備上，這樣可以創建多個頁鏈，從而改善多個並發插入時的性能，因為每一個插入都要找到頁鏈，頁鏈有多個，就允許多個插入同時進行。這一點，尤其適用於客票系統的存根表和訂票存根表（CG30_RRT），所帶來的性能改善會非常明顯。<br />
物理I/O的代價遠大於邏輯I/O，所以要盡量減少磁盤進行物理I/O的次數，盡量多進行內存中的邏輯I/O。使用statistics io工具和sp_sysmon，來觀察磁盤I/O。可以配置使用大的I/O來減少物理I/O的次數，方法有三個：<br />
1）用更多的磁盤；<br />
2）表和索引分開到不同的磁盤；<br />
3）增加一次I/O系統參數值的大小。<br />
SQL Server總是為I/O請求建立一個磁盤檢查的調度環，用sp_configure "I/O polling process count"來提高數值，加長環，可以降低引擎的檢查次數，提高吞吐量。但較小的值一般有助於減少響應時間。<br />
對於可用的磁盤I/O控制塊，要查看操作系統文檔，用sp_configure "disk i/o structures"配置，這個數值要盡可能高。<br />
分離日誌和數據，到不同的設備；給tempdb自己的設備；分離表和索引到不同的設備。這些方法都可以減少I/O。<br />
<br />
2．6 對事務處理的調優 <br />
2．6．1 事務類型<br />
事務處理無外乎三種：1,OLTP; 2, DSS; 3, OLTP + DSS 的混合負載<br />
OLTP（聯機事務處理）的特點：<br />
&nbsp; &nbsp; &nbsp; &nbsp; 數據插入、修改和刪除頻繁。<br />
&nbsp; &nbsp; &nbsp; &nbsp; 經常操作的是單個記錄。<br />
&nbsp; &nbsp; &nbsp; &nbsp; 當不適當設計時，傾向於碰撞和衝突。<br />
DSS（決策支持系統）的特點：<br />
&nbsp; &nbsp; &nbsp; &nbsp; 數據修改不太頻繁。<br />
&nbsp; &nbsp; &nbsp; &nbsp; 如果有插入和刪除，是大批量的。<br />
&nbsp; &nbsp; &nbsp; &nbsp; 平時一般是只讀操作。<br />
&nbsp; &nbsp; &nbsp; &nbsp; 表連接很常見。<br />
&nbsp; &nbsp; &nbsp; &nbsp; 有比較特別的查詢。<br />
OLTP + DSS 混合負載的特點權衡：<br />
&nbsp; &nbsp; &nbsp; &nbsp; 在性能方面要比較，是要吞吐量還是響應時間。<br />
&nbsp; &nbsp; &nbsp; &nbsp; 在鎖方面要比較，是要並發性強呢還是要數據一致性強。<br />
<br />
2．6．2 事務管理原則<br />
一般的事務管理原則有：<br />
1)&nbsp; &nbsp; &nbsp; &nbsp; 分解大的事務成多個小的事務。如客票數據的備份操作中，要刪除過期數據，如果設計小事務做循環，便不會影響應用，完全可以做到任何時候備份和刪除，不一定非得等服務器閒的時候做。<br />
2)&nbsp; &nbsp; &nbsp; &nbsp; 避免在單個事務中更新或刪除大量的數據行。比如客票系統的席位庫數據清理，即使在服務器閒的時候做這種操作，也會鎖定整個表，影響售票。<br />
3)&nbsp; &nbsp; &nbsp; &nbsp; 盡量用可以接受的最低孤立級(isolation level)，來提高並發度。如在余票查詢等功能的應用中，使用這種孤立級，便可以最大程度地降低對售票的影響。<br />
4)&nbsp; &nbsp; &nbsp; &nbsp; 提高事務吞吐量的措施包括：避免延遲更新；盡可能使用存儲過程等等。<br />
<br />
2．6．3 跟事務特徵相關的數據庫可調參數或特性<br />
相對於OLTP應用，SQL Server有一些特性來滿足要求。<br />
1)&nbsp; &nbsp; &nbsp; &nbsp; 命名緩衝（Named cache）<br />
對於命名緩衝，可以配置多個不同大小的內存池，來滿足不同的應用需求。對於多個引擎的情況，命名緩衝還有一項重要的功能是降低自旋鎖的內部爭奪。<br />
2)&nbsp; &nbsp; &nbsp; &nbsp; 日誌I/O緩衝大小可配置<br />
sp_logiosize ["default" | "size" | "all" ]<br />
缺省值是4K，但如果4K內存池沒有配置，SQL Server會使用2K大小的內存池<br />
3)&nbsp; &nbsp; &nbsp; &nbsp; 堆表可分區，分佈插入操作到各個設備<br />
適用於頻繁插入的表和有並發BCP倒入數據的表，如客票系統的售票存根和退票存根表。<br />
4)&nbsp; &nbsp; &nbsp; &nbsp; 鎖升級閥限可配置。<br />
<br />
相對於DSS應用，SQL Server也有一些特性來滿足要求<br />
1)&nbsp; &nbsp; &nbsp; &nbsp; 應用大的 I/O 緩衝池<br />
用sp_poolconfig來配置。<br />
2)&nbsp; &nbsp; &nbsp; &nbsp; 綁定熱表到命名緩衝<br />
如 sysindexes, syslogs, 注意如果把 syslogs 放到單獨的緩衝中，可以減少在缺省或其他命名緩衝上的自旋鎖爭奪。對於客票系統的train_dir, stop_time, 票價表, 取票存儲過程相關的表都可以放在單獨的命名緩衝上。<br />
3)&nbsp; &nbsp; &nbsp; &nbsp; 取後丟棄緩衝策略 (Fetch-and-discard cache strategy)<br />
不會沖洗掉緩衝中的常用對象，可以減少MRU鏈的爭奪，較少對OLTP事務的干擾。<br />
對於收入統計應用，統計過往存根表中的數據，可以應用這一策略。<br />
<br />
2．7 網絡方面的調優<br />
Sybase客戶和服務器之建傳遞的是TDS包，缺省大小是512字節。對於要傳輸大批量數據的應用，如BCP、 文本/圖像的取用、大結果集SQL語句，要用下面的配置命令配置大的TDS包大小。<br />
sp_configure "default network packet size", nnn<br />
sp_configure "maximum network packet size", nnn<br />
&nbsp; &nbsp; &nbsp; &nbsp; 對於isql 和bcp，可以在應用級指定TDS包的大小：isql -Usa -P &#8211;Annn，bcp -Usa -P &#8211;Annn。<br />
注意在調大maximum network packet size的參數後，要增大 additional network memory 參數，來適應 maximum network packet size 的要求。<br />
<img src ="http://www.blogjava.net/shanben/aggbug/226397.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-09-02 15:29 <a href="http://www.blogjava.net/shanben/archive/2008/09/02/226397.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sp_sysmon性能诊断结果分析</title><link>http://www.blogjava.net/shanben/archive/2008/07/20/216156.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Sun, 20 Jul 2008 02:56:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/20/216156.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/216156.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/20/216156.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/216156.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/216156.html</trackback:ping><description><![CDATA[<div class="storytext">
<p><font size="2">文章描述了通过sp_sysmon对Adaptive Server系统运行情况有一个全面系统了解，有利于更好地熟悉系统性能，更为有效地进行系统管理，合理地利用和配置系统资源，达到系统性能调优的目的。 <br />
从18个方面了解在用系统性能状况，并在适当的时候利用环境参数进行性能调优： </font></p>
<p>1、内核管理（kernal）<br />
2、应用管理（appmgmt）<br />
3、数据缓存管理（dcache） <br />
4、ESP管理（esp）<br />
5、索引管理（indexmgmt） <br />
6、锁管理（locks） <br />
7、内存管理（memory）<br />
8、元数据高速缓存管理（mdcache）<br />
9、任务管理（taskmgmt） <br />
10、监视器访问SQL的执行（monaccess） <br />
11、网络I/O管理（netio） <br />
12、并行查询管理（parallel）<br />
13、过程缓存管理（pcache）<br />
14、恢复管理（recovery） <br />
15、事务管理（xactmgmt）<br />
16、事务概要（xactsum） <br />
17、磁盘I/O管理（diskio） <br />
18、工作进程管理（wpm） </p>
<p>括号后英文短词是该模块参数。 <br />
步骤：执行sp_sysmon &#8220;00:10:00&#8221;（server级系统存贮过程，不需要打开某个数据库），或者执行如下格式的过程，查看具体操作批命令对应系统性能情况：（10分钟系统查看） <br />
sp_sysmon begin_sample <br />
SQL语句或者存贮过程 <br />
sp_sysmon commit_sample <br />
本实验采用 sp_sysmon &#8220;hh:mm:ss&#8221;,性能模块名。 <br />
可了解当前系统在各方面的系统运行状况，性能出现什么问题和不平衡不协调之处，学会使用相应的参数和措施进行解决和调优，不断比较对照调整前后的性能状况，最终改善系统性能。 <br />
说明：1、该命令执行结果集的开头相同如下：</p>
<p><br />
<p>====================================================================== <br />
Sybase Adaptive Server Enterprise System Performance Report <br />
====================================================================== <br />
Server Version: Adaptive Server Enterprise/11.9.2/1031/P/NT (IX86)/OS 3. <br />
Server Name: Server is Unnamed <br />
Run Date: May 28, 2001 <br />
Statistics Cleared at: 15:57:27 <br />
Statistics Sampled at: 16:07:28 <br />
Sample Interval: 00:10:00 </p>
<p><br />
2、执行结果集的每列信息提示： </p>
<p>per sec ： 采样期间每秒的平均值 <br />
per xact： 采样期间每提交一个事务的平均值 <br />
count ： 采样期间每秒的总计值 <br />
% of total： 占总数的百分比，根据不同情况各有不同 </p>
<p>3、结果集对应给出性能情况描述、分析以及可调性说明 <br />
4、只给出部分模块的监视结果（可能有删节），用sp_sysmon &#8220;hh:mm:ss&#8221;可看全部详细情况。 <br />
单元一：监视内核利用情况 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,kernal <br />
结果： </p>
<p>Kernel Utilization （内核利用）</p>
<p>------------------ <br />
Engine Busy Utilization <br />
Engine 0 1.8 % <br />
引擎繁忙程度应在80%-90%之间，如果长期在90%以上，应考虑增加引擎数来改善性能。因为此时内部管理进程无法向磁盘写入，则检查点需要将许多页写回磁盘，而检查点进程很可能将CPU的利用率提高到100%，导致响应时间明显增加。 <br />
CPU Yields by Engine per sec per xact count % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Engine 0 6.6 0.6 3949 100.0 % <br />
引擎放弃CPU次数：% of total=1个引擎放弃次数/所有引擎放弃次数，如果显示引擎利用率较低，可通过放弃数判断是否真实反映引擎的停止情况。增加&#8220;runnable process search count&#8221;（引擎放弃CPU给OS之前一个引擎循环查找可执行任务的次数）参数可增加CPU的驻留时间，而如果想减少引擎在空闲时检查I/O的时间，可减少该参数的值。 <br />
Network Checks <br />
Total Network I/O Checks 0.0 0.0 0 n/a <br />
引擎发送或接收网络包的次数。引擎空闲时频繁检查网络包，如果该值很低而&#8220;CPU Yields by Engine&#8221;的值高，表明引擎可能被频繁放弃。 <br />
可能包括阻塞和非阻塞两种检查方式。非阻塞方式不管有无I/O等待都对网络进行I/O检查。如果引擎已被放弃并正执行阻塞网络检查，则在网络包到达以后仍保持一段睡眠时间（潜伏期）。此时增加&#8220;runnable process search count&#8221;（缺省2000）参数可减少潜伏期，保持引擎有较长的循环检查时间，而不是过早被放弃。 <br />
Disk I/O Checks磁盘I/O检查情况： <br />
Total Disk I/O Checks 693.2 58.8 415939 n/a <br />
Checks Returning I/O 469.9 39.9 281921 67.8 % <br />
引擎对I/O情况的有效检查（I/O完成次数），如过高或过低，用&#8220;i/o polling process count&#8221;（Server的调度程序在检查磁盘I/O或网络I/O之前可执行的最大进程数）参数增加或减少检查频率。通常说增加该值可增加有大量磁盘或网络I/O的应用的吞吐量，反之，减少该值有可改善其响应时间。 <br />
Avg Disk I/Os Returned n/a n/a 0.03020 n/a </p>
<p><br />
增加引擎在检查期间的等待时间可改善吞吐量，因为减少引擎检查I/O时间相应增加执行进程的时间。 </p>
<p>单元二：监视并行查询管理 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,parallel <br />
结果： 报告并行查询次数、执行期间调整了多少工作进程，以及在merge和sort操作时加锁情况。 </p>
<p>Parallel Query Management <br />
------------------------- <br />
Parallel Query Usage per sec per xact count % of total <br />
------------------------- --------- --------- ------- ---------- <br />
Total Parallel Queries 0.1 8.0 16 n/a <br />
优化器自动确定是否并行操作，以及为此使用多少工作进程。 <br />
WP Adjustments Made <br />
Due to WP Limit 0.0 0.0 0 0.0 % <br />
会话级的限制受&#8220;set parallel_degree&#8221; or &#8220;set scan_parallel_degree&#8221;参数控制。 <br />
Due to No WPs 0.0 0.0 0 0.0 % <br />
缺乏可用的工作进程导致申请工作进程数减少。可适当增加&#8220;number of worker processes&#8221; <br />
Merge Lock Requests per sec per xact count % of total <br />
报告并行merge操作的锁请求数，很快授予锁的数目，下面3种类型锁的等待情况： <br />
------------------------- --------- --------- ------- ---------- <br />
Network Buffer Merge Locks <br />
Granted with no wait 4.9 438.5 877 56.2 % <br />
Granted after wait 3.7 334.5 669 42.9 % <br />
Result Buffer Merge Locks <br />
Granted with no wait 0.0 0.0 0 0.0 % <br />
Granted after wait 0.0 0.0 0 0.0 % <br />
Work Table Merge Locks <br />
Granted with no wait 0.1 7.0 14 0.9 % <br />
Granted after wait 0.0 0.0 0 0.0 % <br />
------------------------- --------- --------- ------- <br />
Total # of Requests 8.7 780.0 1560 <br />
Sort Buffer Waits per sec per xact count % of total <br />
------------------------- --------- --------- ------- ---------- <br />
Total # of Waits 0.00.0 0 n/a <br />
并行排序所用&#8220;排序缓冲区等待&#8221;锁。如果等待数较高，可考虑加大&#8220;number of sort buffers&#8221;的值。 <br />
====================================================================== </p>
<p>单元三：监视执行SQL的访问情况 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,monaccess <br />
结果： </p>
<p>Monitor Access to Executing SQL（监视执行SQL的访问情况）</p>
<p>------------------------------- <br />
per sec per xact count % of total <br />
------------ ------------ ---------- ---------- <br />
Waits on Execution Plans 0.0 0.00 n/a <br />
每个试图使用sp_showplan但必须等待获得访问查询计划的读资格，报告等待次数。 <br />
Number of SQL Text Overflows 0.0 0.0 0 n/a <br />
SQL批文本超过文本缓冲区大小的溢出次数。 <br />
Maximum SQL Text Requested n/a n/a 0 n/a <br />
(since beginning of sample) <br />
&#8220;max SQL text monitored&#8221;（缺省为0）参数指定分配给每个连接用户的内存量，用以保存SQL文本到内存，供sever监视器共享。推荐值为1024。 <br />
====================================================================== <br />
单元四：事务概要 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,xactsum <br />
结果： </p>
<p>Transaction Profile（事务概要）</p>
<p>报告提交的事务数，要尽量减少多数据库事务的提交（一个事务对多数据库的访问） <br />
Transaction Summary per sec per xact count % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Committed Xacts 11.8 n/a 7073 n/a <br />
Transaction Detailper sec per xactcount% of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Inserts <br />
APL Heap Table 13.6 1.2 8189 100.0 % <br />
如果大量堆表数据插入，结合查看锁的堆表最后一页锁情况，是否引起严重的锁争夺，随之调整相应的数据表，避免因为锁资源争夺引起性能降低。 <br />
APL Clustered Table 0.0 0.0 0 0.0 % <br />
对全页锁的表插入数据行，注意可能引起的页分裂。 <br />
Data Only Lock Table 0.0 0.0 0 0.0 % <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Total Rows Inserted 13.6 1.2 8189 100.0 % </p>
<p>
<p>单元五：事务管理 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,xactmgmt <br />
结果： </p>
<p>Transaction Management（事务管理）</p>
<p>---------------------- <br />
　　用户日志cache（每个用户对应一个）降低了写入事务日志的次数，如果是多处理器系统还减少了事务日志当前页的争夺，因而提高了性能。可配置环境参数&#8220;user log cache size&#8221;（缺省最低2048字节），太小导致用户日志常满并频繁写入事务日志，太大则每个连接用户都扩大，又造成内存浪费。原则是配置不超过事务完成写入事务日志的长度。 <br />
ULC Flushes to Xact Log per sec per xact count % of total <br />
各种类型导致写入事务日志的次数 <br />
------------------------- ------------ ------------ ---------- ---------- <br />
by Full ULC 0.0 0.0 0 0.0 % <br />
如果% of total的值超过20%，考虑增加环境参数&#8220;user log cache size&#8221;的值。 <br />
by End Transaction 11.8 1.0 7095 95.5 % <br />
以显式或隐式的rollback或commit标志事务结束。值大表示有很多短小事务。 <br />
by Change of Database 0.0 0.0 12 0.2 % <br />
如果值大，考虑减低ULC中大于2K的缓冲池，降低或去除大块I/O池。 <br />
by System Log Record 0.5 0.0 321 4.3 % <br />
其% of total值大于20%并且ULC长度大于2048，考虑降低ULC的长度。 <br />
by Other 0.0 0.0 0 0.0 % <br />
------------------------- ------------ ------------ ---------- </p>
<p><br />
Total ULC Flushes 12.4 1.1 7428 </p>
<p>单元六：索引管理 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,indexmgmt <br />
结果： </p>
<p>Index Management（索引管理）<br />
索引可以加速数据检索，但同时又降低了更新的性能。 </p>
<p>---------------- <br />
Nonclustered Maintenance per sec per xact count % of total <br />
非聚簇索引维护情况：报告因为插入、删除、修改、页分裂等造成的索引维护次数。 <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Ins/Upd Requiring Maint 0.0 0.0 0 n/a <br />
影响索引的插入和修改的操作数，需要维护非聚簇索引。对于插入，有多少非聚簇索引，就需要增加多少索引维护的开销；对于修改，则只对相关的索引进行维护。 <br />
# of NC Ndx Maint 0.0 0.0 0 n/a <br />
因为插入和修改需要对多少非聚簇索引进行维护。 <br />
Deletes Requiring Maint 0.0 0.0 0 n/a <br />
# of NC Ndx Maint 0.0 0.0 0 n/a <br />
影响索引的删除操作次数，以及需要维护的非聚簇索引数。 <br />
RID Upd from Clust Split 0.0 0.0 0 n/a <br />
在APL（全页锁）的聚簇索引表发生页分裂次数，相应需要进行索引维护。 <br />
# of NC Ndx Maint 0.0 0.0 0 n/a <br />
页分裂后对应的索引维护次数。 <br />
Upd/Del DOL Req Maint0.0 0.0 0 n/a <br />
DOL表发生影响索引的修改删除操作次数。 <br />
# of DOL Ndx Maint 0.0 0.0 0 n/a <br />
对应索引维护次数。 <br />
Page Splits 0.0 0.0 0 n/a <br />
包括数据页、聚簇索引页和非聚簇索引页因为插入新行没有足够空间单元导致页分裂。页分裂造成修改索引页、修改页指针、增加锁资源争夺等从而降低性能。 <br />
如果页分裂度高（次数多），而又是对全页加锁表进行插入操作，并且表有组合键的聚簇索引，这时可通过改变那些索引的页分裂点来减少页分裂，即是说组合键的第一个键表中在用，第二个键列值按升序排列；也可考虑用fillfactor的合适配置来降低在聚簇索引的APL表的数据页以及非聚簇索引的叶子数据页上的页分裂。 <br />
建议对表插入行按照升序插入方式，这样发生页分裂点也是在插入行点而不是在页中间，这样能够提高性能。通过dbcc tune (ascinserts, 1, "表名")设置插入方式，0反之。 <br />
如果索引维护量大，会因为维护需要额外的进程、额外的I/O、额外的索引页锁从而影响性能。可以通过对比不同操作次数与导致的维护次数，如果维护次数很多，还发生页分裂、retries等现象，严重时可考虑不用索引。 <br />
单元七：锁管理 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,locks <br />
结果： </p>
<p>Lock Management（锁管理）<br />
报告锁、死锁、锁提升和锁争夺的情况 </p>
<p>--------------- <br />
Lock Summary（锁概述） per sec per xact count % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Total Lock Requests 26.1 2.2 15676 n/a <br />
总共的锁请求 <br />
Avg Lock Contention 0.0 0.0 0 0.0 % <br />
平均锁争夺 <br />
Deadlock Percentage 0.0 0.0 0 0.0 % <br />
死锁出现的比例 <br />
Lock Hashtable Lookups 26.1 2.2 15677 n/a <br />
对hash表的表、页、行锁的查询次数。 <br />
Avg Hash Chain Length n/a n/a 0.00038 n/a <br />
Hash链平均长度：采样期间每个hash桶的平均加锁数目。如果每个hash链超过4个锁，可用参数&#8220;lock hashtable size&#8221;调整扩大加锁hash表的大小，尤其是大型bcp可配置更大。 <br />
Lock Detail per sec per xactcount % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
对于各种类型的锁细节，重点查看其立即授予和等待情况。 <br />
Last Page Locks on Heaps 堆表最后页锁 <br />
Granted 13.6 1.2 8189 100.0 % <br />
Waited 0.0 0.0 0 0.0 % <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Total Last Pg Locks 13.6 1.2 8189 100.0 % <br />
如果堆表最后一页锁的争夺激烈（尤其是热对象的等待时间过长），考虑建立聚簇索引，或者表分区来解决锁资源争夺问题。 <br />
Deadlocks by Lock Type per sec per xact count % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Total Deadlocks 0.0 0.00 n/a <br />
死锁出现次数。当很多事务同时访问同一个数据库时，会加剧锁资源争夺，严重时事务之间会发生死锁。可用sp_object_stats查明死锁位置。该过程报告资源争夺最激烈的10张表、一个数据库中资源争夺的表和单个表的争夺情况。语法为sp_object_stats interval [, top_n <br />
[, dbname [, objname [, rpt_option ]]]]，查看锁争夺情况只需设置interval为&#8220;hh:mm:ss&#8221;。如果显示每种锁的争夺程度超过15%，应该改变加锁方式，比如表的全页锁改成数据页锁，数据页锁改成数据行锁等。 <br />
Deadlock Detection 死锁检测 <br />
Deadlock Searches 0.0 0.0 0 n/a <br />
死锁检测次数。死锁检测将特花费时间，如果检测次数过多，用参数&#8220;deadlock checking period&#8221;（缺省500ms）调节死锁检测周期。 <br />
Lock Promotions 锁提升 <br />
Total Lock Promotions 0.0 0.0 0 n/a <br />
锁提升指排它页锁到排它表锁、共享页锁到共享表锁、排它行锁到排它表锁、共享行锁到共享表锁、共享next_key锁到共享表锁。查看锁提升是否加剧了锁争夺或死锁发生，如果锁争夺激烈并且锁提升频繁，考虑调整锁的隔离级别，对全页锁表，需要2级也可强制为3级。 <br />
Lock Timeouts by Lock Type per sec per xact count % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Total Timeouts 0.0 0.0 0 n/a </p>
<p>
<p>单元八：数据cache管理 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,dcache <br />
结果： </p>
<p>Data Cache Management（数据cache管理）</p>
<p>--------------------- <br />
　　报告数据cache的自旋锁争夺、cache应用、cache击中错失、配置缓冲池的翻转、清洗缓存（包括脏页）、预取的请求与拒绝、读脏页请求等情况。 <br />
Cache Statistics Summary (All Caches) <br />
------------------------------------- <br />
per sec per xactcount % of total <br />
------------ ------------ ---------- ---------- <br />
Cache Search Summary cache的击中和错失次数 <br />
Total Cache Hits 18.6 1.6 11171 89.9 % <br />
Total Cache Misses2.1 0.2 1251 10.1 % <br />
------------------------- ------------ ------------ ---------- <br />
Total Cache Searches 20.7 1.8 12422 <br />
Cache Turnover <br />
Buffers Grabbed 0.2 0.0 102 n/a <br />
缓存掠夺。Count表示cache缓存块链中从LRU末端取走的缓存块次数。 <br />
Buffers Grabbed Dirty 0.0 0.0 0 0.0 % <br />
脏页掠夺。在从LRU末端取走脏页时必须等待将脏页写回磁盘。如果其值非零，可找出是什么cache受到影响，这事关cache的击中性能问题。 <br />
Cache Strategy Summary cache策略（对所有的cache） <br />
Cached (LRU) Buffers 19.8 1.7 11880 100.0 % <br />
报告有多少cache中的缓存块放置到MRU/LRU链的头部。 <br />
Discarded (MRU) Buffers 0.0 0.0 0 0.0 % <br />
报告多少缓存块采用了获取-丢弃策略，缓存块用过以后被放到缓存块链的刷洗标记处。 <br />
Large I/O Usage <br />
0.0 0.0 0 n/a <br />
大块I/O请求使用次数，这里没有设置大块I/O，故均为0值，也没有其授权或拒绝使用情况。 <br />
Large I/O Effectiveness <br />
大块I/O的使用效果，百分比值低表示很少的页被带入cache供查询使用，可进一步查看单个cache的使用情况。 <br />
Pages by Lrg I/O Cached 0.0 0.0 0 n/a <br />
通过涉及的页数衡量性能是否有益。低的百分比值意味着表的存贮结构很碎，或是不恰当的cache配置策略。 <br />
Asynchronous Prefetch Activity <br />
0.0 0.0 0 n/a <br />
异步预取情况可结合磁盘I/O管理查看。可看参数&#8220;global async prefetch limit&#8221;。 <br />
Other Asynchronous Prefetch Statistics <br />
APFs Used 0.0 0.0 0 n/a <br />
异步预取合格的页数。 <br />
APF Waits for I/O 0.0 0.0 0 n/a <br />
进程等待异步预取完成的次数。表示查询需要的页没有尽早地完成异步预取，这样进程处于等待状态。出现一定的百分比是合理的：查询的首次异步预取请求通常需要等待；每次的顺序扫描移动到新的分配单元时发出预取请求，查询必须等待第一次的I/O结束；每次非聚簇索引扫描找到合适的行集，也会发出对页的预取请求，也要等待第一次的页返回。 <br />
APF Discards 0.0 0.0 0 n/a <br />
报告已经被异步预取读入但在使用之前就被放弃的页数。如果其值高，建议增加缓冲池的尺寸单位（比如从2K增加4K、8K、16K的缓冲池）以改善性能，或者表示预取进入cache的很多页并不为查询所需要。 <br />
Dirty Read Behavior <br />
Page Requests 0.0 0.0 0 n/a <br />
隔离级为0的脏读请求的页数。 <br />
------------------------------------------------------------------------------- <br />
Cache: default data cache 缺省数据cache的情况： <br />
per sec per xact count % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Spinlock Contentionn/a n/a n/a 0.0 % <br />
自旋锁只对SMP环境有用。当一个用户任务对cache的修改完成之前，其它任务将不能访问该cache而只有等待。虽然自旋锁驻留时间短，但对于高事务率的多处理器系统的性能依然有不好影响，如果自旋锁比例超过10%，应考虑建立命名cache或者是增加cache分片。 <br />
Utilization n/a n/a n/a 100.0 % <br />
下面是cache检查的具体情况： <br />
Cache Searches <br />
Cache Hits 18.6 1.6 11171 89.9 % <br />
Found in Wash 1.1 0.1 677 6.1 % <br />
Cache Misses 2.1 0.2 1251 10.1 % <br />
------------------------- ------------ ------------ ---------- <br />
Total Cache Searches 20.7 1.8 12422 <br />
Pool Turnover <br />
2 Kb Pool <br />
LRU Buffer Grab 0.2 0.0 102 100.0 % <br />
Grabbed Dirty 0.0 0.0 0 0.0 % <br />
------------------------- ------------ ------------ ---------- <br />
Total Cache Turnover 0.2 0.0 102 <br />
Buffer Wash Behavior <br />
Statistics Not Available - No Buffers Entered Wash Section Yet <br />
Cache Strategy <br />
Cached (LRU) Buffers 19.8 1.7 11880 100.0 % <br />
Discarded (MRU) Buffers 0.0 0.0 0 0.0 % <br />
Large I/O Usage <br />
Total Large I/O Requests 0.0 0.0 0 n/a <br />
Large I/O Detail <br />
No Large Pool(s) In This Cache <br />
Dirty Read Behavior <br />
Page Requests 0.0 0.0 0 n/a </p>
<p><br />
单元九：内存管理 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,memory <br />
结果： <br />
Memory Management（内存管理） <br />
per secper xactcount % of total <br />
--------------------------- ------------ ------------ ---------- ---------- <br />
Pages Allocated 0.0 0.0 13 n/a <br />
Pages Released 0.0 0.0 13 n/a </p>
<p>内存中分配一个新页的次数（相当于分配新页数），以及释放内存的页数。 </p>
<p>单元十：磁盘I/O管理 <br />
命令行：sp_sysmon &#8220;00:10:00&#8221;,diskio <br />
结果： </p>
<p>Disk I/O Management（磁盘I/O管理）</p>
<p>-------------------报告server总体磁盘I/O行为，包括读、写和逻辑设备上的semaphore争夺。 <br />
Max Outstanding I/Os per sec per xact count % of total <br />
最大显著I/O数：server总体开销的最大I/O数，分别通过server和引擎表示。 <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Server n/a n/a 10 n/a <br />
Engine 0 n/a n/a 10 n/a <br />
I/Os Delayed by <br />
系统遇到I/O延迟问题，类似于I/O被server或操作系统限制阻塞一样。多数操作系统都有一个参数限制异步I/O数。可用sp_configure查看参数&#8220;allow sql server async i/o&#8221;。 <br />
Disk I/O Structures n/a n/a 0 n/a <br />
达到磁盘I/O结构极限从而被延迟的I/O数。当server超过了可用磁盘I/O的控制块数，I/O就会被延迟，因为server在开始一个I/O请求时需要通过任务来得到一个磁盘I/O控制块。如果其值非零，通过设置增加参数值&#8220;disk i/o structures&#8221;（缺省256）来增加磁盘I/O控制块数，如果操作系统允许尽可能设置大一些，以使用光磁盘I/O结构的机会降到最小。 <br />
Server Config Limit n/a n/a 0 n/a <br />
用参数&#8220;max async i/os per server&#8221;（缺省2147483647）进行调整server一次所用异步磁盘I/O请求数。 <br />
Engine Config Limit n/a n/a 0 n/a <br />
引擎配置最大异步磁盘I/O请求数限制，用参数&#8220;max async i/os per engine&#8221;查看和调整。 <br />
Operating System Limit n/a n/a 0 n/a <br />
操作系统的限制数查看操作系统文档。 <br />
Device Activity Detail <br />
---------------------- <br />
Device: <br />
master.dat <br />
master per sec per xact count % of total <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Reads <br />
APF 0.0 0.0 0 0.0 % <br />
Non-APF 0.2 0.0 102 78.5 % <br />
Writes 0.0 0.0 28 21.5 % <br />
------------------------- ------------ ------------ ---------- ---------- <br />
Total I/Os 0.2 0.0 130 1.5 % <br />
Device Semaphore Granted 0.2 0.0 130 100.0 % <br />
Device Semaphore Waited 0.0 0.0 0 0.0 % <br />
----------------------------------------------------------------------------- </p>
</div>
 <img src ="http://www.blogjava.net/shanben/aggbug/216156.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-20 10:56 <a href="http://www.blogjava.net/shanben/archive/2008/07/20/216156.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sybase常用的命令集合</title><link>http://www.blogjava.net/shanben/archive/2008/07/19/216016.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Sat, 19 Jul 2008 04:40:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/19/216016.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/216016.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/19/216016.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/216016.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/216016.html</trackback:ping><description><![CDATA[以前的文档，整理时发现，贴出<br />
常用的命令集合<br />
一、配置命令<br />
&nbsp; &nbsp;1、检查CPU使用情况：<br />
&nbsp; &nbsp;&nbsp; &nbsp;sp_sysmon "internal","model"<br />
&nbsp; &nbsp;&nbsp; &nbsp;internal是监视时间。例如："00:01:00"<br />
&nbsp; &nbsp;&nbsp; &nbsp;model是模块的名称。包括'kernel','wpm','parallel','taskmgmt','appmgmt','esp','housekeeper','monaccess','xactsum','xactmgmt','indexmgmt','mdcache','locks','dcache','pcache','memory','recovery','diskio','netio'<br />
<br />
&nbsp; &nbsp;2、检查使用的最大的引擎数(CPU)<br />
&nbsp; &nbsp;&nbsp; &nbsp;sp_configure "max online engines"<br />
&nbsp; &nbsp;&nbsp; &nbsp;此命令还可以改变sybase系统使用CPU的数量。<br />
&nbsp; &nbsp;&nbsp; &nbsp;注意：改变后需要重新启动sybase才可以使培植生效。引擎的数量是总的CPU数量减一<br />
<br />
&nbsp; &nbsp;3、显示引擎号、状态、相关任务数以及引擎启动的时间：<br />
&nbsp; &nbsp;&nbsp; &nbsp;select engine, status, affinitied, starttime from sysengines<br />
<br />
&nbsp; &nbsp;4、显示每个用户的繁忙程度<br />
&nbsp; &nbsp;&nbsp; &nbsp;sp_reportstats<br />
<br />
&nbsp; &nbsp;5、监视配置参数的运行情况<br />
&nbsp; &nbsp;&nbsp; &nbsp;sp_monitorconfig "open objects"<br />
&nbsp; &nbsp;&nbsp; &nbsp;参数见sp_monitorconfig中的值<br />
<br />
二、权限命令<br />
&nbsp; &nbsp;1、给一个用户授所有的权限<br />
&nbsp; &nbsp;&nbsp; &nbsp;grant all to user_name<br />
<br />
&nbsp; &nbsp;2、给一个用户授对某张表的所有权限<br />
&nbsp; &nbsp;&nbsp; &nbsp;grant all on table_name to user_name<br />
<br />
&nbsp; &nbsp;3、添加一个登录(login)<br />
&nbsp; &nbsp;&nbsp; &nbsp;sp_addlogin<br />
<br />
三、TSQL使用<br />
&nbsp; &nbsp;1、统计信息更新<br />
&nbsp; &nbsp;&nbsp; &nbsp;select "print '"+name+"'"+char(10)+"update statistics "+name+ char(10) + "go"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from sysobjects<br />
&nbsp; &nbsp;&nbsp; &nbsp; where (type='U' or type='S')<br />
&nbsp; &nbsp;&nbsp; &nbsp; order by name<br />
<br />
&nbsp; &nbsp;2、显示所有触发器的名称、建立时间<br />
&nbsp; &nbsp;&nbsp; &nbsp;SELECT USER_NAME(A.uid) 'Table Owner',<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; B.name 'Table Name',<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; A.name 'Trigger Name',<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; CONVERT(char(10),A.crdate,101)+SUBSTRING(CONVERT(char(20),A.crdate,0),13,7) 'Create Date'<br />
&nbsp; &nbsp;&nbsp; &nbsp;FROM sysobjects A,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;sysobjects B <br />
&nbsp; &nbsp;&nbsp; &nbsp;WHERE A.type='TR' <br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;AND (A.id=B.instrig OR A.id=B.updtrig OR A.id=B.deltrig)<br />
<br />
&nbsp; &nbsp;3、关于参照<br />
&nbsp; &nbsp;&nbsp; &nbsp;--生成建立已有参照的SQL语句<br />
&nbsp; &nbsp;&nbsp; &nbsp;select distinct result_sql='alter table '+rtrim(object_name(tableid))+' add constraint '<br />
&nbsp; &nbsp;&nbsp; &nbsp;+rtrim(object_name(constrid))+' foreign key('+<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.tableid,sysreferences.fokey1)+substring(',',1,sysreferences.fokey2)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.tableid,sysreferences.fokey2)+substring(',',1,sysreferences.fokey3)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.tableid,sysreferences.fokey3)+substring(',',1,sysreferences.fokey4)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.tableid,sysreferences.fokey4)+substring(',',1,sysreferences.fokey5)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.tableid,sysreferences.fokey5)+substring(',',1,sysreferences.fokey6)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.tableid,sysreferences.fokey6)+substring(',',1,sysreferences.fokey7)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.tableid,sysreferences.fokey7)+') '<br />
&nbsp; &nbsp;&nbsp; &nbsp;+'references '+rtrim(object_name(reftabid))+'('<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.reftabid,sysreferences.refkey1)+substring(',',1,sysreferences.refkey2)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.reftabid,sysreferences.refkey2)+substring(',',1,sysreferences.refkey3)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.reftabid,sysreferences.refkey3)+substring(',',1,sysreferences.refkey4)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.reftabid,sysreferences.refkey4)+substring(',',1,sysreferences.refkey5)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.reftabid,sysreferences.refkey5)+substring(',',1,sysreferences.refkey6)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.reftabid,sysreferences.refkey6)+substring(',',1,sysreferences.refkey7)<br />
&nbsp; &nbsp;&nbsp; &nbsp;+col_name(sysreferences.reftabid,sysreferences.refkey7)+')' <br />
&nbsp; &nbsp;&nbsp; &nbsp;+char(10)+"go"<br />
&nbsp; &nbsp;&nbsp; &nbsp; from sysreferences,syscolumns,sysobjects <br />
&nbsp; &nbsp;&nbsp; &nbsp;where sysreferences.tableid=sysobjects.id<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;and sysobjects.id=syscolumns.id<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;and sysobjects.type='U' <br />
&nbsp; &nbsp;&nbsp; &nbsp;order by sysobjects.name<br />
&nbsp; &nbsp;&nbsp; &nbsp;go<br />
&nbsp; &nbsp;&nbsp; &nbsp;--生成删除参照的SQL语句<br />
&nbsp; &nbsp;&nbsp; &nbsp;select distinct result_sql='alter table '+rtrim(object_name(sysreferences.tableid))+' drop constraint '+rtrim(object_name(sysreferences.constrid))+char(10)+"go"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from sysreferences,syscolumns,sysobjects <br />
&nbsp; &nbsp;&nbsp; &nbsp; where sysreferences.tableid=sysobjects.id<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and sysobjects.id=syscolumns.id<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and sysobjects.type='U'<br />
&nbsp; &nbsp;&nbsp; &nbsp; order by object_name(sysreferences.tableid)<br />
&nbsp; &nbsp;&nbsp; &nbsp;go<br />
<br />
&nbsp; &nbsp;4、关于表级锁<br />
&nbsp; &nbsp;&nbsp; &nbsp;--列出所有锁类型为行锁的表<br />
&nbsp; &nbsp;&nbsp; &nbsp;select name<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from sysobjects<br />
&nbsp; &nbsp;&nbsp; &nbsp; where type='U'<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and sysstat2 &amp; 57344 = 32768<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and name not like "XT%"<br />
&nbsp; &nbsp;&nbsp; &nbsp; order by name <br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;--调优的方法有：调整锁类型，调整表的聚簇索引<br />
&nbsp; &nbsp;&nbsp; &nbsp;--调整表锁类型为行锁，可以提高对表的并发访问性能，但是同时也会占用更多的系统资源<br />
&nbsp; &nbsp;&nbsp; &nbsp;--修改行锁的SQL语句：alter table [table_name] lock [datarows|allpages]<br />
&nbsp; &nbsp;&nbsp; &nbsp;--<br />
&nbsp; &nbsp;&nbsp; &nbsp;--调整表的聚簇索引在数据插入时，降低对表的最后一页的争用<br />
&nbsp; &nbsp;&nbsp; &nbsp;--Sybase规定一张表只能有一个聚簇索引，所以在调整聚簇索引时，要先删除原有的聚簇索引，然后再建立新的<br />
&nbsp; &nbsp;&nbsp; &nbsp;--例如将SB_ZSXX表的在ZSXH上聚簇索引修改为在NSRSBH上的聚簇索引.<br />
&nbsp; &nbsp;&nbsp; &nbsp;--删除原聚簇索引PK_SB_ZSXX<br />
&nbsp; &nbsp;&nbsp; &nbsp;drop index SB_ZSXX.PK_SB_ZSXX<br />
&nbsp; &nbsp;&nbsp; &nbsp;go<br />
&nbsp; &nbsp;&nbsp; &nbsp;create&nbsp;&nbsp;nonclustered index PK_SB_ZSXX on SB_ZSXX(ZSXH)<br />
&nbsp; &nbsp;&nbsp; &nbsp;go<br />
&nbsp; &nbsp;&nbsp; &nbsp;-- 建立IDX_SB_ZSXX_NSRSBH的聚簇索引<br />
&nbsp; &nbsp;&nbsp; &nbsp;drop index SB_ZSXX.IDX_SB_ZSXX_NSRSBH<br />
&nbsp; &nbsp;&nbsp; &nbsp;go<br />
&nbsp; &nbsp;&nbsp; &nbsp;create&nbsp;&nbsp;clustered index IDX_SB_ZSXX_NSRSBH on SB_ZSXX(NSRSBH)<br />
&nbsp; &nbsp;&nbsp; &nbsp;go<br />
&nbsp; &nbsp;&nbsp; &nbsp;--以上的索引信息可以用sp_help命令列出<br />
<br />
&nbsp; &nbsp;5、求出指定字段等于指定值的记录数<br />
&nbsp; &nbsp;&nbsp; &nbsp;declare @lc_colname varchar(30)<br />
&nbsp; &nbsp;&nbsp; &nbsp;--declare @lc_colvalue /*定义欲求值的变量，随着变量的类型定义不同的值*/<br />
&nbsp; &nbsp;&nbsp; &nbsp;--取包含指定列名的表<br />
&nbsp; &nbsp;&nbsp; &nbsp;select distinct name=object_name(id) into #AA from syscolumns where name = @lc_colname<br />
&nbsp; &nbsp;&nbsp; &nbsp;--生成SQL，求出指定字段等于指定值的记录数<br />
&nbsp; &nbsp;&nbsp; &nbsp;select "insert into #BB select tname='"+name+"',cnt=count(*) from "+name+" where " + @lc_colname + "='" + @lc_colvalue + "'"+char(10)+"go"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from #AA<br />
&nbsp; &nbsp;&nbsp; &nbsp;--运行SQL<br />
&nbsp; &nbsp;&nbsp; &nbsp;--检查记录不为零的表，既为求出的表<br />
&nbsp; &nbsp;&nbsp; &nbsp;select * from #BB where cnt&gt;0<br />
<br />
&nbsp; &nbsp;6、列出所有无主键的表<br />
&nbsp; &nbsp;&nbsp; &nbsp;select name from sysobjects<br />
&nbsp; &nbsp;&nbsp; &nbsp; where name not in (<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;SELECT object_name(id)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;FROM&nbsp; &nbsp;sysindexes<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;WHERE&nbsp;&nbsp;indid &gt; 0<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; AND status2 &amp; 2 = 2<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; and status &amp; 2048 = 2048<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; )<br />
&nbsp; &nbsp;&nbsp; &nbsp; and type="U"<br />
&nbsp; &nbsp;&nbsp;&nbsp;order by name<br />
<br />
&nbsp; &nbsp;7、关于用户登录<br />
&nbsp; &nbsp;&nbsp; &nbsp;--锁定用户<br />
&nbsp; &nbsp;&nbsp; &nbsp;select "sp_locklogin "+name+",'lock'"+char(10)+"go"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from syslogins<br />
&nbsp; &nbsp;&nbsp; &nbsp; where name not like "%prim%"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and name not like "%maint%"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and name not like "sa"<br />
&nbsp; &nbsp;&nbsp; &nbsp; order by name<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp; --解除锁定的用户<br />
&nbsp; &nbsp;&nbsp; &nbsp; select "sp_locklogin "+name+",'unlock'"+char(10)+"go"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from syslogins<br />
&nbsp; &nbsp;&nbsp; &nbsp; where name not like "%prim%"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and name not like "%maint%"<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and name not like "sa"<br />
&nbsp; &nbsp;&nbsp; &nbsp; order by name<br />
&nbsp; &nbsp;8、为ctais_maint授权<br />
&nbsp; &nbsp;&nbsp; &nbsp;select "grant all on "+name+" to ctais_maint"+char(10)+"go" from sysobjects where type="U"<br />
<br />
&nbsp; &nbsp;9、整理出字段类型有text和image的表<br />
&nbsp; &nbsp;&nbsp; &nbsp;--text类型<br />
&nbsp; &nbsp;&nbsp; &nbsp;select distinct name=object_name(id)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from syscolumns<br />
&nbsp; &nbsp;&nbsp; &nbsp; where type=35<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and id in (select id from sysobjects where type="U")<br />
&nbsp; &nbsp;&nbsp; &nbsp; order by object_name(id)<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;--image类型<br />
&nbsp; &nbsp;&nbsp; &nbsp;select distinct name=object_name(id)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from syscolumns<br />
&nbsp; &nbsp;&nbsp; &nbsp; where type=34<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;and id in (select id from sysobjects where type="U")<br />
&nbsp; &nbsp;&nbsp; &nbsp; order by object_name(id)<br />
<br />
&nbsp; &nbsp;10、修改identity列的最大值<br />
&nbsp; &nbsp;&nbsp; &nbsp;--设置包含identity列的表为可直接插入记录<br />
&nbsp; &nbsp;&nbsp; &nbsp;set identity_insert [table] [on|off]<br />
&nbsp; &nbsp;&nbsp; &nbsp;--插入一个比较大的值<br />
&nbsp; &nbsp;&nbsp; &nbsp;insert into [table] values([value])<br />
<img src ="http://www.blogjava.net/shanben/aggbug/216016.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-19 12:40 <a href="http://www.blogjava.net/shanben/archive/2008/07/19/216016.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>查寻提示改变优化器 </title><link>http://www.blogjava.net/shanben/archive/2008/07/16/215161.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Wed, 16 Jul 2008 03:23:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/16/215161.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/215161.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/16/215161.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/215161.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/215161.html</trackback:ping><description><![CDATA[<p>在Sybase中我们可以象在<a class="bluekey" href="http://whatis.ctocio.com.cn/searchwhatis/14/6093014.shtml" target="_blank">Oracle</a>那样给予查寻提示吗？如何做呢？<br />
发表于04年8月9日</p>
<p>　　象Oracle系统那样，在Sybase中优化器提示可以更改优化器。它们应当更慎重地使用。当数据建模正确实行，必要的索引存在，及时地更新统计，仔细编写代码后，ASE优化器可以完成出色的优化工作。</p>
<p>ASE允许你说明优化器改动如下：<br />
[ SET FORCEPLAN ON ]</p>
<p>SELECT columns<br />
&nbsp; FROM table ( [ INDEX indid or name | PREFETCH size | MRU | LRU ] )<br />
&nbsp;WHERE columns = something</p>
<p>UPDATE table<br />
&nbsp;&nbsp; SET columns = something<br />
&nbsp; FROM table ( [ INDEX indid or name | PREFETCH size | MRU | LRU ] )<br />
&nbsp;WHERE columns = something</p>
<p>DELETE table<br />
&nbsp; FROM table ( [ INDEX indid or name | PREFETCH size | MRU | LRU ] )<br />
&nbsp;WHERE columns = something</p>
<p>[ SET FORCEPLAN OFF ]</p>
<p>指定索引</p>
<p>　　通过用关键字INDEX及说明indid或索引名称你可以强制使用特定的索引，如果你必须强迫使用一个索引，你应当一直用索引名称而不是indid。这是因为indid是根据生成的次序分派给许多索引，例行的数据库维护会改变indid。</p>
<p>你也可以用( INDEX 0 )来定义一个图表扫描。这意味着ASE要进行一次全图表扫描。</p>
<p><br />
强制索引<br />
SELECT columns<br />
&nbsp; FROM table ( INDEX index_name )<br />
&nbsp;WHERE columns = something</p>
<p>UPDATE table<br />
&nbsp;&nbsp; SET columns = something<br />
&nbsp; FROM table ( INDEX index_name )<br />
&nbsp;WHERE columns = something</p>
<p>DELETE table<br />
&nbsp; FROM table ( INDEX index_name )<br />
&nbsp;WHERE columns = something</p>
<p>To force a Full Table Scan: <br />
SELECT columns<br />
&nbsp; FROM table ( INDEX 0 )<br />
&nbsp;WHERE columns = something</p>
<p>UPDATE table<br />
&nbsp;&nbsp; SET columns = something<br />
&nbsp; FROM table ( INDEX 0 )<br />
&nbsp;WHERE columns = something</p>
<p>DELETE table<br />
&nbsp; FROM table ( INDEX 0 )<br />
&nbsp;WHERE columns = something</p>
<p><br />
　　象我们前面提到的，使用提示会引起应用维护和最甲优化策略的应用问题，如果你要强制实施索引或图表扫描，不要在应用代码内执行。用索引或图表扫描生成一个view，用view作为图表。这将使你的维护方便很多，并且指明了强制索引或图表扫描的对象。</p>
<p>例如</p>
<p>CREATE<br />
&nbsp; VIEW vw_[index_name | scan]_test_table<br />
AS<br />
SELECT columns<br />
&nbsp; FROM table ( INDEX index_name | 0 )<br />
go</p>
<p><br />
这段语句访问了图表，强制实施了一种方法，并可以用作view</p>
<p><br />
例如</p>
<p>SELECT columns<br />
&nbsp; FROM vw_[index_name | scan]_table<br />
&nbsp;WHERE columns = something</p>
<p>UPDATE table<br />
&nbsp;&nbsp; SET columns = something<br />
&nbsp; FROM vw_[index_name | scan]_table<br />
&nbsp;WHERE columns = something</p>
<p>DELETE table<br />
&nbsp; FROM vw_[index_name | scan]_table<br />
&nbsp;WHERE columns = something</p>
<p>连接次序</p>
<p>用SET FORCEPLAN [ ON | OFF ]实施连接次序</p>
<p>　　警告！！！实施连接次序会增加应用的维护工作。另外系统的发展也会产生一条更有效的途径。</p>
<p>　　根据选项设定的SET FORCEPLAN ON all查寻将用它们在查寻中FROM子句的次序来连接。</p>
<p>例如<br />
SET FORCEPLAN ON</p>
<p>SELECT t1.columns<br />
&nbsp; FROM table1 t1,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; table2 t2<br />
&nbsp;WHERE t1.column01 = t2.column01</p>
<p>SET FORCEPLAN OFF<br />
go</p>
<p>　　在前面的例子中，我们强制ASE在作为Outer图表的test_table1上进行扫描，根据统计情况这可能是一件很坏的事，只要非常熟练的DBA才能用这样的逻辑来实施。<br />
</p>
<img src ="http://www.blogjava.net/shanben/aggbug/215161.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-16 11:23 <a href="http://www.blogjava.net/shanben/archive/2008/07/16/215161.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sybase配置参数</title><link>http://www.blogjava.net/shanben/archive/2008/07/09/213731.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Wed, 09 Jul 2008 09:32:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/09/213731.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/213731.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/09/213731.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/213731.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/213731.html</trackback:ping><description><![CDATA[<div class="subtable altbg2 t_msg" style="width: auto; height: auto"><strong>Sybase配置参数</strong><br />
<br />
sp_configure 'max online engines',4<br />
go<br />
--配置启动cpu个数<br />
sp_configure 'number of engines at startup',4 <br />
go<br />
--配置最大内存数<br />
sp_configure 'max memory' ,2097151<br />
go<br />
--分配最大存储过程缓存<br />
sp_configure 'procedure cache',102400<br />
go<br />
--配置高速缓存<br />
sp_cacheconfig&nbsp;&nbsp;'default data cache' , '700M'<br />
go<br />
--缺省缓存分配页大小<br />
sp_poolconfig 'default data cache','200M','16K'<br />
go<br />
--网络包大小<br />
sp_configure 'max network packet size',1024<br />
go<br />
<br />
--最大连接数<br />
sp_configure 'number of user connections',500<br />
go<br />
--最大打开对象<br />
sp_configure&nbsp;&nbsp;'number of open object',9000<br />
go<br />
--最大索引<br />
sp_configure&nbsp;&nbsp;'number of open index',10000<br />
go<br />
--最大锁数<br />
sp_configure&nbsp;&nbsp;'number of locks',100000<br />
go<br />
<br />
--增加网络内存<br />
--sp_configure&nbsp;&nbsp;'additional network memory',1024<br />
go<br />
<br />
--锁内存<br />
sp_configure&nbsp;&nbsp;'lock shared memory',512<br />
go<br />
<br />
--优化tempdb<br />
select dbid, name,segmap<br />
from sysusages,&nbsp;&nbsp;sysdevices<br />
where sysdevices.low&nbsp;&nbsp;&lt;= sysusages.size +vstart<br />
and sysdevices.high &gt;=sysusages.size+vstart -1<br />
and dbid =2<br />
and (status=2 or status=3) <br />
go<br />
use tempdb<br />
go<br />
sp_dropsegment&nbsp;&nbsp;'default',tempdb,master<br />
go<br />
sp_dropsegment&nbsp;&nbsp;'logsegment',tempdb,master<br />
go<br />
<br />
<br />
select dbid, name,segmap<br />
from sysusages,&nbsp;&nbsp;sysdevices<br />
where sysdevices.low&nbsp;&nbsp;&lt;= sysusages.size +vstart<br />
and sysdevices.high &gt;=sysusages.size+vstart -1<br />
and dbid =2<br />
and (status=2 or status=3) <br />
go<br />
<br />
sp_cacheconfig tempdb_cache, '100M'<br />
go<br />
sp_poolconfig tempdb_cache,'50M','16K'<br />
go<br />
sp_bindcache 'tempdb_cache',tempdb<br />
go<br />
sp_helpcache tempdb_cache<br />
<br />
select name,id from syscharsets <br />
<br />
dbcc traceon(3604)<br />
dbcc memusage<br />
<br />
<br />
<br />
1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 数据库安装的优化<br />
1.1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 应用数据库使用裸设备<br />
若在UNIX（UNIXWARE）操作系统下安装数据库服务器，请将SYBASE应用数据库的设备(device)安装成裸设备。即在创建应用数据库设备（如：IVSP，DB160，NAP2000等）时用裸设备，把文件名指向 /dev/dsk/ 子目录下的相应文件。系统数据库设备（如:master等）仍然指向文件系统。<br />
1.2&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 补丁程序<br />
完成SYBASE数据库的安装，请注意原版的SYBASE软件都会带有最新的补丁，一定要把补丁打上，否则会出现一些莫名其妙的问题。FOR NT 版的补丁是一个ZIP文件，解压至C:\SYBASE子目录即可。<br />
1.3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 安装 SYBASE 在线帮助<br />
在安装好 SYBASE 后，在安装目录下有一文件：../scripts/ins_syn_sql，在服务器上执行该脚本：<br />
Sybase for Unix版：&nbsp; &nbsp;&nbsp;&nbsp;./isql -Usa -P -i../scripts/ins_syn_sql<br />
Sybase for Winnt版：&nbsp; &nbsp; isql -Usa -P -i\sybase\scripts\ins_syn_sql<br />
执行完毕后，即可在任意的 SYBASE 客户端上连接上 SQL SERVER ，在线取得任意命令的帮助：<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;sp_syntax "关键字"<br />
如：&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;sp_syntax "alter"&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;即可列出所有包含"alter"字符的命令<br />
2&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 数据库配置的优化<br />
2.1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 优化master数据库<br />
首先加大master设备空间，初始默认为30M，加大为150M。然后加大master数据库空间，默认数据段和日志段各为5M大小，建议改为数据段100M，日志段50M。<br />
alter database master on master=95<br />
2.2&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 优化tempdb数据库<br />
首先创建tempdb设备，分配给tempdb数据库，默认tempdb数据库数据段和日志段各为2M大小，并创建在master设备上，建议改为数据段200M，日志段50M，创建在tempdb设备上。<br />
alter database tempdb on tempdb=200<br />
<br />
SQL SEVRER所有用户都共享工作表和临时表的tempdb数据库，tempdb主要瓶颈是磁盘I/0。解决办法是把tempdb放在更快的设备上。在UNIX环境中，把tempdb放在文件系统中而不用原始的设备。由于tempdb在创建数据时，自动在master设备上创建为2M的数据库，为了减少冲突，最好的办法是把tempdb从master设备中移走。为了达到上述目的，可采用如下办法实现：<br />
<br />
1：在单用户状态下启动SQL SERVER<br />
启动单用户方法：dataserver -dmaster.dat -m<br />
2：以sa登录<br />
3：在文件系统中创建一个哑数据库。<br />
4：删除sysusages和 sysdatabase表中对现有tempdb数据库的引用<br />
5：获取哑数据库的数据库ID，相应修改sysusages和 ysdatabase表对tempdb的引用<br />
6：重新启动数据库<br />
以在newdevice中创建200M的tempdb数据库为例，执行过程如下：<br />
create database newtemp on newdevice=200<br />
go /* 创建新的数据库 */<br />
begin tran<br />
go /* 开始事务，防止操作错误时破坏整个SQL SERVER的运行*/<br />
delete sysusages where dbid = 2<br />
delete sysdatabases where dbid = 2<br />
go /*删除系统表对tempdb的引用，只能在单用户状态下执行*/<br />
select dbid from sysdatabases where name = &#8216;newtemp&#8217;<br />
<br />
go /*获取newtemp数据库ID，假定为10*/<br />
<br />
update sysusages set dbid = 2 where dbid=10<br />
<br />
update sysdatabases set name=&#8217;tempdb&#8217;,dbid=2 where name=&#8217;newtemp&#8217;<br />
<br />
go /*修改数据库的引用，对newtemp的引用改为对tempdb的引用*/<br />
<br />
select name,dbid, from sysdatabases where name = &#8216;tempdb&#8217;<br />
<br />
select * from sysusages where dbid = 2<br />
<br />
go /*测试修改是否正确，正确则提交，否则可用rollback回退*/<br />
<br />
commit tran<br />
<br />
go /*修改成功，重新启动系统*/<br />
<br />
这种方法只对tempdb有效，其他数据库不能采用这种方法。因为在SQL SERVER启动时，tempdb每次都重新初始化。<br />
2.3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 优化系统参数<br />
以下参数为标准建议值，可根据实际情况修改。<br />
优化系统参数的SQL脚本&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;注释<br />
sp_configure 'total memory', 100000&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;优化数据库的内存，应根据不同机器配置设置, 建议为一半的物理内存大小。以db_block为单位，即每个单位为2k，上例为200M，默认为24M.<br />
sp_configure "lock scheme" , 1,"datarows"&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;系统默认为表级锁，优化为行锁<br />
sp_configure "number of locks" , 10000&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;加大最大锁进程数, 默认为5000。<br />
（如果设置一个已经存在的表(tabel)的锁方式为行锁，则执行：<br />
alter table table_name lock datarows）<br />
sp_configure 'procedure cache percent' ,30&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;缺省值：20 建议值：procedure使用频率高时采用较大的值，不超过30<br />
sp_configure "number of user connections",100&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;最大用户连接数，默认为25，每个连接要占70k内存<br />
sp_configure 'number of devices',20&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;将最大设备文件数据改为15个<br />
sp_configure &#8216;number of Open databases&#8217;,20&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;最大打开数据库个数，对于需在一台数据库服务上打个多个数据库则需加大此参数，默认为15<br />
Sp_configure &#8216;max online engines CPU&#8217;,2&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;缺省值：1 建议值：采用实际机器的CPU个数<br />
Sp_configure &#8216;total data cache size&#8217;,60000&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;缺省值：0 建议值：使用SQL SERVER内存的30%<br />
如果上述参数改动后SYBASE启动不正常，则可检查SYBASE的错误日志，把SYBASE.cfg中的相应选项修改为较小的值。<br />
附：SYBASE SQL SERVER 内存的分配<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;1. SQL SERVER 可执行代码&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;3-4M<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;2. SQL SERVER 使用的静态内存&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;2.2-3.25M<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;3. 用户可配置的参数所占用内存，以下示例(11.9.2版)：<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;默认值&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 占用内存<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;用户连接数(user connections)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;25&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 每个 约70k<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;打开的数据库数(open database)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;12&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 每个 约60k<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;打开的对象数(open objects)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 500&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 每个 约1k<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;打开的索引数(open indexs)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 500&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 每个 约1k<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;锁数目(locks)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 5000&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;每个 约0.1k<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;数据库设备数(data device)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 10&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 每个 约0.5k<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;4. 剩余部份分配给<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;过程缓存 ( 由 procedure cache percent 决定，默认值为 20% )<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;数据缓存 ( 默认值为减去1、2、3项的 80% )<br />
2.4&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 优化数据库系统属性<br />
在sybase center中选择数据库属性，将属性中options选项中的下列项目选中。<br />
allow select into/bulk copy<br />
truncate log on checkpoint<br />
checkpoint on recovery<br />
abort transction on full log<br />
free space accounting<br />
allow nulls by default<br />
auto identity column in non-unquie index <br />
方法二：在SQLPLUS中执行下列SQL脚本<br />
如：<br />
sp_dboption mydb,"abort tran on log full",true（设定当数据库的日志空间满时，就终止该进程，使用 sa 用户）<br />
sp_dboption mydb," select into/bulkcopy ",true<br />
sp_dboption mydb," trunc log on chkpt ",true<br />
sp_dboption mydb," no chkpt on recovery",true<br />
sp_dboption mydb," no free space acctg ",true<br />
sp_dboption mydb,"allow nulls by default",true<br />
sp_dboption mydb," auto identity dbo use only ",true<br />
2.5&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 创建阈值存储过程<br />
可根据不同的应用修改以下脚本或创建多个阈值存储过程，并在配置阈值时指定相应的存储过程。<br />
&nbsp; &nbsp;create procedure sp_thresholdaction <br />
&nbsp; &nbsp;@dbname varchar(30),@segmentname varchar(30), @free_space int,@status int&nbsp;&nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;as dump transaction @dbname with no_log <br />
print "LOG DUMP: '%1!' for '%2!' dumped",@segmentname,@dbname<br />
go<br />
2.6&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 配置多个阈值<br />
方法一： <br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;打开 Sybase Central，双击相应数据库(database)的段 Segments -&gt;; logsegment，在&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Thresholds 页面中可设置自动清除日志的阀值。其中有 Last Chance 的一行是系统默认的最后机会阀值，即系统日志空闲空间小于该值时为最后一次自动清除日志的机会。设置时阀值的大小可设为日志总空间大小的20%左右。<br />
另外再增加多个阈值。<br />
方法二：<br />
1、使用如下指令查出数据库中日志的容量（用页表示）<br />
select sum(size) from master..sysusages where dbid=db_id("database_name" and (segmap&amp;4)=4<br />
2、使用sp_addthreshold增加新的阈值，大小为日志容量的50%,如上面语句显示值为2048<br />
sp_addthreshold database_name,logsegment,1024，proc_dump_display<br />
注意：因一个大事务时可能会越过当前的threshold，所以必须加多个threshold，<br />
使用命令select @@thresh_hysteresis查看数据库的滞后值，如结果为64页，则下一个阈值设为"最近的阈值-（2*64）"，请在所设阈值再按这种原则各增加两个更小的阈值。<br />
3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 索引的优化<br />
在良好的数据库设计基础上，需高效地使用索引，并经常的维护索引，下文介绍关于索引的相关内容。<br />
3.1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 创建索引<br />
索引分为三类：聚簇索引(clustered indexes)、非聚簇索引(nonclustered indexes)、覆盖索引(covering indexes)<br />
鉴于索引加快了查询速度，但减慢了数据更新速度的特点。可通过在一个段上建表，而在另一个段上建其非聚簇索引，而这两段分别在单独的物理设备上来改善操作性能。<br />
create [unique][clustered|nonclustered] index index_name on table_name(column_name...)<br />
3.2&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 重建索引<br />
　　随着数据行的插入、删除和数据页的分裂，有些索引页可能只包含几页数据，另外应用在执行大块I/O的时候，重建非聚簇索引可以降低分片，维护大块I/O的效率。重建索引实际上是重新组织B-树空间。在下面情况下需要重建索引：<br />
　　(1)、数据和使用模式大幅度变化。<br />
　　(2)、排序的顺序发生改变。<br />
　　(3)、要进行大量插入操作或已经完成。<br />
　　(4)、使用大块I/O的查询的磁盘读次数比预料的要多。<br />
　　(5)、由于大量数据修改，使得数据页和索引页没有充分使用而导致空间的使用超出估算。<br />
　　(6)、dbcc检查出索引有问题。<br />
　　当重建聚簇索引时,这张表的所有非聚簇索引将被重建.<br />
3.3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 索引统计信息的更新<br />
　　当在一个包含数据的表上创建索引的时候，SQL Server会创建分布数据页来存放有关索引的两种统计信息：分布表和密度表。优化器利用这个页来判断该索引对某个特定查询是否有用。但这个统计信息并不动态地重新计算。这意味着，当表的数据改变之后，统计信息有可能是过时的，从而影响优化器追求最有工作的目标。因此，在下面情况下应该运行update statistics命令：<br />
　　(1)、数据行的插入和删除修改了数据的分布。<br />
　　(2)、对用truncate table删除数据的表上增加数据行。<br />
　　(3)、修改索引列的值。<br />
4&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 查询优化<br />
4.1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; NOT IN子句<br />
不知大家是否喜欢使用&#8216;NOT IN&#8217;这样的操作，如果是，那尽量使用(NOT) EXISTS 替代。<br />
例子：<br />
语句1<br />
SELECT dname, deptno FROM dept&nbsp;&nbsp;WHERE deptno NOT IN (SELECT deptno FROM emp);<br />
语句2<br />
SELECT dname, deptno FROM dept WHERE NOT EXISTS (SELECT deptno FROM emp WHERE dept.deptno = emp.deptno);<br />
明显的，2要比1的执行性能好很多，因为1中对emp进行了full table scan,这是很浪费时间的操作。而且1中没有用到emp的index，因为没有where子句。而2中的语句对emp进行的是range scan。<br />
4.2&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 海量查询<br />
在海量查询时尽量少用格式转换。<br />
如用<br />
WHERE a.order_no = b.order_no<br />
而不用<br />
WHERE TO_NUMBER (substr(a.order_no, instr(b.order_no, '.') - 1)= TO_NUMBER (substr(a.order_no, instr(b.order_no, '.') - 1)<br />
3）查询海量数据是可以使用optimizer hints，例如/*+ORDERED */ <br />
如<br />
SELECT /*+ FULL(EMP) */ E.ENAME FROM EMP E WHERE E.JOB = 'CLERK';<br />
而不是<br />
SELECT E.ENAME FROM EMP E WHERE E.JOB || '' = 'CLERK';<br />
<br />
<br />
<br />
对于数据量较大和业务功能较复杂的系统，Sybase的默认参数根本无法满足需要，必须进行优化。系统数据库方面的优化从两方面进行，一个是调整数据库系统的一些性能参数的值，另一个是应用程序的调优。应用程序的调优调整hsql和sql的写法，配合sql合理的建索引，这里主要对Sybase系统一些基本的性能参数的调优进行一个小结。
<p>1、内存<br />
&nbsp;&nbsp; 内存是对性能影响最大，也是最需要也是最难调优的地方。内存调优一定要，常用的需要调整的参数有：<br />
<br />
sp_configure &#8220;max memory&#8221;,0,&#8221;2600M&#8221; (设置为共享内存的75%,重启生效)<br />
sp_configure &#8220;allocate max shared mem&#8221;,1 (启动的时候自动分配max memory指定的最大内存)<br />
sp_cacheconfig &#8220;default data cache&#8221;,&#8221;1300m&#8221;(设置数据缓存为max memory的一半)<br />
sp_cacheconfig &#8220;default data cache&#8221;,&#8221;cache_partition=2&#8243;<br />
sp_configure &#8220;procedure cache size&#8221;,102400 (过程高速缓存,通常是max mem20%-30%,这里是200M,在大量的执行sql的时候这个参数一定要调大)<br />
sp_cacheconfig &#8216;tempdb_cache&#8217;,'100m&#8217;,'mixed&#8217; (创建一个100M命名高速缓存tempdb_cache给temdpb使用)<br />
sp_bindcache &#8216;tempdb_cache&#8217;,tempdb (将tempdb_cache绑定到tempdb)</p>
<p>&nbsp;&nbsp; 实际中遇到一个很头痛的问题， 32位Windows版本的Sybase 最大内存只能到搞到3G左右，"default data cache"的值超过 1500M Sybase实例就起不来了，导致服务器的16G内存形同虚设，所以如果大家的项目和我这个类似，服务器和操作系统由客户提供，还换不了的，内存不妨要求个4G就够了，多了也浪费。</p>
<p>2、CPU<br />
&nbsp;&nbsp; 当服务器的CPU个数多于一个时，可以考虑多CPU。实际上对于OS会自动调度，设一下只不过是控制的更精确一点。实际需要根据CPU数来修改，若CPU=N，一般设置为N-1。设置这个参数，比如我的服务器8个CPU, 就像下面这样设置：</p>
<p>sp_configure &#8220;max online engines&#8221;,7<br />
sp_configure &#8220;number of engines at startup&#8221;,7<br />
sp_configure &#8220;max parallel degree&#8221;,1 (并行的度，大于或等于1)<br />
sp_configure &#8220;number of worker processes&#8221;,7 (并行度＊并发连接数＊1.5倍)</p>
<p>3、连接数(这个没什么说的，数量管够就可以，默认数为25，可根据应用需要来修改。)<br />
sp_configure &#8220;number of user connections&#8221;,600</p>
<p>查询数据库死进程<br />
select * from master..syslogshold</p>
<p>4、锁<br />
&nbsp;&nbsp; 数据库的锁机制其实是一个比较复杂的话题，这里只能简单说一下。Sybase数据库系统两个级别的锁机制：所有页锁、数据页锁。所有页锁在当数据库加锁时，既锁数据页，也锁索引页；数据页锁当数据库加锁时，只锁数据页，不锁索引页。<br />
&nbsp;&nbsp; Sybase支持三种类型的锁: 数据表锁、数据页锁、数据行锁。 一些常用的调优命令和策略如下:</p>
<p>sp_configure "number of locks",50000 （设置锁的数量）<br />
&nbsp;&nbsp;&nbsp; 系统设置时要把锁的数量设大一点，简单说就是要管够；如果需要节省空间，减少维护量，使用所有页锁机制；而如果需要加快速度，空间足够，使用数据页锁机制。</p>
<p>&nbsp;&nbsp; sp_sysmon &#8220;00:10:00&#8221;,locks （检测表的使用情况）<br />
&nbsp;&nbsp; 当通过监测发现锁竞争超过15%时，首先修改加锁最重的表的锁机制，然后再把数据页锁设置为数据行锁。如果发现螺旋锁多，则为该表建立单独的命名缓存并对命名缓存进行分区。</p>
<p>5、I/O<br />
&nbsp;&nbsp; 数据库调优总的思路是尽量减少和分散物理I/O，尽量减少网络I/O。</p>
<p>&nbsp;&nbsp; 减少物理I/O的办法有： 在命名缓存中增加大块的I/O缓冲池，把数据分散到多个硬盘上,采用RAID技术,建立段，使一个表跨越多个硬盘等等，基本和其他的数据库软件调优一样。<br />
&nbsp;&nbsp; 减少网络I/O的办法是采用大数据包。<br />
sp_configure "default network packet size",2048 设置网络传送包的大小(需要重启动)<br />
sp_configure "max network packet size",2048</p>
<p>6、设备调整<br />
&nbsp;&nbsp; 主要调整两块： 一个是业务数据库的数据设备与日志设备必须分开，添加临时数据库设备；另一个是调整Tempdb，这一条很重要却很容易被大家忽视。<br />
&nbsp;&nbsp; Tempdb是sybase数据库当中的临时库，用于存放中间结果和临时表。由于使用很频繁而默认大小又很小，我们需要加大其设备空间和库的大小，尽可能把tempdb放置到最快的硬盘上，并建立单独的命名缓存。<br />
sp_cacheconfig &#8216;tempdb_cache&#8217;,'200m&#8217;,'mixed&#8217; (创建一个200M命名高速缓存tempdb_cache给temdpb使用)<br />
sp_bindcache &#8216;tempdb_cache&#8217;,tempdb (将tempdb_cache绑定到tempdb)</p>
</div>
<img src ="http://www.blogjava.net/shanben/aggbug/213731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-09 17:32 <a href="http://www.blogjava.net/shanben/archive/2008/07/09/213731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sybase 常用sql语句</title><link>http://www.blogjava.net/shanben/archive/2008/07/08/213244.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Tue, 08 Jul 2008 02:31:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/08/213244.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/213244.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/08/213244.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/213244.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/213244.html</trackback:ping><description><![CDATA[<p>常用sql语句</p>
<p>1. 查看数据库的版本 <br />
select @@version <br />
<br />
2. 查看数据库所在机器操作系统参数 <br />
exec master..xp_msver <br />
<br />
3. 查看数据库启动的参数 <br />
sp_configure <br />
<br />
4. 查看数据库启动时间 <br />
select convert(varchar(30),login_time,120) from master..sysprocesses where spid=1 <br />
查看数据库服务器名和实例名 <br />
print 'Server Name...............：' + convert(varchar(30),@@SERVERNAME) <br />
print 'Instance..................：' + convert(varchar(30),@@SERVICENAME) <br />
<br />
5. 查看所有数据库名称及大小 <br />
sp_helpdb <br />
重命名数据库用的SQL <br />
sp_renamedb 'old_dbname', 'new_dbname' <br />
<br />
6. 查看所有数据库用户登录信息 <br />
sp_helplogins <br />
查看所有数据库用户所属的角色信息 <br />
sp_helpsrvrolemember <br />
修复迁移服务器时孤立用户时,可以用的fix_orphan_user脚本或者LoneUser过程 <br />
更改某个数据对象的用户属主 <br />
sp_changeobjectowner [@objectname =] 'object', [@newowner =] 'owner' <br />
注意：更改对象名的任一部分都可能破坏脚本和存储过程。 <br />
把一台服务器上的数据库用户登录信息备份出来可以用add_login_to_aserver脚本 <br />
查看某数据库下,对象级用户权限 <br />
sp_helprotect <br />
<br />
7. 查看链接服务器 <br />
sp_helplinkedsrvlogin <br />
查看远端数据库用户登录信息 <br />
sp_helpremotelogin <br />
<br />
8.查看某数据库下某个数据对象的大小 <br />
sp_spaceused @objname <br />
还可以用sp_toptables过程看最大的N(默认为50)个表 <br />
查看某数据库下某个数据对象的索引信息 <br />
sp_helpindex @objname <br />
还可以用SP_NChelpindex过程查看更详细的索引情况 <br />
SP_NChelpindex @objname <br />
clustered索引是把记录按物理顺序排列的，索引占的空间比较少。 <br />
对键值DML操作十分频繁的表我建议用非clustered索引和约束，fillfactor参数都用默认值。 <br />
查看某数据库下某个数据对象的的约束信息 <br />
sp_helpconstraint @objname <br />
<br />
9.查看数据库里所有的存储过程和函数 <br />
use @database_name <br />
sp_stored_procedures <br />
查看存储过程和函数的源代码 <br />
sp_helptext '@procedure_name' <br />
查看包含某个字符串@str的数据对象名称 <br />
select distinct object_name(id) from syscomments where text like '%@str%' <br />
创建加密的存储过程或函数在AS前面加WITH ENCRYPTION参数 <br />
解密加密过的存储过程和函数可以用sp_decrypt过程 <br />
<br />
10.查看数据库里用户和进程的信息 <br />
sp_who <br />
查看SQL Server数据库里的活动用户和进程的信息 <br />
sp_who 'active' <br />
查看SQL Server数据库里的锁的情况 <br />
sp_lock <br />
进程号1--50是SQL Server系统内部用的,进程号大于50的才是用户的连接进程. <br />
spid是进程编号,dbid是数据库编号,objid是数据对象编号 <br />
查看进程正在执行的SQL语句 <br />
dbcc inputbuffer () <br />
推荐大家用经过改进后的sp_who3过程可以直接看到进程运行的SQL语句 <br />
sp_who3 <br />
检查死锁用sp_who_lock过程 <br />
sp_who_lock <br />
<br />
11.查看和收缩数据库日志文件的方法 <br />
查看所有数据库日志文件大小 <br />
dbcc sqlperf(logspace) <br />
如果某些日志文件较大，收缩简单恢复模式数据库日志，收缩后@database_name_log的大小单位为M <br />
backup log @database_name with no_log <br />
dbcc shrinkfile (@database_name_log, 5) <br />
<br />
12.分析SQL Server SQL 语句的方法： <br />
set statistics time {on | off} <br />
set statistics io {on | off} <br />
图形方式显示查询执行计划 <br />
在查询分析器-&gt;查询-&gt;显示估计的评估计划(D)-Ctrl-L 或者点击工具栏里的图形 <br />
文本方式显示查询执行计划 <br />
set showplan_all {on | off} <br />
set showplan_text { on | off } <br />
set statistics profile { on | off } <br />
<br />
13.出现不一致错误时，NT事件查看器里出3624号错误，修复数据库的方法 <br />
先注释掉应用程序里引用的出现不一致性错误的表，然后在备份或其它机器上先恢复然后做修复操作 <br />
alter database [@error_database_name] set single_user <br />
修复出现不一致错误的表 <br />
dbcc checktable('@error_table_name',repair_allow_data_loss) <br />
或者可惜选择修复出现不一致错误的小型数据库名 <br />
dbcc checkdb('@error_database_name',repair_allow_data_loss) <br />
alter database [@error_database_name] set multi_user <br />
CHECKDB 有3个参数： <br />
repair_allow_data_loss 包括对行和页进行分配和取消分配以改正分配错误、结构行或页的错误，以及删除已损坏的文本对象，这些修复可能会导致一些数据丢失。 <br />
修复操作可以在用户事务下完成以允许用户回滚所做的更改。 <br />
如果回滚修复，则数据库仍会含有错误，应该从备份进行恢复。 <br />
如果由于所提供修复等级的缘故遗漏某个错误的修复，则将遗漏任何取决于该修复的修复。 <br />
修复完成后，请备份数据库。 <br />
repai*_**st 进行小的、不耗时的修复操作，如修复非聚集索引中的附加键。 <br />
这些修复可以很快完成，并且不会有丢失数据的危险。 <br />
repair_rebuild 执行由 repai*_**st 完成的所有修复，包括需要较长时间的修复（如重建索引）。 <br />
执行这些修复时不会有丢失数据的危险。 <br />
<br />
<br />
添加、删除、修改使用db.Execute(Sql)命令执行操作 <br />
╔--------------------╗ <br />
☆ 数据记录筛选 ☆ <br />
╚--------------------╝ <br />
注意：单双引号的用法可能有误(没有测式) <br />
<br />
Sql = "Select Distinct 字段名 From 数据表" <br />
Distinct函数，查询数据库存表内不重复的记录 <br />
<br />
Sql = "Select Count(*) From 数据表 where 字段名1&gt;#18:0:0# and 字段名1&lt; #19:00# " <br />
count函数,查询数库表内有多少条记录，&#8220;字段名1&#8221;是指同一字段 <br />
例： <br />
set rs=conn.execute("select count(id) as idnum from news") <br />
response.write rs("idnum") <br />
<br />
sql="select * from 数据表 where 字段名 between 值1 and 值2" <br />
Sql="select * from 数据表 where 字段名 between #2003-8-10# and #2003-8-12#" <br />
在日期类数值为2003-8-10 19:55:08 的字段里查找2003-8-10至2003-8-12的所有记录，而不管是几点几分。 <br />
<br />
select * from tb_name where datetime between #2003-8-10# and #2003-8-12# <br />
字段里面的数据格式为：2003-8-10 19:55:08，通过sql查出2003-8-10至2003-8-12的所有纪录，而不管是几点几分。 <br />
<br />
Sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]" <br />
<br />
Sql="select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]" <br />
模糊查询 <br />
<br />
Sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]" <br />
查找数据库中前10记录 <br />
<br />
Sql="select top n * form 数据表 order by newid()" <br />
随机取出数据库中的若干条记录的方法 <br />
top n，n就是要取出的记录数 <br />
<br />
Sql="select * from 数据表 where 字段名 in ('值1','值2','值3')" <br />
<br />
╔--------------------╗ <br />
☆ 添加数据记录 ☆ <br />
╚--------------------╝ <br />
sql="insert into 数据表 (字段1,字段2,字段3 &#8230;) valuess (值1,值2,值3 &#8230;)" <br />
<br />
sql="insert into 数据表 valuess (值1,值2,值3 &#8230;)" <br />
不指定具体字段名表示将按照数据表中字段的顺序，依次添加 <br />
<br />
sql="insert into 目标数据表 select * from 源数据表" <br />
把源数据表的记录添加到目标数据表 <br />
<br />
╔--------------------╗ <br />
☆ 更新数据记录 ☆ <br />
╚--------------------╝ <br />
Sql="update 数据表 set 字段名=字段值 where 条件表达式" <br />
<br />
Sql="update 数据表 set 字段1=值1,字段2=值2 &#8230;&#8230; 字段n=值n where 条件表达式" <br />
<br />
Sql="update 数据表 set 字段1=值1,字段2=值2 &#8230;&#8230; 字段n=值n " <br />
没有条件则更新整个数据表中的指定字段值 <br />
<br />
╔--------------------╗ <br />
☆ 删除数据记录 ☆ <br />
╚--------------------╝ <br />
Sql="delete from 数据表 where 条件表达式" <br />
<br />
Sql="delete from 数据表" <br />
没有条件将删除数据表中所有记录) <br />
<br />
╔--------------------------╗ <br />
☆ 数据记录统计函数 ☆ <br />
╚--------------------------╝ <br />
AVG(字段名) 得出一个表格栏平均值 <br />
COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计 <br />
MAX(字段名) 取得一个表格栏最大的值 <br />
MIN(字段名) 取得一个表格栏最小的值 <br />
SUM(字段名) 把数据栏的值相加 <br />
<br />
引用以上函数的方法： <br />
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式" <br />
set rs=conn.excute(sql) <br />
用 rs("别名") 获取统的计值，其它函数运用同上。 <br />
<br />
╔-----------------------------╗ <br />
☆ 数据表的建立和删除 ☆ <br />
╚-----------------------------╝ <br />
CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) &#8230;&#8230; ) <br />
例：CREATE TABLE tab01(name varchar(50),datetime default now()) <br />
DROP TABLE 数据表名称 (永久性删除一个数据表) <br />
<br />
╔--------------------------╗ <br />
☆ 记录集对象的方法 ☆ <br />
╚--------------------------╝ <br />
rs.movenext 将记录指针从当前的位置向下移一行 <br />
rs.moveprevious 将记录指针从当前的位置向上移一行 <br />
rs.movefirst 将记录指针移到数据表第一行 <br />
rs.movelast 将记录指针移到数据表最后一行 <br />
rs.absoluteposition=N 将记录指针移到数据表第N行 <br />
rs.absolutepage=N 将记录指针移到第N页的第一行 <br />
rs.pagesize=N 设置每页为N条记录 <br />
rs.pagecount 根据 pagesize 的设置返回总页数 <br />
rs.recordcount 返回记录总数 <br />
rs.bof 返回记录指针是否超出数据表首端，true表示是，false为否 <br />
rs.eof 返回记录指针是否超出数据表末端，true表示是，false为否 <br />
rs.delete 删除当前记录，但记录指针不会向下移动 <br />
rs.addnew 添加记录到数据表末端 <br />
rs.update 更新数据表记录<br />
<br />
14.查询数据库死进程<br />
select * from master..syslogshold</p>
<img src ="http://www.blogjava.net/shanben/aggbug/213244.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-08 10:31 <a href="http://www.blogjava.net/shanben/archive/2008/07/08/213244.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据类型的不匹配可能会导致索引失效</title><link>http://www.blogjava.net/shanben/archive/2008/07/07/213040.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Mon, 07 Jul 2008 07:02:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/07/213040.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/213040.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/07/213040.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/213040.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/213040.html</trackback:ping><description><![CDATA[&nbsp;&nbsp; Sybase和SQL Server在这一点上有所不同，如果条件比较中的数据类型不匹配的话，可能会引起索引失效，导致潜在的Performance问题。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 简单说明如下：<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<div style="border-right: windowtext 0.5pt solid; padding-right: 5.4pt; border-top: windowtext 0.5pt solid; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; border-left: windowtext 0.5pt solid; width: 98%; padding-top: 4px; border-bottom: windowtext 0.5pt solid">
<div><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" /><span style="color: #0000ff">Create</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">Table</span><span style="color: #000000">&nbsp;Test(<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" />c1&nbsp;</span><span style="font-weight: bold; color: #000000">int</span><span style="color: #000000">&nbsp;</span><span style="color: #808080">not</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">null</span><span style="color: #000000">,<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" />c2&nbsp;</span><span style="font-weight: bold; color: #000000">money</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">default</span><span style="color: #000000">&nbsp;</span><span style="font-weight: bold; color: #800000">0</span><span style="color: #000000">,<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" />c3&nbsp;</span><span style="font-weight: bold; color: #000000">varchar</span><span style="color: #000000">(</span><span style="font-weight: bold; color: #800000">20</span><span style="color: #000000">),<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">constraint</span><span style="color: #000000">&nbsp;PK_Test&nbsp;</span><span style="color: #0000ff">primary</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">key</span><span style="color: #000000">(c1))<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">go</span><span style="color: #000000"><br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" /><br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">create</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">index</span><span style="color: #000000">&nbsp;ind_c2_Test&nbsp;</span><span style="color: #0000ff">on</span><span style="color: #000000">&nbsp;Test(c2)<br />
<img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top"  alt="" /></span><span style="color: #0000ff">go</span></div>
</div>
<p>插入一些数据后，我们可以测试如下：<br />
1&gt; set showplan on<br />
2&gt; go<br />
1&gt; declare @var_int int<br />
2&gt; select @var_int=2<br />
3&gt; select * from Test where <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#99;&#49;&#61;&#64;&#118;&#97;&#114;&#95;&#105;&#110;&#116;">c1=@var_int</a><br />
4&gt; go</p>
<p>QUERY PLAN FOR STATEMENT 1 (at line 1).</p>
<p><br />
&nbsp;&nbsp;&nbsp; STEP 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The type of query is DECLARE.</p>
<p><br />
QUERY PLAN FOR STATEMENT 2 (at line 2).</p>
<p><br />
&nbsp;&nbsp;&nbsp; STEP 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The type of query is SELECT.</p>
<p><br />
QUERY PLAN FOR STATEMENT 3 (at line 3).</p>
<p><br />
&nbsp;&nbsp;&nbsp; STEP 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The type of query is SELECT.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM TABLE<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Test<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nested iteration.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using Clustered Index.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Index : PK_Test<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Forward scan.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Positioning by key.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Keys are:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c1&nbsp; ASC<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using I/O Size 2 Kbytes for data pages.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; With LRU Buffer Replacement Strategy for data pages.</p>
<p>(1 row affected)<br />
&nbsp;c1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c3<br />
&nbsp;----------- ------------------------ --------------------<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 129.14 Hellen</p>
<p>(1 row affected)<br />
我们看到，sybase的执行计划会使用clustered index来读取数据。<br />
<br />
下面，采用money类型来进行测试<br />
1&gt; declare @var_money money<br />
2&gt; select @var_money=2<br />
3&gt; select * from Test where <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#99;&#49;&#61;&#64;&#118;&#97;&#114;&#95;&#109;&#111;&#110;&#101;&#121;">c1=@var_money</a><br />
4&gt; go</p>
<p>QUERY PLAN FOR STATEMENT 1 (at line 1).</p>
<p><br />
&nbsp;&nbsp;&nbsp; STEP 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The type of query is DECLARE.</p>
<p><br />
QUERY PLAN FOR STATEMENT 2 (at line 2).</p>
<p><br />
&nbsp;&nbsp;&nbsp; STEP 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The type of query is SELECT.</p>
<p><br />
QUERY PLAN FOR STATEMENT 3 (at line 3).</p>
<p><br />
&nbsp;&nbsp;&nbsp; STEP 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The type of query is SELECT.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM TABLE<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Test<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nested iteration.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Table Scan.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Forward scan.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Positioning at start of table.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using I/O Size 2 Kbytes for data pages.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; With LRU Buffer Replacement Strategy for data pages.</p>
<p>(1 row affected)<br />
&nbsp;c1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c3<br />
&nbsp;----------- ------------------------ --------------------<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 129.14 Hellen</p>
<p>(1 row affected)<br />
<br />
我们可以看到，sybase没有采用索引，而是采用了全表扫描。<br />
<br />
实际上，Sybase并不是类型不一致就一定不会使用索引，而是有一个匹配原则，原则上是只要索引列的类型优先级高于搜索条件的数据类型，就会使用索引。<br />
这个优先级，可以通过查询系统表master.dbo.systypes.<br />
1&gt; select hierarchy,name from master.dbo.systypes<br />
2&gt; order by 1<br />
3&gt; go<br />
&nbsp;hierarchy name<br />
&nbsp;--------- ------------------------------<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 floatn<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2 float<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3 datetimn<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4 datetime<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5 real<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6 numericn<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7 numeric<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8 decimaln<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9 decimal<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10 moneyn<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 11 money<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12 smallmoney<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 13 smalldatetime<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 14 intn<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 15 int<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 16 smallint<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 17 tinyint<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 18 bit<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 19 univarchar<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 20 unichar<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 22 sysname<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 22 varchar<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 22 nvarchar<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 23 char<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 23 nchar<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24 timestamp<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24 varbinary<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 25 binary<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 26 text<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 27 image<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 99 extended type<br />
</p>
<img src ="http://www.blogjava.net/shanben/aggbug/213040.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-07 15:02 <a href="http://www.blogjava.net/shanben/archive/2008/07/07/213040.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大表清理方案</title><link>http://www.blogjava.net/shanben/archive/2008/07/06/212912.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Sun, 06 Jul 2008 13:43:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/06/212912.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/212912.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/06/212912.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/212912.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/212912.html</trackback:ping><description><![CDATA[經過各種測試，現將經驗總結如下：<br />
ASE+SYB12.0&nbsp; &nbsp;一個大表AAA 共一億多條記錄 21G（加索引的大小）<br />
<br />
<!--<!--[quote][i]原帖由 &quot;chuxu&quot; 發表：[/i]<br />
選條件將原表select into到臨時表，刪除原表，rename臨時表，再建索引<br />
應該速度快[/quote]-->--&gt;<br />
方法是最快的，不過要更改server的一些參數，如果相應的參數沒有更改，就不一定了。<br />
1，sp_poolconfig 'default data cache',"7G","16K","2K"<br />
&nbsp; &nbsp;&nbsp;&nbsp;－－－－建16K的7G大小的default cache<br />
&nbsp; &nbsp;&nbsp;&nbsp;select into ，大部分的查詢（包括，全表掃瞄，使用聚集索引的範圍查詢， order by等）使用16K的I/O能獲得更好的性能。<br />
&nbsp; &nbsp; [color=red]增加16K 緩存的大小，能明顯提高"select into"的速度！[/color]<br />
<br />
<br />
2，sp_configure 'max online engines,7<br />
&nbsp; &nbsp;&nbsp;&nbsp;－－－－Adaptive Server 聯機引擎的最大數，不能超過cpu數<br />
&nbsp; &nbsp;sp_configure 'number of worker processes',7<br />
&nbsp; &nbsp; －－－同步運行並行的組合查詢可使用的工作進程的最大數，小於等於「max online"數<br />
&nbsp; &nbsp;sp_configure 'max parallel degree',7<br />
&nbsp; &nbsp; －－－最大並行度，全服務器範圍每個查詢允許使用的工作進程的最大數目，小於等於「worker processes"數<br />
&nbsp; &nbsp;sp_configure 'max scan parallel degree',7<br />
&nbsp; &nbsp; －－基於散列掃瞄的最大並行度，小於等於「worker processes"數<br />
&nbsp; &nbsp;[color=red]以上參數提高並發度[/color]<br />
<br />
3，sp_configure 'number of sort buffers',20000<br />
&nbsp; &nbsp; －－－指定在排序時所用的2K緩衝區的數目。說明了有多少緩衝區可以用來存放輸入表的數據頁。<br />
&nbsp; &nbsp;[color=red]創建一個很大的索引，而SQL Server上其他的操作又很少時，設置該參數可以提高create index 的性能。[/color]<br />
<br />
<br />
調整以上參數後，採用select into到臨時表，drop原表，rename臨時表，建索引是比較快的。<br />
比bcp快！bcp出來就慢，回去更慢！
<img src ="http://www.blogjava.net/shanben/aggbug/212912.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-06 21:43 <a href="http://www.blogjava.net/shanben/archive/2008/07/06/212912.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SYBASE建立索引的原则</title><link>http://www.blogjava.net/shanben/archive/2008/07/05/212764.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Sat, 05 Jul 2008 10:45:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/05/212764.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/212764.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/05/212764.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/212764.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/212764.html</trackback:ping><description><![CDATA[建立Clustered索引原则：<br />
1、Primary key，但必须满足以下条件：A、常用于Where条件，B、插入记录时的字段值是随机的（不能用于顺序增加的字段，否则将增加最后一个数据页的lock等待时间）<br />
2、用于范围查询的字段，如 col1 between 100 and 200; col12&gt;;62 and &lt;70;<br />
3、用于order by的字段<br />
4、不经常改变（update）的字段<br />
5、用于连接条件的字段<br />
其他有关建立索引的原则：<br />
1、如果索引是唯一的，将其定义为Unique<br />
2、如果使用引用表索引（foreign key ... reference），被引用的列必须建立唯一索引<br />
3、如果建立索引的表有频繁的插入（Insert）操作，使用fillfactor减少数据页的分裂、提高并发性能并减少死锁<br />
4、如果在一个只读表上建立索引，将其fillfactor设为100以尽可能压缩数据和索引空间<br />
5、尽可能减少单个索引的长度<br />
6、在满足使用的前提下，尽可能使用小容量的数据类型（如用numeric代替char）<br />
7、数值（numeric）的比较在内部操作上比字符（string）略快<br />
8、变长字符串（varchar）和二进制（binary）类型比定长（fixed-length）类型需要更多的系统负载<br />
9、只要可能，使用定长（fixed-length）、非空（non-null）、短（short）的数据类型字段作为索引<br />
10、连接条件两端的索引必须是兼容的（最好是同一类型），如果需要进行数据转换的话，连接索引将不能发挥作用<br />
建立复合字段索引的原则<br />
1、当条件内不包含复合索引的第一个字段时，有两种可能：A、当所有select的字段均包含在复合索引内时，做所有索引页的遍历查询，B、当select中包含非复合索引字段时，做全表数据页的遍历查询，既此时索引对检索无效。<br />
2、复合索引优点：A、提供索引覆盖（index covering）的机会，但仅限于所有查询（select）字段均包含在索引内，B、当查询条件使用到复合索引的所有字段时，所需要时间少于这些字段建立单独索引的情况，C、复合索引有利于强制多个属性合并后的唯一性限制<br />
3、复合索引缺点：A、需要更多的索引空间，减少每个索引页包含的索引记录数，并增加索引页数目，B、对复合索引中任何一个字段的变化（update或insert）都会更新索引，因此复合索引必须选择不进行变化的字段组成<br />
4、差的复合索引特点：A、复合索引长度接近于记录长度，B、复合索引中只有少量字段用于查询<br />
<table class="tableborder" cellspacing="1" cellpadding="4" width="95%" align="center">
    <!-- <tr><td bgcolor="#f0f3fa"><table cellspacing="0" cellpadding="0" width="100%"><td class="bold">ZealeS</td><td align="right">2004-12-21 12:48</td></tr></table></td></tr>-->
    <tbody>
        <tr>
            <td bgcolor="#def7ff"><img height="18" src="http://www.lslnet.com/images/icon_new.gif" width="29" border="0"  alt="" /><strong>SYBASE建立索引的原则</strong><br />
            <br />
            补充一点：<br />
            <br />
            &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;--创建非聚集索引<br />
            <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 1.满足查询条件的数据不超过20% <br />
            &nbsp; &nbsp; &nbsp; &nbsp; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 2.能实现 index covering<br />
            &nbsp; &nbsp; &nbsp; &nbsp; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 3.用于集函数、连接、group by和order by的列<br />
            &nbsp; &nbsp; &nbsp; &nbsp; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 4.要权衡索引对查询速度的加快与降低修改速度之间的利弊<br />
            <br />
            <br />
            --删除影响性能的索引<br />
            &nbsp; &nbsp; &nbsp; &nbsp; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 如果一个应用在白天执行数据修改，在夜间生成报表，<br />
            &nbsp; &nbsp; &nbsp; &nbsp; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 就需要在早晨删除索引，在晚上再把索引重建起来。<br />
            &nbsp; &nbsp; &nbsp; &nbsp; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 另外许多系统设计者创建许多优化器很少使用的索引，<br />
            &nbsp; &nbsp; &nbsp; &nbsp; <br />
            &nbsp; &nbsp; &nbsp; &nbsp; 可以根据showplan，把没有用的索引删除。</td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/shanben/aggbug/212764.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-05 18:45 <a href="http://www.blogjava.net/shanben/archive/2008/07/05/212764.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sybase性能优化经验谈！</title><link>http://www.blogjava.net/shanben/archive/2008/07/05/212755.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Sat, 05 Jul 2008 09:44:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/05/212755.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/212755.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/05/212755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/212755.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/212755.html</trackback:ping><description><![CDATA[<span style="font-family: Georgia">
<div class="cnt" id="blog_text">
<p>首先，有一句话要认识 ： 80%的性能问题由SQL语句引起。<br />
<br />
经过看 SYBASE 的书，结合从 MSSQL 迁移过来的系统过程 ，发现以下几个问题比较重要：<br />
&nbsp;&nbsp;&nbsp;&nbsp; 经验一、where 条件左边最好不要使用函数，比如 ★★★★★<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select ...&nbsp;&nbsp;&nbsp;&nbsp; where&nbsp;&nbsp;&nbsp;&nbsp; datediff(day,date1,getdate())&gt;;0 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这样即使在 date1 列上建立了索引，也可能不会使用索引，而使用表扫描。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这样的语句要重新规划设计，保证不使用函数也能够实现。通过修改，一个系统过程的运行效率提高大约100倍！此外不要使用诸如like '%ab',不能充分利用索引，而要在%前加字符<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验二、两个比较字段最好使用相同数据类型，而不是兼容数据类型。比如 int 与 numeric（感觉一般不是太明显）★★<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验三、复合索引的非前导列做条件时，基本没有起到索引的作用。★★★★★<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 比如 create index idx_tablename_ab on tablename(a,b) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; update tablename set c = XX where b&gt;;= XXX and ...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在这个语句中，基本上索引没有发挥作用。 导致表扫描引起blocking 甚至运行十几分钟后报告失败。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一定要认真检查 改正措施： 在接口中附加条件 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; update tablename set c = XX where a = XXX&nbsp;&nbsp;&nbsp;&nbsp; and b&gt;;= XXX <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 或者建立索引类似于 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create index idx_tablename_ba on tablename(b,a)&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验四、 多个大表的关联查询，如果性能不好，并且其中一个大表中取的数据比较少，可以考虑将查询分两步执行。★★★★<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 先将一个大表中的少部分数据 select * into #1 from largetable1 where ...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 然后再用 #1 去做关联，效果可能会好不少。（前提：生成 #1表应该使用比较好的索引，速度比较快）<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验五、 tempdb 的使用。★★★★★<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最好多用 select into&nbsp;&nbsp;&nbsp;&nbsp; ，这样不记日志 ，尤其是有大量数据的报表时。虽然写起来麻烦，但值得。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create table #tmp1 (......)这样写性能不好。尤其是大量使用时，容易发生tempdb 争用。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验六、 系统级别的参数设置&nbsp;&nbsp;&nbsp;&nbsp; ★★★★<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一定要估计一下，不要使用太多，占用资源&nbsp;&nbsp;&nbsp;&nbsp; ，太少，发生性能问题。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 连接数，索引打开个数、锁个数 等、 当然 ，内存配置不要有明显的问题，比如，procedure cache <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不够 （一般缺省20%，如果觉得太多，可以减少一些）。如果做报表经常使用大数据量读，可以考虑使用 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 16Kdata cache <br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验七、索引的建立。很重要。★★★★★<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; clustered index&nbsp;&nbsp;&nbsp;&nbsp; /nonclustered index 的差异，自己要搞清楚。各适用场合，另外如果<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; clustered index 不允许 重复数，也一定要说明。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 索引设计是以为数据访问快速为原则的，不能 完全（！！） 参照数据逻辑设计的，逻辑设计时的一些东西，<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可能对物理访问不起作用<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验八、统计数据的更新：大约10天进行 update statistics&nbsp;&nbsp;&nbsp;&nbsp; ,sp_recompile table_name（★★★）<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验九、强制索引使用 （★★★★）<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果怀疑有表访问时不是使用索引，而且这些条件字段上建立了合适的索引,可以强制使用<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select * from tableA (index idx_name) where ...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个对一些报表程序可能比较有用。<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 经验十、找一个好的监视工具 ★★★<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 工欲善其事，比先利其器，一点都不错呀。<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我用 DBartisian 5.4 ,监视哪些表被锁定时间长， blocking 等<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 还有 sp_object_status 20:00:00 ， sp_sysmon 20:00:00 等<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 以上是我的一点经验，在不到一个月的时间内，我修改了20个左右的语句和系统过程&nbsp;&nbsp;&nbsp;&nbsp; ，<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 系统性能明显改善，cpu利用 高峰时大约50% 平时 不到30%IO 明显改善。所有月报表能顺利完成 5min 以内。</p>
<p>&nbsp;&nbsp;&nbsp; 经验十一： 综合以上对IN/EXISTS的讨论，我们可以得出一个基本通用的结论：IN适合于外表大而内表小的情况；EXISTS适合于外表小而内表大的情况。<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 另外，系统中确认不使用的中间数据，可以进行转移。这些要看系统的情况哦<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最后祝你好运气。<br />
<br />
以上为个人经验，欢迎批评指正！&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp; 呵呵 写完后忘记一个&nbsp;&nbsp;&nbsp;&nbsp; 一定要注意热点表 ，这是影响并发问题的一个潜在因素。！解决方法： 行锁模式 如果表的行比较小，可以故意增加一些不用的字段<br />
&nbsp;&nbsp;&nbsp;&nbsp; 比如&nbsp;&nbsp;&nbsp;&nbsp; char(200)&nbsp;&nbsp;&nbsp;&nbsp; 让一页中存放的行不要太多。</p>
</div>
</span>
<img src ="http://www.blogjava.net/shanben/aggbug/212755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-05 17:44 <a href="http://www.blogjava.net/shanben/archive/2008/07/05/212755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sybase数据库的日常维护 </title><link>http://www.blogjava.net/shanben/archive/2008/07/05/212751.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Sat, 05 Jul 2008 09:19:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/05/212751.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/212751.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/05/212751.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/212751.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/212751.html</trackback:ping><description><![CDATA[<div class="itemText">
<p>维护目的：<br />
<br />
监测数据库的当前运行状况，保证数据库稳定运行。<br />
<br />
做好数据库的日常的备份工作，减轻问题发生时的风险和责任<br />
<br />
检测数据库的整体运行状况，对数据库的性能进行调整，保证数据库高效的运行。<br />
<br />
可以减少紧急故障发生频率，减少对系统的影响。<br />
<br />
尽早发现系统存在的潜在问题，使可能的故障消除在萌芽状态。</p>
<p>数据库日常维护的主要内容<br />
监测数据库运行情况<br />
<br />
监控CPU和I/O的使用情况<br />
<br />
监控空间的使用情况<br />
<br />
监控数据库的错误日志errorlog<br />
<br />
制定一个合理的备份计划<br />
<br />
检查数据库的一致性<br />
<br />
数据库的排错<br />
<br />
数据库性能调整<br />
<br />
数据库日常维护的方法<br />
<br />
数据库服务和备份服务的启动和关闭方法<br />
<br />
数据库和备份服务的启动<br />
<br />
$cd&nbsp; $SYBASE/ASE-12_*/install<br />
<br />
$startserver &#8211;f RUN_Servername(Servername为你的数据库服务名)<br />
<br />
$startserver &#8211;f RUN_Servername_back<br />
<br />
数据库和备份服务的关闭<br />
<br />
isql &#8211;Usa &#8211;Pxxx &#8211;Sservername<br />
<br />
&gt;shutdown SYB_BACKUP (关闭备份服务)<br />
<br />
&gt;go<br />
<br />
&gt;use dbname (用户库)<br />
<br />
&gt;go<br />
<br />
&gt;checkpoint<br />
<br />
&gt;go<br />
<br />
&gt;shutdown<br />
<br />
&gt;go<br />
<br />
查看sybase用户的运行环境是否正常<br />
<br />
$env <br />
<br />
查看SYBASE,SYBASE_ASE,SYBASE_OCS等环境是否正常<br />
<br />
查看数据库服务和备份服务进程是否运行正常<br />
<br />
$ps &#8211;ef | grep&nbsp; dataserver<br />
<br />
$ps &#8211;ef | grep&nbsp; backupserver<br />
<br />
查看数据库的版本和补丁信息<br />
<br />
$dataserver &#8211;v<br />
<br />
或是<br />
<br />
isql &#8211;Usa &#8211;Pxxx &#8211;Sservername<br />
<br />
&gt;select&nbsp; @@version<br />
<br />
检查数据库的配置是否合理.<br />
<br />
检查数据库内存分配、锁个数、存储过程缓冲、多CPU配置、用户连接配置、网络包尺寸等重要参数的设置。<br />
<br />
命令：<br />
<br />
sp_configure &#8216;allocate max shared memory&#8217;<br />
<br />
sp_configure &#8216;max memory&#8216;<br />
<br />
sp_configure &#8216;procedure cache size&#8216;<br />
<br />
sp_configure &#8216;user log cache size&#8217;<br />
<br />
sp_configure &#8216;default network packet size&#8217;<br />
<br />
sp_cacheconfig<br />
<br />
sp_configure &#8216;number of user connections&#8216;<br />
<br />
sp_configure &#8216;number of locks&#8216;<br />
<br />
sp_configure &#8216;number of engines at startup&#8217;<br />
<br />
sp_configure &#8216;max online engines&#8217;<br />
<br />
sp_configure &#8220;number of open index&#8221;<br />
<br />
sp_configure &#8220;number of open objects&#8221;<br />
<br />
sp_configure &#8220;number of open partitions&#8221;<br />
<br />
查看数据库的用户，数据库的锁状况<br />
<br />
sp_who(用户)<br />
<br />
sp_lock(锁)<br />
<br />
select spid,blocked from sysprocesses where blocked&gt;0(检查阻塞进程)<br />
<br />
查看最早进程，发现不完整事务<br />
<br />
select *&nbsp; from master..syslogshold<br />
<br />
其中对应的spid为最早的进程，如果最早的进程开始时间距离当前时间已经非常长(如2、3天)，则可能该进程有问题，确认后，应该将其kill，下面有一个存储过程来检测不完整的事务，可以将该存储过程放在操作系统的crontab中。</p>
<p>create proc proc_kill<br />
@last_min int=720<br />
<br />
AS <br />
<br />
/*<br />
<br />
** kill the processes that didn&#8217;t commit for a impossible time<br />
<br />
** the parameter's time unit is minute,default is 12 hours<br />
<br />
** the procedure is just for user transactions ,not&nbsp; for xa transaction<br />
<br />
** you can add this procedure to your opration system crontab<br />
<br />
*/<br />
<br />
declare @spid&nbsp;&nbsp;&nbsp;&nbsp; int<br />
<br />
declare @cspid&nbsp;&nbsp;&nbsp;&nbsp; char(20)<br />
<br />
select spid into #killtab from master..syslogshold where datediff(mi,starttime,getdate()) &gt;@last_min and spid&gt;0<br />
<br />
declare t1_cur cursor for select * from #killtab <br />
<br />
open t1_cur<br />
<br />
fetch t1_cur into @spid <br />
<br />
while @@sqlstatus!=2<br />
<br />
begin<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp; select @cspid=convert(char(20),@spid)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp; select "kill "<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#43;&#64;&#99;&#115;&#112;&#105;&#100;" shape="" type="" coords="">+@cspid</a><br />
exec("kill "<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#43;&#64;&#99;&#115;&#112;&#105;&#100;" shape="" type="" coords="">+@cspid</a>)<br />
&nbsp;&nbsp;&nbsp;&nbsp; fetch t1_cur into @spid<br />
<br />
end<br />
<br />
查看CPU和I/O的使用<br />
<br />
#sar #iostat<br />
<br />
#vmstat<br />
<br />
查看Sybase的资源使用<br />
<br />
sp_sysmon &#8220;00:10:00&#8221;<br />
<br />
如果CPU或是I/O的使用率长期高于80%，则要看系统使用是否正常还是系统资源配置不够，具体细节可参考下面数据库的优化部分。<br />
<br />
定期检查数据库日志使用的情况，定期清除数据库的日志<br />
<br />
如果数据库日志满，则业务无法进行。<br />
<br />
sp_helpdb dbname(用户库名)<br />
<br />
发现数据库的日志空间不足，应立刻清除已经完成的事务，或是根据需要扩大日志空间。<br />
<br />
定期清除日志：<br />
<br />
dump tran dbname with&nbsp; truncate_only<br />
<br />
可以将上述的命令放在crontab中。<br />
<br />
定期检查磁盘空间的使用。 如果发现操作系统有磁盘分区使用达到100%，应尽快处理。<br />
<br />
#df -k<br />
<br />
定期检查数据库的错误日志(errorlog)，如果错误日志中发现有数据库的严重错误，应立即处理。<br />
<br />
有计划的截断数据库的错误日志。<br />
<br />
数据库运行一定时间后，可以在数据库没有业务时，（下班或周末），将数据库stop后，将错误日志更名，然后重新启动数据库，产生一个新的数据库错误日志。</p>
<p>在错误日志中,以下的错误级别为不严重错误,通常是用户错误,如语法,读写权限空间不足等。<br />
10&nbsp;Status information<br />
<br />
11 &nbsp;Specified database object not found<br />
<br />
12&nbsp;Wrong datatype encountered<br />
<br />
13&nbsp;User transaction syntax error<br />
<br />
14&nbsp;Insufficient permissions to execute commands<br />
<br />
15&nbsp;Syntax error in SQL statement<br />
<br />
16&nbsp;Miscellaneous user error<br />
<br />
&nbsp;Hardware/software errors<br />
<br />
17&nbsp;Insufficient resources<br />
<br />
18&nbsp;Non-fatal internal error<br />
<br />
在错误级别17中,要注意错误号1105,1105表示空间不足(数据库数据或是日志)。<br />
<br />
在错误级别18中,最多的是1608,1608表示一个客户连接不是正常方式退出的，<br />
<br />
这类错误不用关注。</p>
<p>以下错误级别为严重错误,通常是系统(数据库、磁盘损坏、操作系统)错误<br />
19&nbsp;Fatal error in resource<br />
<br />
20&nbsp;Fatal error in current process<br />
<br />
21&nbsp;Fatal error in database process<br />
<br />
22&nbsp;Fatal error: table integrity suspect<br />
<br />
23&nbsp;Fatal error: database integrity suspect<br />
<br />
24&nbsp;Hardware error or system table corruption<br />
<br />
在错误日志中，可以按照"Sybase Technical Support"来搜索，<br />
<br />
找到的错误通常是严重错误。</p>
<p>检查数据库的一致性<br />
检测系统数据库、用户数据库的数据分配页物理可用性，检查数据库系统表和用户表的数据完整性，查看是否有表损坏（可能是逻辑也可能是物理的损坏，如磁盘错误可能导致数据库的表损坏）<br />
<br />
做数据库的一致性通常需要单用户状态，并且都是一些耗时操作（如果数据库大的话），因此，这些检查可以考虑在系统检修时运行。<br />
<br />
dbcc checkalloc(dbname)检查数据库的空间分配，必须在单用户下执行<br />
<br />
checkcatalog(dbname) 检查系统表的一致性<br />
<br />
dbcc checkdb(dbname) 检查用户数据库的数据库一致性(包括系统表和用户表)<br />
<br />
dbcc checktable(tablename) 检查一张表的数据一致性。</p>
<p>数据库的备份<br />
数据库的备份对于日常的维护来说十分重要，系统管理员一定要注意数据库每天都有成功备份。需要检查备份的介质(磁盘或是磁带)是否正常。<br />
<br />
备份命令:<br />
<br />
dump database&nbsp; to &#8216;/xx/xxx&#8217; （设备名或是磁盘上的文件名）<br />
<br />
用户可以规划一个备份的计划，然后将备份的命令放在crontab 中，让系统自动定时做数据库的备份。</p>
<p>数据库的排错<br />
数据库通常的错误主要是以下几种<br />
<br />
数据库服务无法启动<br />
<br />
解决：查看数据库的错误日志，根据错误日志找到无法启动的原因，原因通常是ip地址，端口不对或是被占用，内存配置过大，或是数据库设备的属性不对，sybase用户没有访问权限。<br />
<br />
数据库不能恢复(recovery)<br />
<br />
解决：查看数据库的错误日志，找到数据库不能恢复的原因，然后做相应的处理。通常是由于系统突然断电或是系统非正常关机。<br />
<br />
用户表不能访问<br />
<br />
解决：查看数据库的错误日志，找到不能访问的原因。通常是由于访问权限或是表损坏， </p>
<p>最常见的数据库重大故障分析<br />
根据我们的经验，除去硬件故障外，造成重大数据库故障都是因为日志满，重启动时异常删除日志导致，<br />
<br />
而日志满主要是一下几个原因造成：<br />
<br />
1）数据库配置不合理<br />
<br />
主要是内存、锁的配置不合理。<br />
<br />
2）存在不完整的事务或是进程<br />
<br />
出现这种情况是由于不完整的事务引起的。引起不完整的事务主要有两个方面的原因：第一是网络质量不佳，如果在客户端向ASE服务端进行事务时，如果网络突然中断，会导致事务的不完整。第二是应用程序存在问题，而在这种情况下，重新启动数据库服务后，数据库的恢复可能是非常缓慢的，主要是看日志的大小和事务的类型，（有时用户为了快速启动，通常会异常清除数据库的日志，就有可能会造成数据库表损坏）所以，在这种情况下千万不要急于重新启动数据库服务。<br />
<br />
如果存在不完整的事务，在该事务之后的所有事务都不能被清除，导致数据库日志满。<br />
<br />
不完整的事务可以从master..syslogshold表中发现，如果其中starttime（最早的事务开始时间）距离当前时间很长，比如一天，一月，则该事务应该是一个不完整的事务，可以将该事务对应的数据库进程kill。</p>
<p>3）出现过大的事务<br />
这类问题完全是应用或是人为造成的问题。例如一次删除一个5000万条记录的表，导致日志满。这种情况下重启动服务，数据库的恢复也将是十分缓慢的，防止出现这类问题，是将大事务转换成许多小事务来执行。在事务之间来删除数据库的日志。例如手工删除日志或是将数据库设置成自动清日志。</p>
<p>数据库的性能优化<br />
查看数据库的配置，看能否有不合理的数据库配置。<br />
<br />
查看方法参考上一节查看数据库的配置。<br />
<br />
更新数据库的统计信息(页锁)<br />
<br />
update statistics tablename<br />
<br />
对行锁表回收空间(表锁)<br />
<br />
reorg rebuild&nbsp; tablename<br />
<br />
做空间回收时需要在系统维护时做<br />
<br />
监测数据库的运行，查看是否阻塞缩<br />
<br />
sp_lock<br />
<br />
sp_who<br />
<br />
select spid,bloced from master..sysprocesses where blocked&gt;0<br />
<br />
对数据库系统运行进行监测，发现可能引起性能差的因素<br />
<br />
在系统运行忙或是性能不佳时运行<br />
<br />
sp_sysmon &#8220;00:10:00&#8221;</p>
<p><br />
分析的一些常用工具：</p>
<p>在应用开发或是执行之前，对可能引起问题的语句检查命令：</p>
<p>set showplan on<br />
set noexec on</p>
<p>这样，可以看到该语句的执行过程，而该语句并不执行。</p>
<p><br />
set showplan off<br />
<br />
set noexec off</p>
<p>off 以上的选项</p>
<p>set statistics io on /off<br />
set statistics time on/off</p>
<p>在语句执行时可以看到I/O的实际情况（包括物理I/O,逻辑I/O)</p>
<p>以上语句基于session</p>
<p>在运行过程中查找可能有问题的语句的命令：</p>
<p>运行几次 sp_lock<br />
查找其中对表意向锁 (sh_intent,ex_intent)较长时间的进程，记录下spid<br />
<br />
( 比如，update 一张100万条记录中的一条，如果表不使用索引，最终会有一个Ex_row锁，但在表扫描期间，一直会有一个<br />
<br />
Ex_intent意向锁）<br />
<br />
通过spid,可以看到执行命令和执行过程：<br />
<br />
dbcc traceon(3604)<br />
<br />
go<br />
<br />
dbcc sqltext(spid)<br />
<br />
go<br />
<br />
sp_showplan spid,null,null,null<br />
<br />
go</p>
<p>查找阻塞进程：<br />
select spid,blocked from master..sysprocesses where blocked&gt;0<br />
<br />
其中blocked 对应阻塞别人的进行，spid对应被阻塞经常，<br />
<br />
查看blocked对应进程执行的命令和执行过程<br />
<br />
方法同上</p>
<p>查找最耗资源的进程：下面有两个存储过程，可以分析在数据库繁忙时最消耗cpu和i/o资源的数据库进程，并显示该进程所执行的语句以及执行的过程，根据执行过程来判断问题的原因。</p>
<p>if exists( select name from&nbsp; sysobjects where&nbsp; type='P' and name='proc_who_do_io')<br />
&nbsp;drop proc&nbsp; proc_who_do_io<br />
<br />
go</p>
<p>/* print&nbsp; top n (of&nbsp; physical_io usages)&nbsp; applications 's&nbsp; execute plan and sql */<br />
/* example useage : proc_who_do_io&nbsp; */&nbsp; <br />
<br />
create proc&nbsp; proc_who_do_io<br />
<br />
@inter_time&nbsp;&nbsp;&nbsp; char(8)='00:00:05',<br />
<br />
@topn&nbsp;&nbsp;int=3<br />
<br />
as<br />
<br />
declare&nbsp; @spid &nbsp;int<br />
<br />
select @spid=11<br />
<br />
dbcc traceon(3604)<br />
<br />
select&nbsp; spid,cmd,physical_io,hostname,program_name into #t1 from&nbsp; master..sysprocesses order<br />
<br />
by spid<br />
<br />
waitfor delay @inter_time<br />
<br />
select spid,cmd,physical_io ,hostname,program_name into #t2 from&nbsp; master..sysprocesses order<br />
<br />
by spid</p>
<p>select&nbsp; #t1.spid,#t1.cmd,#t1.program_name,#t1.physical_io as phy_io,#t1.hostname,#t2.physical_io - #t1.physical_io as<br />
phy_io_add&nbsp; into #t3 from #t1,#t2<br />
<br />
where&nbsp; #t1.spid=#t2.spid&nbsp; and abs(#t2.physical_io - #t1.physical_io) &gt;2 order by&nbsp; #t2.physical_io - #t1.physical_io&nbsp; desc<br />
<br />
select *&nbsp; from #t3<br />
<br />
select&nbsp; *&nbsp; into #t4&nbsp; from #t3 where 1=2<br />
<br />
set rowcount 1<br />
<br />
while&nbsp; @topn &gt;0<br />
<br />
begin <br />
<br />
&nbsp;insert&nbsp; #t4 select * from #t3<br />
<br />
&nbsp;delete #t3<br />
<br />
&nbsp;select @spid=spid from&nbsp; #t4<br />
<br />
&nbsp;select "execute plan :"+&nbsp; str(@spid)<br />
<br />
&nbsp;exec sp_showplan @spid,null,null,null<br />
<br />
&nbsp;dbcc sqltext(@spid)<br />
<br />
&nbsp;delete #t4<br />
<br />
&nbsp;select @topn = @topn-1<br />
<br />
end<br />
<br />
set rowcount 0<br />
<br />
go</p>
<p>&nbsp;</p>
<p>if exists( select name from&nbsp; sysobjects where&nbsp; type='P' and name='proc_who_use_cpu')<br />
&nbsp;drop proc&nbsp; proc_who_use_cpu<br />
<br />
go</p>
<p>/* print&nbsp; top n (of&nbsp; cpu usages)&nbsp; applications 's&nbsp; execute plan and sql */<br />
/* example useage : proc_who_use_cpu&nbsp; */&nbsp; <br />
<br />
create proc&nbsp; proc_who_use_cpu<br />
<br />
@inter_time&nbsp;&nbsp;&nbsp; char(8)='00:00:05',<br />
<br />
@topn&nbsp;&nbsp;int=3<br />
<br />
as<br />
<br />
declare&nbsp; @spid &nbsp;int<br />
<br />
select @spid=11<br />
<br />
dbcc traceon(3604)<br />
<br />
select&nbsp; spid,cmd,cpu,hostname,program_name into #t1 from&nbsp; master..sysprocesses order<br />
<br />
by spid<br />
<br />
waitfor delay @inter_time<br />
<br />
select spid,cmd,cpu ,hostname,program_name into #t2 from&nbsp; master..sysprocesses order<br />
<br />
by spid</p>
<p>select&nbsp; #t1.spid,#t1.cmd,#t1.program_name,#t1.cpu ,#t1.hostname,#t2.cpu - #t1.cpu as<br />
cpu_add&nbsp; into #t3 from #t1,#t2<br />
<br />
where&nbsp; #t1.spid=#t2.spid&nbsp; and abs(#t2.cpu - #t1.cpu) &gt;2 order by&nbsp; #t2.cpu - #t1.cpu&nbsp; desc<br />
<br />
select *&nbsp; from #t3<br />
<br />
select&nbsp; *&nbsp; into #t4&nbsp; from #t3 where 1=2<br />
<br />
set rowcount 1<br />
<br />
while&nbsp; @topn &gt;0<br />
<br />
begin <br />
<br />
&nbsp;insert&nbsp; #t4 select * from #t3<br />
<br />
&nbsp;delete #t3<br />
<br />
&nbsp;select @spid=spid from&nbsp; #t4<br />
<br />
&nbsp;select "execute plan :"+&nbsp; str(@spid)<br />
<br />
&nbsp;exec sp_showplan @spid,null,null,null<br />
<br />
&nbsp;dbcc sqltext(@spid)<br />
<br />
&nbsp;delete #t4<br />
<br />
&nbsp;select @topn = @topn-1<br />
<br />
end<br />
<br />
set rowcount 0<br />
<br />
go</p>
<p>数据库版本在 ASE12.5.0.3以上， 寻找索引使用情况<br />
select s.SPID,s.CpuTime,t.LineNumber,t.SQLText <br />
<br />
from monProcessStatement s,monProcessSQLText t<br />
<br />
where s.SPID=t.SPID<br />
<br />
order by s.CpuTime,s.SPID,t.LineNumber desc</p>
</div>
<img src ="http://www.blogjava.net/shanben/aggbug/212751.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-05 17:19 <a href="http://www.blogjava.net/shanben/archive/2008/07/05/212751.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>查看近段时间来对哪些表、索引的操作比较多的存储过程</title><link>http://www.blogjava.net/shanben/archive/2008/07/05/212747.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Sat, 05 Jul 2008 08:29:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/05/212747.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/212747.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/05/212747.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/212747.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/212747.html</trackback:ping><description><![CDATA[print &nbsp; &nbsp; &nbsp; &nbsp; 'sp_hotobjects' &nbsp; <br />
&nbsp; SETUSER &nbsp; &nbsp; 'dbo' &nbsp; <br />
&nbsp; go &nbsp; <br />
&nbsp; use &nbsp; sybsystemprocs &nbsp; <br />
&nbsp; go &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; drop &nbsp; proc &nbsp; sp_hotobjects &nbsp; <br />
&nbsp; go &nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; create &nbsp; procedure &nbsp; sp_hotobjects &nbsp; <br />
&nbsp; @interval &nbsp; char(12) &nbsp; = &nbsp; "", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* &nbsp; time &nbsp; interval &nbsp; string &nbsp; */ &nbsp; <br />
&nbsp; @interval_sample &nbsp; char(12) &nbsp; = &nbsp; "00:05:00" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* &nbsp; sample &nbsp; interval &nbsp; every &nbsp; 5 &nbsp; minutes &nbsp; by &nbsp; default &nbsp; */ &nbsp; <br />
&nbsp; as &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; declare &nbsp; @TmpTime &nbsp; datetime &nbsp; &nbsp; &nbsp; /* &nbsp; temporary &nbsp; datetime &nbsp; variable &nbsp; */ &nbsp; <br />
&nbsp; declare &nbsp; @Seconds &nbsp; int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* &nbsp; Interval &nbsp; converted &nbsp; to &nbsp; seconds &nbsp; */ &nbsp; <br />
&nbsp; declare &nbsp; @Endtime &nbsp; datetime &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; create &nbsp; table &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; (dbname &nbsp; char(30) &nbsp; not &nbsp; null, &nbsp; <br />
&nbsp; objname &nbsp; char(30) &nbsp; not &nbsp; null, &nbsp; <br />
&nbsp; lockcount &nbsp; int &nbsp; null, &nbsp; <br />
&nbsp; locktable &nbsp; int &nbsp; null, &nbsp; &nbsp; &nbsp; <br />
&nbsp; lockshared &nbsp; int &nbsp; null, &nbsp; <br />
&nbsp; lockexclusive &nbsp; int &nbsp; null, &nbsp; <br />
&nbsp; lockrow &nbsp; int &nbsp; null, &nbsp; <br />
&nbsp; lockpage &nbsp; int &nbsp; null, &nbsp; <br />
&nbsp; lockblk &nbsp; int &nbsp; null &nbsp; <br />
&nbsp; ) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; create &nbsp; table &nbsp; #hotobjects &nbsp; <br />
&nbsp; (dbname &nbsp; char(30) &nbsp; not &nbsp; null, &nbsp; <br />
&nbsp; objname &nbsp; char(30) &nbsp; not &nbsp; null, &nbsp; <br />
&nbsp; lockcount &nbsp; int &nbsp; null, &nbsp; <br />
&nbsp; locktype &nbsp; int &nbsp; not &nbsp; null, &nbsp; <br />
&nbsp; primary &nbsp; key(dbname, &nbsp; objname, &nbsp; locktype)) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; /* &nbsp; loop &nbsp; for &nbsp; the &nbsp; interval &nbsp; specified &nbsp; */ &nbsp; <br />
&nbsp; select &nbsp; @TmpTime &nbsp; = &nbsp; convert(datetime, &nbsp; @interval) &nbsp; <br />
&nbsp; select &nbsp; @Seconds &nbsp; = &nbsp; datepart(hour,@TmpTime)*3600+datepart(minute,@TmpTime)*60+datepart(second,@TmpTime) &nbsp; <br />
&nbsp; select &nbsp; @Endtime &nbsp; = &nbsp; dateadd(second, &nbsp; @Seconds, &nbsp; getdate()) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; /* &nbsp; create &nbsp; a &nbsp; holding &nbsp; table &nbsp; */ &nbsp; <br />
&nbsp; select &nbsp; dbname, &nbsp; objname &nbsp; into &nbsp; #hotobjects_holding &nbsp; &nbsp; <br />
&nbsp; from &nbsp; #hotobjects &nbsp; where &nbsp; 1=2 &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; while &nbsp; (getdate() &nbsp; &lt; &nbsp; @Endtime) &nbsp; <br />
&nbsp; begin &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; /* &nbsp; populate &nbsp; the &nbsp; initial &nbsp; records &nbsp; */ &nbsp; <br />
&nbsp; delete &nbsp; from &nbsp; #hotobjects &nbsp; <br />
&nbsp; insert &nbsp; into &nbsp; #hotobjects(dbname, &nbsp; objname, &nbsp; lockcount, &nbsp; locktype) &nbsp; <br />
&nbsp; select &nbsp; distinct &nbsp; db_name(dbid), &nbsp; object_name(id,dbid), &nbsp; count(type), &nbsp; type &nbsp; from &nbsp; master..syslocks &nbsp; &nbsp; <br />
&nbsp; where &nbsp; object_name(id,dbid) &nbsp; not &nbsp; like &nbsp; "#%" &nbsp; &nbsp; &nbsp; -- &nbsp; and &nbsp; object_name(id,dbid) &nbsp; not &nbsp; like &nbsp; "sys%" &nbsp; &nbsp; <br />
&nbsp; and &nbsp; object_name(id,dbid) &nbsp; not &nbsp; like &nbsp; "hot%" &nbsp; &nbsp; group &nbsp; by &nbsp; type &nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; waitfor &nbsp; delay &nbsp; @interval_sample &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; /* &nbsp; add &nbsp; a &nbsp; record &nbsp; into &nbsp; #hotobjects_totals &nbsp; if &nbsp; it &nbsp; does &nbsp; not &nbsp; exist &nbsp; */ &nbsp; <br />
&nbsp; if &nbsp; not &nbsp; exists(select &nbsp; 1 &nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname) &nbsp; &nbsp; <br />
&nbsp; /* &nbsp; add &nbsp; this &nbsp; new &nbsp; lock &nbsp; record &nbsp; */ &nbsp; <br />
&nbsp; begin &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; /* &nbsp; populate &nbsp; a &nbsp; holding &nbsp; table &nbsp; */ &nbsp; <br />
&nbsp; delete &nbsp; from &nbsp; #hotobjects_holding &nbsp; <br />
&nbsp; insert &nbsp; into &nbsp; #hotobjects_holding &nbsp; <br />
&nbsp; select &nbsp; distinct &nbsp; dbname, &nbsp; objname &nbsp; from &nbsp; #hotobjects &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; /* &nbsp; now &nbsp; delete &nbsp; from &nbsp; the &nbsp; holding &nbsp; table &nbsp; all &nbsp; records &nbsp; we &nbsp; have &nbsp; done &nbsp; before &nbsp; */ &nbsp; <br />
&nbsp; delete &nbsp; from &nbsp; #hotobjects_holding &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_holding &nbsp; HOLD, &nbsp; #hotobjects_totals &nbsp; TOT &nbsp; <br />
&nbsp; where &nbsp; HOLD.dbname &nbsp; = &nbsp; TOT.dbname &nbsp; and &nbsp; HOLD.objname &nbsp; = &nbsp; TOT.objname &nbsp; <br />
&nbsp; /* &nbsp; what &nbsp; is &nbsp; left &nbsp; is &nbsp; the &nbsp; new &nbsp; records.....add &nbsp; these &nbsp; into &nbsp; the &nbsp; totals &nbsp; table &nbsp; */ &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; insert &nbsp; into &nbsp; #hotobjects_totals(dbname, &nbsp; objname, &nbsp; &nbsp; lockcount, &nbsp; locktable, &nbsp; lockshared, &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lockexclusive, &nbsp; lockrow, &nbsp; lockpage, &nbsp; lockblk) &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select &nbsp; distinct &nbsp; HOLD.dbname, &nbsp; HOLD.objname, &nbsp; 0, &nbsp; 0,0,0,0,0,0 &nbsp; &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_holding &nbsp; HOLD &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; end &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; /* &nbsp; from &nbsp; here &nbsp; on &nbsp; we &nbsp; will &nbsp; update &nbsp; this &nbsp; record &nbsp; &nbsp; */ &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; update &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; set &nbsp; lockcount &nbsp; = &nbsp; TOT.lockcount &nbsp; + &nbsp; HOT.lockcount &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; update &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; set &nbsp; locktable &nbsp; = &nbsp; locktable &nbsp; + &nbsp; HOT.lockcount &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname &nbsp; <br />
&nbsp; and &nbsp; HOT.locktype &nbsp; in &nbsp; (1,2) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; update &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; set &nbsp; lockshared &nbsp; = &nbsp; lockshared &nbsp; + &nbsp; HOT.lockcount &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname &nbsp; <br />
&nbsp; and &nbsp; HOT.locktype &nbsp; in &nbsp; (2,6,9) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; update &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; set &nbsp; lockexclusive &nbsp; = &nbsp; lockexclusive &nbsp; + &nbsp; HOT.lockcount &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname &nbsp; <br />
&nbsp; and &nbsp; HOT.locktype &nbsp; in &nbsp; (1,5,8) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; update &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; set &nbsp; lockrow &nbsp; = &nbsp; lockrow &nbsp; + &nbsp; HOT.lockcount &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname &nbsp; <br />
&nbsp; and &nbsp; HOT.locktype &nbsp; in &nbsp; (8,9,10) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; update &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; set &nbsp; lockpage &nbsp; = &nbsp; lockpage &nbsp; + &nbsp; HOT.lockcount &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname &nbsp; <br />
&nbsp; and &nbsp; HOT.locktype &nbsp; in &nbsp; (5,6,7) &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; update &nbsp; #hotobjects_totals &nbsp; <br />
&nbsp; set &nbsp; lockblk &nbsp; = &nbsp; lockblk &nbsp; + &nbsp; HOT.lockcount &nbsp; <br />
&nbsp; from &nbsp; #hotobjects_totals &nbsp; TOT, &nbsp; #hotobjects &nbsp; HOT &nbsp; <br />
&nbsp; where &nbsp; TOT.dbname &nbsp; = &nbsp; HOT.dbname &nbsp; and &nbsp; TOT.objname &nbsp; = &nbsp; HOT.objname &nbsp; <br />
&nbsp; and &nbsp; HOT.locktype &nbsp; &gt; &nbsp; 255 &nbsp; and &nbsp; HOT.locktype &nbsp; &lt; &nbsp; 269 &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; end &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; select &nbsp; "In &nbsp; " &nbsp; + &nbsp; rtrim(dbname) &nbsp; + &nbsp; "the &nbsp; table &nbsp; " &nbsp; + &nbsp; rtrim(objname) &nbsp; + &nbsp; " &nbsp; had &nbsp; " &nbsp; + &nbsp; &nbsp; <br />
&nbsp; case &nbsp; &nbsp; <br />
&nbsp; when &nbsp; locktable &nbsp; &gt; &nbsp; 1 &nbsp; then &nbsp; "table &nbsp; level, &nbsp; " &nbsp; <br />
&nbsp; when &nbsp; lockshared &nbsp; &gt; &nbsp; 1 &nbsp; then &nbsp; "shared, &nbsp; " &nbsp; &nbsp; <br />
&nbsp; when &nbsp; lockexclusive &nbsp; &gt; &nbsp; 1 &nbsp; then &nbsp; "exclusive, &nbsp; " &nbsp; <br />
&nbsp; when &nbsp; lockrow &nbsp; &gt; &nbsp; 1 &nbsp; then &nbsp; "row, &nbsp; " &nbsp; <br />
&nbsp; when &nbsp; lockpage &nbsp; &gt; &nbsp; 1 &nbsp; then &nbsp; "page, &nbsp; " &nbsp; <br />
&nbsp; when &nbsp; lockblk &nbsp; &gt; &nbsp; 1 &nbsp; then &nbsp; "blocking &nbsp; " &nbsp; <br />
&nbsp; end &nbsp; <br />
&nbsp; + &nbsp; " &nbsp; locks" &nbsp; <br />
&nbsp; &nbsp; from &nbsp; #hotobjects_totals &nbsp; &nbsp; <br />
&nbsp; return &nbsp; (0) &nbsp; <br />
&nbsp; go &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; grant &nbsp; exec &nbsp; on &nbsp; sp_hotobjects &nbsp; to &nbsp; public &nbsp; <br />
&nbsp; go&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; <br />
<img src ="http://www.blogjava.net/shanben/aggbug/212747.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-05 16:29 <a href="http://www.blogjava.net/shanben/archive/2008/07/05/212747.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Sybase数据库死锁对策</title><link>http://www.blogjava.net/shanben/archive/2008/07/02/212199.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Wed, 02 Jul 2008 12:58:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/02/212199.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/212199.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/02/212199.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/212199.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/212199.html</trackback:ping><description><![CDATA[<p>死锁的发生对系统的性能和吞吐量都有重要影响，经检测发现，管理信息系统的死锁主要是因为两个或多个线程（登录）抢占同一表数据资源。引起长时间抢占同一资源不是因为我们需要处理的事务太复杂，时间太长，而往往是因为我们在前端应用程序对数据库作操作时忘了提交。本文介绍一种处理解决这种死锁的方法。
<p><strong>Sybase封锁原理</strong></p>
<p>数据共享与数据一致性是一对不可调和的矛盾，为了达到数据共享与数据一致，必须进行并发控制。并发控制的任务就是为了避免共享冲突而引起的数据不一致。Sybase SQL Server并发控制的方法是加锁机制（LOCKING）.</p>
<p><strong>锁的类型</strong></p>
<table cellspacing="1" cellpadding="5" align="center" bgcolor="#000000" border="1">
    <tbody>
        <tr bgcolor="#cccccc">
            <td>可申请的锁<br />
            已有的锁</td>
            <td>S</td>
            <td>U</td>
            <td>X</td>
        </tr>
        <tr bgcolor="#cccccc">
            <td>S</td>
            <td>∨</td>
            <td>∨</td>
            <td>&#215;</td>
        </tr>
        <tr bgcolor="#cccccc">
            <td>U</td>
            <td>∨</td>
            <td>&#215;</td>
            <td>&#215;</td>
        </tr>
        <tr bgcolor="#cccccc">
            <td>X</td>
            <td>&#215;</td>
            <td>&#215;</td>
            <td>&#215;</td>
        </tr>
    </tbody>
</table>
<p>Sybase SQL Server有三种封锁类型：排它锁（exclusive lock,简称X锁）；共享锁（share lock,简称S锁）；更新锁（update lock,简称U锁）。这三种锁的相容矩阵表如下：</p>
<p>&#215;:表示不兼容。∨：表示兼容。</p>
<p>Sybase SQL Server是自动决定加锁类型的。一般来说，读（SELECT）操作使用S锁，写（UPDATE,INSERT和delete）操作使用X锁。U锁是建立在页级上的，它在一个更新操作开始时获得，当要修改这些页时，U锁会升级为X锁。</p>
<p><strong>锁的力度</strong></p>
<p>SQL Server有两级锁：页锁和表锁。通常页锁比表锁的限制更少（或更小）。页锁对本页的所有行进行锁定，而表锁则锁定整个表。为了减小用户间的数据争用和改进并发性，SQL Server试图尽可能地使用页锁。</p>
<p>当SQL Server决定一个语句将访问整个表或表的大多数页时，它用表锁来提供更有效的锁定。锁定策略直接受查询方案约束，如果update或delete语句没有可用的索引，它就执行表扫描或请求一个表锁定。如果update或delete语句使用了索引，它就通过请求页锁来开始，如果影响到大多数行，它就要请求表锁。一旦一个语句积累的页锁超过锁提升阈值，SQL Server就设法给该对象分配一个表锁。如果成功了，页锁就不再必要了，因此被释放。表锁也在页层提供避免锁冲突的方法。对于有些命令SQL Server自动使用表锁。</p>
<p><strong>锁的状态</strong></p>
<p>SQL SERVER加锁有三种状态：</p>
<p>1）意向锁（intend）—是一种表级锁，它表示在一个数据页上获得一个S或X锁的意向。意向锁可以防止其他事务在该数据页的表上获得排它锁。</p>
<p>2）阻塞（blocking,简记blk）—它表明目前加锁进程的状态，带有blk后缀的锁说明该进程目前正阻塞另一个需要获得锁的进程，只有这一进程完成，其他进程才可以进行。</p>
<p>3）需求锁（demand）—表示此时该进程企图得到一个排它锁。它可以防止在这一表或页上加过多的S锁，她表示某一事务是下一个去锁定该表和该页的事务。</p>
<p>需求锁是一个内部过程，因此用sp_lock是无法看见的。</p>
<p><strong>死锁DEADLOCK</strong></p>
<p>简单地说，有两个用户进程，每个进程都在一个单独的页或表上有一个锁，而且每个进程都想在对方进程的页或表上请求不相容锁时就会发生&#8220;死锁&#8221;。在这种情况下，第一个进程在等待另一进程释放锁，但另一进程要等到第一个进程的对象释放时才会释放自己的锁。</p>
<p>SQL Server检查是否死锁，并终止事务中CPU时间积累最小的用户（即最后进入的用户）。SQL Server回滚该用户的事务，并用消息号1205通知有此死锁行为的应用程序，然后允许其他用户进程继续进行。</p>
<p>在多用户情形下，每个用户的应用程序都应检查每个修改数据的事务是否有1205号消息，以此确定是否有可能死锁。消息号1025表示该用户的事务因死锁而终止并被回滚。应用程序必须重新开始这个事务处理。</p>
<p><strong>查找死锁原因</strong></p>
<p>既然管理信息系统长时间死锁的原因是由于我们提交或者是提交不当，那么我们就可以通过修改程序防止出现死锁。定位死锁出错处主要经过以下三步：</p>
<p>1）在死锁出现时，用SP_WHO,SP_LOCK获得进程与锁的活动情况。<br />
2）结合库表sysobjects和相应的操作员信息表查出被锁的库表与锁住别人的操作员。<br />
3）根据锁定的库表与操作员的岗位，可以估计出程序大约出错处。询问操作员在死锁时执行的具体操作即可完全定位出错处。最后查找程序并修改之。</p>
<p><strong>用sp_who获取关于被阻碍进程的信息</strong></p>
<p>系统过程sp_who给出系统进程的报告。如果用户的命令正被另一进程保持的锁阻碍，则：<br />
◆status列显示&#8220;lock sleep&#8221;。<br />
◆blk列显示保持该锁或这些锁的进程标识，即被谁锁定了。<br />
◆loginame列显示登录操作员。结合相应的操作员信息表，便可知道操作员是谁。<br />
<em>Fid spid status loginame origname blk dbname cmd <br />
0 1 lock sleep lm lm 18 QJYD SELECT <br />
0 2 sleeping NULL NULL 0 master NETWORK HANDLER <br />
0 3 sleeping NULL NULL 0 master NETWORK HANDLER <br />
&#8230;&#8230;</em></p>
<p><strong>用sp_lock浏览锁</strong><br />
要得到关于当前SQL Server上保持的锁的报告，可用系统过程sp_lock [spid1[,spid2]]，spid1,spid2是表master.dbo.sysprocesses中的sql server进程id号，用sp_who可以得到锁定与被锁定的spid号：</p>
<p>◆locktype列显示加锁的类型和封锁的粒度，有些锁的后缀还带有blk表明锁的状态。前缀表明锁的类型：Sh—共享锁，Ex—排它锁或更新锁，中间表明锁死在表上（&#8221;table&#8221;或&#8217;intent&#8217;）还是在页上（page）. 后缀&#8220;blk&#8221;表明该进程正在障碍另一个需要请求锁的进程。一旦正在障碍的进程一结束，其他进程就向前移动。&#8220;demand&#8221;后缀表明当前共享锁一释放， 该进程就申请互斥锁。</p>
<p>◆table_id列显示表的id号，结合sysobjects即可查出被封锁的表名。</p>
<p>执行该进程后屏幕显示</p>
<p><em>Fid Spid locktype table_id page row dbname Class context<br />
0 1 Sh_intent 678293476 0 0 QJYD Non Cursor LockFam dur <br />
0 1 Sh_page 678293476 31764 0 QJYD Non Cursor Lock <br />
0 18 Ex_intent 9767092 0 0 QJYD Non Cursor LockFam dur <br />
&#8230;&#8230;</em></p>
<p><strong>定位出错处</strong></p>
<p>根据sp_who与sp_lock命令的结果，结合sysobjects和相应的操作员信息表。得到操作员及其在死锁时所操作的库表，便大约可以知道应用程序的出错处，再询问操作员在死锁时执行什么操作以进一步认证。最后查找程序并修正之。<br />
<br />
<br />
附:<br />
锁的问题一般来讲是这样的 &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; 首先判断是否存在锁竞争 &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; 1、基于系统管理员的经验找出热点表 &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; 2、通过以下方法判断： &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; sp_who &nbsp; <br />
&nbsp; 察看系统中是否有status为lock &nbsp; sleep的用户。如果存在，再运行命令 &nbsp; <br />
&nbsp; sp_lock &nbsp; <br />
&nbsp; 察看系统中被锁住的表的table_id &nbsp; <br />
&nbsp; 继续运行命令 &nbsp; <br />
&nbsp; select &nbsp; * &nbsp; from &nbsp; sysobjects &nbsp; where &nbsp; id &nbsp; = &nbsp; table_id &nbsp; <br />
&nbsp; 找到被锁住的表名。 &nbsp; <br />
&nbsp; 如果发现sp_who中有多个用户被锁，那么，就可认为是锁造成了数据库系统的性能底下。 &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; 调优方法1：修改对应表的加锁模式由allpages为行级锁，如： &nbsp; <br />
&nbsp; alter &nbsp; table &nbsp; XXXX &nbsp; lock &nbsp; datarows &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; 调优方法2：改大环境参数number &nbsp; of &nbsp; locks，如： &nbsp; <br />
&nbsp; sp_configure &nbsp; &#8220;number &nbsp; of &nbsp; locks&#8221;, &nbsp; 50000 &nbsp; <br />
&nbsp; 注意，在这种情况下需要保持total &nbsp; memory与number &nbsp; of &nbsp; locks的匹配性。 &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; 调优方法3：配置锁提升阈值，可修改的配置包括两项内容： &nbsp; <br />
&nbsp; Page(HWM)、Row(HWM)，命令如下： &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; Sp_configure &nbsp; &#8216;page &nbsp; lock &nbsp; promotion &nbsp; HWM&#8217;, &nbsp; 500 &nbsp; <br />
&nbsp; Sp_configure &nbsp; &#8216;row &nbsp; lock &nbsp; promotion &nbsp; HWM&#8217;, &nbsp; 500&nbsp;&nbsp; <br />
</p>
<img src ="http://www.blogjava.net/shanben/aggbug/212199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-02 20:58 <a href="http://www.blogjava.net/shanben/archive/2008/07/02/212199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sybase数据库中sp_who报告的状态值</title><link>http://www.blogjava.net/shanben/archive/2008/07/02/212091.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Wed, 02 Jul 2008 03:48:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/02/212091.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/212091.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/02/212091.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/212091.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/212091.html</trackback:ping><description><![CDATA[<p>sp_who报告的状态值</p>
<p>状态&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 表示&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; kill命令的效果</p>
<p>recv sleep&nbsp; 等待网络读取&nbsp; 立即<br />
send sleep&nbsp; 等待网络发送&nbsp; 立即<br />
alarm sleep 等待警报&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 立即<br />
lock sleep&nbsp; 等待获取锁&nbsp;&nbsp;&nbsp; 立即<br />
sync sleep&nbsp; 等待同系列另一进程的同步消息&nbsp;&nbsp; 立即.系列中的其它进程也必须变为可注销的状态<br />
sleeping&nbsp;&nbsp;&nbsp; 等待磁盘I/O或某种其它资源.或许表示正在运行的进程,但正在执行大量的磁盘I/O&nbsp; 少数休眠进程不能苏醒,因而需要服务器重启以将其清除.<br />
runnable&nbsp;&nbsp;&nbsp; 在可运行进程队列中&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 立即<br />
running&nbsp;&nbsp;&nbsp;&nbsp; 活跃地运行在一个服务器引擎中&nbsp; 立即<br />
infected&nbsp;&nbsp;&nbsp; 服务器已检测到严重的错误情况;极其少见&nbsp;&nbsp;&nbsp; 不要使用kill命令.可能需要重启服务器以清除进程.<br />
background&nbsp; 由Adaptive Server而不是用户进程运行的进程例如阈值过程&nbsp;&nbsp; 立即;使用kill时必须极其小心.建议在注销背景进程之前,仔细检查sysprocesses<br />
log suspend&nbsp;&nbsp; 到达日志的最后机会阈值时挂起的进程&nbsp;&nbsp; 立即</p>
<p>参考&lt;sybase数据库系统管理指南&gt;</p>
<p><br />
&nbsp;---★ 本文转摘自『onlyhot blog ※ blog.onlyhot.cn』http://www.onlyhot.cn/blog/?action=show&amp;id=1178</p>
<img src ="http://www.blogjava.net/shanben/aggbug/212091.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-02 11:48 <a href="http://www.blogjava.net/shanben/archive/2008/07/02/212091.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>实例讲解了Sybase数据库性能优化的具体过程</title><link>http://www.blogjava.net/shanben/archive/2008/07/01/211952.html</link><dc:creator>虎啸长沙,龙跃深圳.</dc:creator><author>虎啸长沙,龙跃深圳.</author><pubDate>Tue, 01 Jul 2008 08:48:00 GMT</pubDate><guid>http://www.blogjava.net/shanben/archive/2008/07/01/211952.html</guid><wfw:comment>http://www.blogjava.net/shanben/comments/211952.html</wfw:comment><comments>http://www.blogjava.net/shanben/archive/2008/07/01/211952.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shanben/comments/commentRss/211952.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shanben/services/trackbacks/211952.html</trackback:ping><description><![CDATA[用一个实例讲解了Sybase数据库性能优化的具体过程，具体内容请参考下文：
<p style="text-indent: 2em">共享锁
<p style="text-indent: 2em">sp_getapplock 锁定应用程序资源
<p style="text-indent: 2em">
<p style="text-indent: 2em">sp_releaseapplock 为应用程序资源解锁
<p style="text-indent: 2em">
<p style="text-indent: 2em">SET LOCK_TIMEOUT 1800 锁超时期限设置
<p style="text-indent: 2em">
<p style="text-indent: 2em">sp_configure 'deadlock checking period',5000 设置锁检测周期
<p style="text-indent: 2em">sp_configure 'lock wait period',5000 设置锁的等待时间
<p style="text-indent: 2em">sp_setrowlockpromote 设置基本个表的最大行锁升级数(锁数)
<p style="text-indent: 2em">sp_setrowlockpromote 'TABLE',TREECODE,500,500,100
<p style="text-indent: 2em">sp_setrowlockpromote 'TABLE',LCD05,500,500,100
<p style="text-indent: 2em">
<p style="text-indent: 2em">
<p style="text-indent: 2em">[Lock Manager]
<p style="text-indent: 2em">number of locks = 50000 #锁数
<p style="text-indent: 2em">deadlock checking period = DEFAULT
<p style="text-indent: 2em">freelock transfer block size = DEFAULT
<p style="text-indent: 2em">max engine freelocks = DEFAULT
<p style="text-indent: 2em">lock spinlock ratio = DEFAULT
<p style="text-indent: 2em">lock hashtable size = DEFAULT
<p style="text-indent: 2em">lock scheme = DEFAULT
<p style="text-indent: 2em">lock wait period = DEFAULT
<p style="text-indent: 2em">read committed with lock = DEFAULT
<p style="text-indent: 2em">当很多事务同时访问同一个数据库时，会加剧锁资源争夺，严重时事务之间会发生死锁。可用sp_object_stats查明死锁位置。该过程报告资源争夺最激烈的10张表、一个数据库中资源争夺的表和单个表的争夺情况。语法为sp_object_stats interval [, top_n [, dbname [, objname [, rpt_option ]]]]，查看锁争夺情况只需设置interval为&#8220;hh:mm:ss&#8221;。如果显示每种锁的争夺程度超过15%，应该改变加锁方式，比如表的全页锁改成数据页锁，数据页锁改成数据行锁等。
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">allow remote access 1 0 1 1
<p style="text-indent: 2em">print recovery information 0 0 0 0
<p style="text-indent: 2em">recovery interval in minutes 5 0 5 5
<p style="text-indent: 2em">tape retention in days 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">global async prefetch limit 10 0 10 10
<p style="text-indent: 2em">global cache partition number 1 0 1 1
<p style="text-indent: 2em">memory alignment boundary 2048 0 2048 2048
<p style="text-indent: 2em">number of index trips 0 0 0 0
<p style="text-indent: 2em">number of oam trips 0 0 0 0
<p style="text-indent: 2em">procedure cache percent 20 22426 20 20
<p style="text-indent: 2em">total data cache size 0 89698 0 89698
<p style="text-indent: 2em">total memory 47104 196608 98304 98304
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">cis bulk insert batch size 0 0 0 0
<p style="text-indent: 2em">cis connect timeout 0 0 0 0
<p style="text-indent: 2em">cis cursor rows 50 0 50 50
<p style="text-indent: 2em">cis packet size 512 0 512 512
<p style="text-indent: 2em">cis rpc handling 0 0 0 0
<p style="text-indent: 2em">enable cis 1 0 1 1
<p style="text-indent: 2em">max cis remote connections 0 0 0 0
<p style="text-indent: 2em">max cis remote servers 25 19 25 25
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">dtm detach timeout period 0 0 0 0
<p style="text-indent: 2em">dtm lock timeout period 300 0 300 300
<p style="text-indent: 2em">enable xact coordination 1 0 1 1
<p style="text-indent: 2em">number of dtx participants 500 149 500 500
<p style="text-indent: 2em">strict dtm enforcement 0 0 0 0
<p style="text-indent: 2em">txn to pss ratio 16 3692 16 16
<p style="text-indent: 2em">xact coordination interval 60 0 60 60
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">average cap size 200 0 200 200
<p style="text-indent: 2em">caps per ccb 50 0 50 50
<p style="text-indent: 2em">dump on conditions 0 0 0 0
<p style="text-indent: 2em">maximum dump conditions 10 0 10 10
<p style="text-indent: 2em">number of ccbs 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">allow sql server async i/o 1 0 1 1
<p style="text-indent: 2em">disable disk mirroring 0 0 0 0
<p style="text-indent: 2em">disk i/o structures 256 31 256 256
<p style="text-indent: 2em">number of devices 10 #5 10 10
<p style="text-indent: 2em">page utilization percent 95 0 95 95
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">event log computer name LocalSystem 0 LocalSystem LocalSystem
<p style="text-indent: 2em">event logging 1 0 1 1
<p style="text-indent: 2em">log audit logon failure 0 0 0 0
<p style="text-indent: 2em">log audit logon success 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">esp execution priority 8 0 8 8
<p style="text-indent: 2em">esp execution stacksize 77824 0 77824 77824
<p style="text-indent: 2em">esp unload dll 0 0 0 0
<p style="text-indent: 2em">start mail session 0 0 0 0
<p style="text-indent: 2em">xp_cmdshell context 1 0 1 1
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">configuration file 0 0 0 /sybase/hgd
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">enable java 0 0 0 0
<p style="text-indent: 2em">size of global fixed heap 300 0 300 300
<p style="text-indent: 2em">size of process object heap 300 0 300 300
<p style="text-indent: 2em">size of shared class heap 3072 0 3072 3072
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">default character set id 1 0 1 1
<p style="text-indent: 2em">default language id 0 0 0 0
<p style="text-indent: 2em">default sortorder id 50 0 50 50
<p style="text-indent: 2em">disable character set conversi 0 0 0 0
<p style="text-indent: 2em">enable unicode conversions 0 0 1 1
<p style="text-indent: 2em">number of languages in cache 3 4 3 3
<p style="text-indent: 2em">size of unilib cache 0 140 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">deadlock checking period 500 0 500 500
<p style="text-indent: 2em">freelock transfer block size 30 0 30 30
<p style="text-indent: 2em">lock address spinlock ratio 100 0 100 100
<p style="text-indent: 2em">lock hashtable size 2048 48 2048 2048
<p style="text-indent: 2em">lock scheme allpages 0 allpages allpages
<p style="text-indent: 2em">lock spinlock ratio 85 0 85 85
<p style="text-indent: 2em">lock table spinlock ratio 20 0 20 20
<p style="text-indent: 2em">lock wait period 2147483647 0 2147483647 2147483647
<p style="text-indent: 2em">max engine freelocks 10 0 10 10
<p style="text-indent: 2em">number of locks 5000 2344 10000 10000
<p style="text-indent: 2em">print deadlock information 0 0 1 1
<p style="text-indent: 2em">read committed with lock 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">additional network memory 0 0 0 0
<p style="text-indent: 2em">allow resource limits 0 0 0 0
<p style="text-indent: 2em">audit queue size 100 42 100 100
<p style="text-indent: 2em">average cap size 200 0 200 200
<p style="text-indent: 2em">caps per ccb 50 0 50 50
<p style="text-indent: 2em">deadlock pipe max messages 0 0 0 0
<p style="text-indent: 2em">default network packet size 512 #505 512 512
<p style="text-indent: 2em">disk i/o structures 256 31 256 256
<p style="text-indent: 2em">enable rep agent threads 0 0 0 0
<p style="text-indent: 2em">errorlog pipe max messages 0 0 0 0
<p style="text-indent: 2em">event buffers per engine 100 #11 100 100
<p style="text-indent: 2em">executable codesize + overhead 0 20261 0 20261
<p style="text-indent: 2em">lock hashtable size 2048 48 2048 2048
<p style="text-indent: 2em">lock spinlock ratio 85 0 85 85
<p style="text-indent: 2em">max cis remote servers 25 19 25 25
<p style="text-indent: 2em">max number network listeners 5 868 5 5
<p style="text-indent: 2em">max online engines 1 216 1 1
<p style="text-indent: 2em">max roles enabled per user 20 #22 20 20
<p style="text-indent: 2em">memory per worker process 1024 0 1024 1024
<p style="text-indent: 2em">number of alarms 40 3 40 40
<p style="text-indent: 2em">number of aux scan descriptors 200 #258 200 200
<p style="text-indent: 2em">number of ccbs 0 0 0 0
<p style="text-indent: 2em">number of devices 10 #5 10 10
<p style="text-indent: 2em">number of languages in cache 3 4 3 3
<p style="text-indent: 2em">number of large i/o buffers 6 97 6 6
<p style="text-indent: 2em">number of locks 5000 2344 10000 10000
<p style="text-indent: 2em">number of mailboxes 30 1 30 30
<p style="text-indent: 2em">number of messages 64 3 64 64
<p style="text-indent: 2em">number of open databases 12 1239 12 12
<p style="text-indent: 2em">number of open indexes 500 512 500 500
<p style="text-indent: 2em">number of open objects 500 561 500 500
<p style="text-indent: 2em">number of remote connections 20 86 50 50
<p style="text-indent: 2em">number of remote logins 20 23 20 20
<p style="text-indent: 2em">number of remote sites 10 1729 10 10
<p style="text-indent: 2em">number of user connections 25 43141 250 250
<p style="text-indent: 2em">number of worker processes 0 0 0 0
<p style="text-indent: 2em">partition groups 1024 904 1024 1024
<p style="text-indent: 2em">permission cache entries 15 #227 15 15
<p style="text-indent: 2em">plan text pipe max messages 0 0 0 0
<p style="text-indent: 2em">procedure cache percent 20 22426 20 20
<p style="text-indent: 2em">process wait events 0 0 0 0
<p style="text-indent: 2em">remote server pre-read packets 3 #83 3 3
<p style="text-indent: 2em">size of global fixed heap 300 0 300 300
<p style="text-indent: 2em">size of process object heap 300 0 300 300
<p style="text-indent: 2em">size of shared class heap 3072 0 3072 3072
<p style="text-indent: 2em">size of unilib cache 0 140 0 0
<p style="text-indent: 2em">sql text pipe max messages 0 0 0 0
<p style="text-indent: 2em">stack guard size 4096 #1108 4096 4096
<p style="text-indent: 2em">stack size 86016 #23269 86016 86016
<p style="text-indent: 2em">statement pipe max messages 0 0 0 0
<p style="text-indent: 2em">total data cache size 0 89698 0 89698
<p style="text-indent: 2em">total memory 47104 196608 98304 98304
<p style="text-indent: 2em">txn to pss ratio 16 3692 16 16
<p style="text-indent: 2em">wait event timing 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">number of open databases 12 1239 12 12
<p style="text-indent: 2em">number of open indexes 500 512 500 500
<p style="text-indent: 2em">number of open objects 500 561 500 500
<p style="text-indent: 2em">open index hash spinlock ratio 100 0 100 100
<p style="text-indent: 2em">open index spinlock ratio 100 0 100 100
<p style="text-indent: 2em">open object spinlock ratio 100 0 100 100
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">Q diagnostics active 0 0 0 0
<p style="text-indent: 2em">SQL batch capture 0 0 0 0
<p style="text-indent: 2em">deadlock pipe active 0 0 0 0
<p style="text-indent: 2em">deadlock pipe max messages 0 0 0 0
<p style="text-indent: 2em">errorlog pipe active 0 0 0 0
<p style="text-indent: 2em">errorlog pipe max messages 0 0 0 0
<p style="text-indent: 2em">object lockwait timing 0 0 0 0
<p style="text-indent: 2em">per object statistics active 0 0 0 0
<p style="text-indent: 2em">plan text pipe active 0 0 0 0
<p style="text-indent: 2em">plan text pipe max messages 0 0 0 0
<p style="text-indent: 2em">process wait events 0 0 0 0
<p style="text-indent: 2em">sql text pipe active 0 0 0 0
<p style="text-indent: 2em">sql text pipe max messages 0 0 0 0
<p style="text-indent: 2em">statement pipe active 0 0 0 0
<p style="text-indent: 2em">statement pipe max messages 0 0 0 0
<p style="text-indent: 2em">statement statistics active 0 0 0 0
<p style="text-indent: 2em">wait event timing 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">additional network memory 0 0 0 0
<p style="text-indent: 2em">allow remote access 1 0 1 1
<p style="text-indent: 2em">allow sendmsg 0 0 0 0
<p style="text-indent: 2em">default network packet size 512 #505 512 512
<p style="text-indent: 2em">max network packet size 512 0 512 512
<p style="text-indent: 2em">max number network listeners 5 868 5 5
<p style="text-indent: 2em">number of remote connections 20 86 50 50
<p style="text-indent: 2em">number of remote logins 20 23 20 20
<p style="text-indent: 2em">number of remote sites 10 1729 10 10
<p style="text-indent: 2em">remote server pre-read packets 3 #83 3 3
<p style="text-indent: 2em">syb_sendmsg port number 0 0 0 0
<p style="text-indent: 2em">tcp no delay 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">max async i/os per engine 2147483647 0 2147483647 2147483647
<p style="text-indent: 2em">max async i/os per server 2147483647 0 2147483647 2147483647
<p style="text-indent: 2em">o/s file descriptors 0 0 0 1024
<p style="text-indent: 2em">tcp no delay 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">max parallel degree 1 0 1 1
<p style="text-indent: 2em">max scan parallel degree 1 0 1 1
<p style="text-indent: 2em">memory per worker process 1024 0 1024 1024
<p style="text-indent: 2em">number of worker processes 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">additional network memory 0 0 0 0
<p style="text-indent: 2em">lock shared memory 0 0 0 0
<p style="text-indent: 2em">max SQL text monitored 0 7 0 0
<p style="text-indent: 2em">shared memory starting address 0 0 0 0
<p style="text-indent: 2em">total memory 47104 196608 98304 98304
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">max online engines 1 216 1 1
<p style="text-indent: 2em">min online engines 1 0 1 1
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">enable rep agent threads 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">SQL Perfmon Integration 1 0 1 1
<p style="text-indent: 2em">abstract plan cache 0 0 0 0
<p style="text-indent: 2em">abstract plan dump 0 0 0 0
<p style="text-indent: 2em">abstract plan load 0 0 0 0
<p style="text-indent: 2em">abstract plan replace 0 0 0 0
<p style="text-indent: 2em">allow backward scans 1 0 1 1
<p style="text-indent: 2em">allow nested triggers 1 0 1 1
<p style="text-indent: 2em">allow resource limits 0 0 0 0
<p style="text-indent: 2em">allow updates to system tables 0 0 1 1
<p style="text-indent: 2em">audit queue size 100 42 100 100
<p style="text-indent: 2em">cpu accounting flush interval 200 0 200 200
<p style="text-indent: 2em">cpu grace time 500 0 500 500
<p style="text-indent: 2em">deadlock retries 5 0 5 5
<p style="text-indent: 2em">default database size 2 0 2 2
<p style="text-indent: 2em">default exp_row_size percent 5 0 5 5
<p style="text-indent: 2em">default fill factor percent 0 0 0 0
<p style="text-indent: 2em">enable DTM 0 0 0 0
<p style="text-indent: 2em">enable HA 0 0 0 0
<p style="text-indent: 2em">enable housekeeper GC 1 0 1 1
<p style="text-indent: 2em">enable sort-merge join and JTC 0 0 0 0
<p style="text-indent: 2em">event buffers per engine 100 #11 100 100
<p style="text-indent: 2em">housekeeper free write percent 1 0 1 1
<p style="text-indent: 2em">i/o accounting flush interval 1000 0 1000 1000
<p style="text-indent: 2em">i/o polling process count 10 0 10 10
<p style="text-indent: 2em">identity burning set factor 5000 0 5000 5000
<p style="text-indent: 2em">identity grab size 1 0 1 1
<p style="text-indent: 2em">license information 25 0 25 25
<p style="text-indent: 2em">number of alarms 40 3 40 40
<p style="text-indent: 2em">number of aux scan descriptors 200 #258 200 200
<p style="text-indent: 2em">number of large i/o buffers 6 97 6 6
<p style="text-indent: 2em">number of mailboxes 30 1 30 30
<p style="text-indent: 2em">number of messages 64 3 64 64
<p style="text-indent: 2em">number of open databases 12 1239 12 12
<p style="text-indent: 2em">number of open indexes 500 512 500 500
<p style="text-indent: 2em">number of open objects 500 561 500 500
<p style="text-indent: 2em">number of pre-allocated extent 2 0 2 2
<p style="text-indent: 2em">number of sort buffers 500 0 500 500
<p style="text-indent: 2em">page lock promotion HWM 200 0 200 200
<p style="text-indent: 2em">page lock promotion LWM 200 0 200 200
<p style="text-indent: 2em">page lock promotion PCT 100 0 100 100
<p style="text-indent: 2em">partition groups 1024 904 1024 1024
<p style="text-indent: 2em">partition spinlock ratio 10 0 10 10
<p style="text-indent: 2em">print deadlock information 0 0 1 1
<p style="text-indent: 2em">row lock promotion HWM 200 0 200 200
<p style="text-indent: 2em">row lock promotion LWM 200 0 200 200
<p style="text-indent: 2em">row lock promotion PCT 100 0 100 100
<p style="text-indent: 2em">runnable process search count 2000 0 2000 2000
<p style="text-indent: 2em">size of auto identity column 10 0 10 10
<p style="text-indent: 2em">sql server clock tick length 100000 0 100000 100000
<p style="text-indent: 2em">text prefetch size 16 0 16 16
<p style="text-indent: 2em">time slice 100 0 100 100
<p style="text-indent: 2em">upgrade version 1100 0 12000 12000
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">allow procedure grouping 1 0 1 1
<p style="text-indent: 2em">auditing 0 0 0 0
<p style="text-indent: 2em">check password for digit 0 0 0 0
<p style="text-indent: 2em">curread change w/ open cursors 1 0 1 1
<p style="text-indent: 2em">current audit table 1 0 1 1
<p style="text-indent: 2em">max roles enabled per user 20 #22 20 20
<p style="text-indent: 2em">maximum failed logins 0 0 0 0
<p style="text-indent: 2em">minimum password length 6 0 6 6
<p style="text-indent: 2em">msg confidentiality reqd 0 0 0 0
<p style="text-indent: 2em">msg integrity reqd 0 0 0 0
<p style="text-indent: 2em">secure default login guest 0 guest guest
<p style="text-indent: 2em">select on syscomments.text 1 0 1 1
<p style="text-indent: 2em">suspend audit when device full 1 0 1 1
<p style="text-indent: 2em">unified login required 0 0 0 0
<p style="text-indent: 2em">use security services 0 0 0 0
<p style="text-indent: 2em">
<p style="text-indent: 2em">Parameter Name Default Memory Used Config Value Run Value
<p style="text-indent: 2em">-------------- ------- ----------- ------------ ---------
<p style="text-indent: 2em">default network packet size 512 #505 512 512
<p style="text-indent: 2em">number of pre-allocated extent 2 0 2 2
<p style="text-indent: 2em">number of user connections 25 43141 250 250
<p style="text-indent: 2em">permission cache entries 15 #227 15 15
<p style="text-indent: 2em">stack guard size 4096 #1108 4096 4096
<p style="text-indent: 2em">stack size 86016 #23269 86016 86016
<p style="text-indent: 2em">systemwide password expiration 0 0 0 0
<p style="text-indent: 2em">user log cache size 2048 0 2048 2048
<p style="text-indent: 2em">user log cache spinlock ratio 20 0 20 20 </p>
<img src ="http://www.blogjava.net/shanben/aggbug/211952.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shanben/" target="_blank">虎啸长沙,龙跃深圳.</a> 2008-07-01 16:48 <a href="http://www.blogjava.net/shanben/archive/2008/07/01/211952.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>