StatspackはOracle Standard Edition(SE)でも使用できる性能診断ツールです。
本記事では、Statspackのインストールから、手動・自動スナップショット取得方法、古いデータの削除方法まで、DBAが現場ですぐ使えるように徹底的に解説します。
💰 【PR】Oracleエンジニアの市場価値、調べてみませんか?
Oracleのスキルは需要が高く、特定の資格や経験を持っていると年収が大幅にアップするケースがあります。まずはIT専門のエージェントで非公開求人をチェックしてみませんか?
✅ 記事で解説する内容
- Statspackのインストール方法(spcreate.sql)
- 手動でスナップショットを取得する方法
spauto.sqlを使って1時間ごとに自動取得する方法- 古いスナップショットを削除する方法
🛠️ 1. Statspackのインストール手順(初期セットアップ)
StatspackはOracleに標準で用意されており、簡単にインストールできます。
📌 実行スクリプト
$ORACLE_HOME/rdbms/admin/spcreate.sql
⚙️ 手順
- SQL*PlusでSYSDBAとしてログイン
sqlplus / as sysdba
- スクリプト実行
@?/rdbms/admin/spcreate.sql
- 実行中に聞かれる内容(例)
PERFSTATユーザーのパスワード:perfstat
表領域名:USERS(または専用表領域)
TEMP表領域:TEMP
✅ これでPERFSTATユーザーと必要な統計情報収集テーブル群が作成されます。
SQL> @?/rdbms/admin/spcreate.sql
セッションが変更されました。
Choose the PERFSTAT user's password
-----------------------------------
Not specifying a password will result in the installation FAILING
perfstat_passwordに値を入力してください: perfstat
perfstat
Choose the Default tablespace for the PERFSTAT user
---------------------------------------------------
Below is the list of online tablespaces in this database which can
store user data. Specifying the SYSTEM tablespace for the user's
default tablespace will result in the installation FAILING, as
using SYSTEM for performance data is not supported.
Choose the PERFSTAT users's default tablespace. This is the tablespace
in which the STATSPACK tables and indexes will be created.
TABLESPACE_NAME
--------------------------------------------------------------------------------
CONTENTS
---------------------------------------------------------------
STATSPACK DEFAULT TABLESPACE
----------------------------
SYSAUX
PERMANENT
*
USERS
PERMANENT
TABLESPACE_NAME
--------------------------------------------------------------------------------
CONTENTS
---------------------------------------------------------------
STATSPACK DEFAULT TABLESPACE
----------------------------
Pressing <return> will result in STATSPACK's recommended default
tablespace (identified by *) being used.
default_tablespaceに値を入力してください: sysaux
Using tablespace SYSAUX as PERFSTAT default tablespace.
Choose the Temporary tablespace for the PERFSTAT user
-----------------------------------------------------
Below is the list of online tablespaces in this database which can
store temporary data (e.g. for sort workareas). Specifying the SYSTEM
tablespace for the user's temporary tablespace will result in the
installation FAILING, as using SYSTEM for workareas is not supported.
Choose the PERFSTAT user's Temporary tablespace.
TABLESPACE_NAME
--------------------------------------------------------------------------------
CONTENTS
---------------------------------------------------------------
DB DEFAULT TEMP TABLESPACE
--------------------------
TEMP
TEMPORARY
*
Pressing <return> will result in the database's default Temporary
tablespace (identified by *) being used.
temporary_tablespaceに値を入力してください: temp
Using tablespace temp as PERFSTAT temporary tablespace.
:
SQL> set echo off;
Creating Package STATSPACK...
パッケージが作成されました。
エラーはありません。
Creating Package Body STATSPACK...
パッケージ本体が作成されました。
エラーはありません。
NOTE:
SPCPKG complete. Please check spcpkg.lis for any errors.
SQL>
SQL> -- Bug#25233027: xxx Set this parameter to FALSE for creating common objects in consolidated database
SQL> alter session set "_oracle_script" = FALSE;
セッションが変更されました。
SQL>
💰 【PR】Oracleエンジニアの市場価値、調べてみませんか?
Oracleのスキルは需要が高く、特定の資格や経験を持っていると年収が大幅にアップするケースがあります。まずはIT専門のエージェントで非公開求人をチェックしてみませんか?
✋ 2. 手動でスナップショットを取得する
スナップショットの取得は、PERFSTATユーザーまたはEXEC権限を持つユーザーで以下を実行します。
EXEC statspack.snap;
📌 基本的にはこれで「ある時点」の性能情報が取得されます。
⏰ 3. 1時間ごとに自動でスナップショットを取得する(spauto.sql)
Statspackには、自動スナップショット取得を設定する専用スクリプト spauto.sql が用意されています。
📦 spauto.sqlとは?
- DBMS_JOBを使って、自動で
statspack.snapを定期実行する設定を行うスクリプトです。 - デフォルトでは「1時間おき」にsnapを取得します。
⚙️ 実行手順
PERFSTATユーザーでSQL*Plusにログイン
sqlplus perfstat/perfstat
spauto.sqlを実行
@?/rdbms/admin/spauto.sql
✅ ジョブが1時間ごとにスナップショットを自動取得するよう設定されます。
📝 作成されたジョブの確認
SELECT job, next_date, interval, what FROM user_jobs;
SQL> @?/rdbms/admin/spauto.sql
SQL> Rem
SQL> Rem $Header: rdbms/admin/spauto.sql /main/5 2017/05/28 22:46:10 stanaya Exp $
SQL> Rem
SQL> Rem spauto.sql
SQL> Rem
SQL> Rem Copyright (c) 1999, 2017, Oracle and/or its affiliates.
SQL> Rem All rights reserved.
SQL> Rem
SQL> Rem NAME
SQL> Rem spauto.sql
SQL> Rem
SQL> Rem DESCRIPTION
SQL> Rem SQL*PLUS command file to automate the collection of STATPACK
SQL> Rem statistics.
SQL> Rem
SQL> Rem NOTES
SQL> Rem Should be run as the STATSPACK owner, PERFSTAT.
SQL> Rem Requires job_queue_processes init.ora parameter to be
SQL> Rem set to a number >0 before automatic statistics gathering
SQL> Rem will run.
SQL> Rem
SQL> Rem BEGIN SQL_FILE_METADATA
SQL> Rem SQL_SOURCE_FILE: rdbms/admin/spauto.sql
SQL> Rem SQL_SHIPPED_FILE: rdbms/admin/spauto.sql
SQL> Rem SQL_PHASE: UTILITY
SQL> Rem SQL_STARTUP_MODE: NORMAL
SQL> Rem SQL_IGNORABLE_ERRORS: NONE
SQL> Rem END SQL_FILE_METADATA
SQL> Rem
SQL> Rem MODIFIED (MM/DD/YY)
SQL> Rem cdialeri 02/16/00 - 1191805
SQL> Rem cdialeri 12/06/99 - 1059172, 1103031
SQL> Rem cdialeri 08/13/99 - Created
SQL> Rem
SQL>
SQL>
SQL> spool spauto.lis
SQL>
SQL> --
SQL> -- Schedule a snapshot to be run on this instance every hour, on the hour
SQL>
SQL> variable jobno number;
SQL> variable instno number;
SQL> begin
2 select instance_number into :instno from v$instance;
3 dbms_job.submit(:jobno, 'statspack.snap;', trunc(sysdate+1/24,'HH'), 'trunc(SYSDATE+1/24,''HH'')', TRUE, :instno);
4 commit;
5 end;
6 /
PL/SQLプロシージャが正常に完了しました。
SQL>
SQL>
SQL> prompt
SQL> prompt Job number for automated statistics collection for this instance
Job number for automated statistics collection for this instance
SQL> prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> prompt Note that this job number is needed when modifying or removing
Note that this job number is needed when modifying or removing
SQL> prompt the job:
the job:
SQL> print jobno
JOBNO
----------
2
SQL>
SQL> prompt
SQL> prompt Job queue process
Job queue process
SQL> prompt ~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~
SQL> prompt Below is the current setting of the job_queue_processes init.ora
Below is the current setting of the job_queue_processes init.ora
SQL> prompt parameter - the value for this parameter must be greater
parameter - the value for this parameter must be greater
SQL> prompt than 0 to use automatic statistics gathering:
than 0 to use automatic statistics gathering:
SQL> show parameter job_queue_processes
NAME TYPE VALUE
------------------------------------ --------------------------------- ------------------------------
job_queue_processes integer 20
SQL> prompt
SQL>
SQL> prompt
SQL> prompt Next scheduled run
Next scheduled run
SQL> prompt ~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~
SQL> prompt The next scheduled run for this job is:
The next scheduled run for this job is:
SQL> select job, next_date, next_sec
2 from user_jobs
3 where job = :jobno;
JOB NEXT_DAT NEXT_SEC
---------- -------- ------------------------------------------------------------------------------------------------
2 25-04-05 00:00:00
1行が選択されました。
SQL>
SQL> spool off;
SQL> alter session set nls_date_format='YYYY/MM/DD HH24:MI:SS';
セッションが変更されました。
SQL> col interval for a30
SQL> col what for a30
SQL> SELECT job, next_date, interval, what FROM user_jobs;
JOB NEXT_DATE INTERVAL WHAT
---------- ------------------- ------------------------------ ------------------------------
2 2025/04/05 01:00:00 trunc(SYSDATE+1/24,'HH') statspack.snap;
1行が選択されました。
🛑 自動取得を停止・削除する方法
-- 作成されたジョブ番号を確認
SELECT job FROM user_jobs;
-- JOBを削除
BEGIN
DBMS_JOB.REMOVE(<JOB番号>);
COMMIT;
END;
/
🧽 4. 古いスナップショットの削除方法
Statspackではスナップショットが蓄積されていくため、表領域圧迫を避けるためにも古いデータの削除が必要です。
📌 スナップ一覧を確認
SELECT snap_id, snap_time FROM stats$snapshot ORDER BY snap_id;
🗑 古いスナップショットを削除
-- 例:snap_id 1~100の範囲を削除
EXEC stats$delete_snapshot_range(low_snap_id => 1, high_snap_id => 100);
✅ 実行後にCOMMITは不要です(PL/SQLプロシージャ内で自動実行されます)。
📘 運用イメージ図(テキスト)
【Statspack運用フロー】
┌────────────┐
│ spcreate.sql実行 │ ← 初期セットアップ
└────┬───────┘
▼
┌────────────┐
│ spauto.sql実行 │ ← 1時間ごとにsnapを取得
└────┬───────┘
▼
┌────────────┐
│ stats$snapshot表に蓄積 │
└────┬───────┘
▼
┌────────────┐
│ 定期削除で管理 │ ← 表領域肥大化防止
└────────────┘
✅ まとめ
| ポイント | 内容 |
|---|---|
spcreate.sql でStatspackをインストール | |
statspack.snap で手動スナップショット取得が可能 | |
spauto.sql を使えば1時間ごとの自動取得が簡単に設定できる | |
stats$delete_snapshot_range で古いsnapを削除し、表領域肥大化を防ぐ | |
| 自動収集+定期削除の運用が基本パターン |
[参考]
Oracle® Databaseパフォーマンス・チューニング・ガイド 19c




コメント