UJP - 技術情報

Life is fun and easy!

不正IP報告数

Okan Sensor
 
メイン
ログイン
ブログ カテゴリ一覧

     

Sybaseでテーブル利用容量を探る 〜sp_spaceused〜

Sybaseでテーブル利用容量を探る

〜sp_spaceused〜


0.更新履歴

  • 2001.05.08 新規作成

1.はじめに

 このドキュメントでは,Sybaseでテーブルに割り当てられた領域の表示方法を説明する.

2.テーブルのサイズの確認

 テーブルが利用している領域を,推定値で表示する為に,sp_spaceusedを利用する.

 以下に利用例を示す.

1> sp_spaceused user_table
2> go
 name                 rowtotal    reserved   data     index_size unused
 -------------------- ----------- ---------- -------- ---------- -------
 user_table           36947       79606 KB   5476 KB  73896 KB   234 KB

(1 row affected)
(return status = 0)
1> 

 この場合,各項目は次の様になる.

プロセス 説明
name  テーブル名称
rowtotal  ロー数.つまり件数.
reserved  予約済の領域
data  データが利用している領域.
index_size  インデックスが使用している領域.
unused  利用されていない領域.

 つまり,このテーブルの場合,

全体の領域=データ+インデックス+未使用
79606=5476+73896+234

 となる.

3.インデックスの表示

 sp_spaceused テーブル名,1と指定することで,インデックス情報も出力される.

1> sp_spaceused user_table,1
2> go
 index_name           size       reserved   unused
 -------------------- ---------- ---------- ----------
 user_table           0 KB       5630 KB    154 KB
 tuser_table          73896 KB   73976 KB   80 KB

(1 row affected)
 name                 rowtotal    reserved   data     index_size unused
 -------------------- ----------- ---------- -------- ---------- -------
 user_table           36947       79606 KB   5476 KB  73896 KB   234 KB

(return status = 0)
1>

 この場合,user_tabletuser_tableとあるが,tがついているのはtextフィールドまたはimageフィールドが使用している領域を示す.

4.VBScriptを使ってテーブル利用量をレポートする

 以下の例では,ADOを使ってSybaseに接続するVBScriptを使い,複数のテーブルのsp_spaceusedの結果を集計すし,結果をダイアログで表示するサンプルを作成した.

Err.Clear

'インスタンス作成
Set dbCn = CreateObject("ADODB.Connection")

'DB接続する

dbCn.Provider="Sybase ASE OLE DB Provider"
dbCn.Properties("Data Source").Value = "SYBASE01"
dbCn.Properties("Initial Catalog").Value = "SYBASE01"
dbCn.Properties("User ID").Value = "sa"
dbCn.Properties("Password").Value = "password"

	'各テーブルのデータ領域、インデックス領域、未使用領域を取り出してサマリする。

	'取り出した結果は文字列で" KB"というのがついているので、それをとって数字だけにして、足し算する

	'データ領域(3),インデックス領域(4),未使用領域(5)
	'sp_spaceusedを実行すると、2つめは失敗するので、毎回DBOpen,Closeを繰り返している。

      'ちょっとかっこわるい

	intSumData = 0
	intSumIndx = 0
	intSumFree = 0

dbCn.Open
	strSQL = "sp_spaceused testtable1"
	Set dbRs = dbCn.Execute(strSQL)
	temp1 = trim(dbRs.Fields.Item(3)):temp2 = left(temp1,len(temp1)-3)
		intSumData = intSumData + temp2
	temp1 = trim(dbRs.Fields.Item(4)):temp2 = left(temp1,len(temp1)-3)
		intSumIndx = intSumIndx + temp2 
	temp1 = trim(dbRs.Fields.Item(5)):temp2 = left(temp1,len(temp1)-3)
		intSumFree = intSumFree + temp2 
dbCn.Close

dbCn.Open
	strSQL = "sp_spaceused testtable2"
	Set dbRs = dbCn.Execute(strSQL)
	temp1 = trim(dbRs.Fields.Item(3)):temp2 = left(temp1,len(temp1)-3)
		intSumData = intSumData + temp2
	temp1 = trim(dbRs.Fields.Item(4)):temp2 = left(temp1,len(temp1)-3)
		intSumIndx = intSumIndx + temp2 
	temp1 = trim(dbRs.Fields.Item(5)):temp2 = left(temp1,len(temp1)-3)
		intSumFree = intSumFree + temp2 
dbCn.Close
dbCn.Open
	strSQL = "sp_spaceused testtable3"
	Set dbRs = dbCn.Execute(strSQL)
	temp1 = trim(dbRs.Fields.Item(3)):temp2 = left(temp1,len(temp1)-3)
		intSumData = intSumData + temp2
	temp1 = trim(dbRs.Fields.Item(4)):temp2 = left(temp1,len(temp1)-3)
		intSumIndx = intSumIndx + temp2 
	temp1 = trim(dbRs.Fields.Item(5)):temp2 = left(temp1,len(temp1)-3)
		intSumFree = intSumFree + temp2 
dbCn.Close
dbCn.Open
	strSQL = "sp_spaceused testtable4"
	Set dbRs = dbCn.Execute(strSQL)
	temp1 = trim(dbRs.Fields.Item(3)):temp2 = left(temp1,len(temp1)-3)
		intSumData = intSumData + temp2
	temp1 = trim(dbRs.Fields.Item(4)):temp2 = left(temp1,len(temp1)-3)
		intSumIndx = intSumIndx + temp2 
	temp1 = trim(dbRs.Fields.Item(5)):temp2 = left(temp1,len(temp1)-3)
		intSumFree = intSumFree + temp2 
dbCn.Close

	strData = "Data: " & intSumData & " KB" + vbcr
	strData= strData + "Index: " & intSumIndx & " KB" + vbcr
	strData= strData + "Unuse: " & intSumFree  & " KB"

	msgbox strData '結果をダイアログで表示

 sp_spaceusedが返却する値は,文字列でさらに" KB"という文字が含まれるために,取り出したデータそのままでは演算できない.

 よって,trimで右のスペースを削除し," KB"分,つまり右から3文字を削除して“数値”化して足しこみを行っている.



広告スペース
Google