質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Oracle

Oracleは、米オラクルが取り扱うリレーショナルデータベース管理システムです。メインフレームからPCまで、多様なプラットフォームに対応しています。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Q&A

解決済

1回答

596閲覧

【Oracle】統計情報取得後に実行計画のコストが高くなるのは何故?

ametyan

総合スコア43

Oracle

Oracleは、米オラクルが取り扱うリレーショナルデータベース管理システムです。メインフレームからPCまで、多様なプラットフォームに対応しています。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

0グッド

0クリップ

投稿2018/06/02 05:09

下記のようにテーブル&レコード作成した環境で統計情報の取得前と取得後の実行計画を比較してみました。
すると、統計情報取得後の方がコストが高くなっているのですが、これはどうしてなのでしょうか?

###■環境準備

sql

1-- テーブル作成 2CREATE TABLE JOB_MASTER ( 3 UPD_TIME DATE DEFAULT SYSDATE 4 ,NAME_ID CHAR(8) 5 ,JOB_ID CHAR(6) 6 ,TSUBAN NUMBER(10) 7 ,COMPANY_NAME VARCHAR(30) 8 ,EXIST_FLG CHAR(1) 9 ,SAVE_MONEY NUMBER(15) 10 ,PRIMARY KEY(NAME_ID, JOB_ID) 11) 12 13-- データ作成 14BEGIN 15 FOR I IN 1..1000000 LOOP 16 INSERT INTO JOB_MASTER 17 SELECT SYSDATE 18 ,LPAD(I, 8, 'N') 19 ,LPAD(I, 6, 'J') 20 ,I 21 ,DBMS_RANDOM.STRING('A', 30) 22 ,'0' 23 ,ABS(DBMS_RANDOM.VALUE(0, 999999999999999)) 24 FROM DUAL; 25 END LOOP; 26 COMMIT; 27END; 28/

###実行計画取得対象SQL

sql

1DELETE FROM JOB_MASTER WHERE NAME_ID <= 'NN453214';

###■統計情報取得前

353,216行削除されました。 Plan hash value: 3759745238 --------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------- | 0 | DELETE STATEMENT | | 117 | 2106 | 60 (0)| 00:00:01 | | 1 | DELETE | JOB_MASTER | | | | | |* 2 | INDEX RANGE SCAN| SYS_C008248 | 117 | 2106 | 60 (0)| 00:00:01 | --------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("NAME_ID"<='NN453214') Note ----- - dynamic sampling used for this statement (level=2) Statistics ----------------------------------------------------------- 603 CPU used by this session 603 CPU used when call started 1167 DB time 216 enqueue conversions 399 enqueue releases 401 enqueue requests 323 messages sent 8592 non-idle wait count 579 non-idle wait time 84 opened cursors cumulative 1 opened cursors current 8548 physical read total IO requests 82534400 physical read total bytes 51 physical write total IO requests 12804096 physical write total bytes 50 physical write total multi block requests 1 pinned cursors current 725 recursive calls 3 recursive cpu usage 399179 session logical reads 564 user I/O wait time 6 user calls

###■統計情報取得

sql

1-- 統計情報取得(第一引数はスキーマ名) 2EXECUTE DBMS_STATS.GATHER_TABLE_STATS('SYS', 'JOB_MASTER');

###■統計情報取得後

353,216行削除されました。 Plan hash value: 941518712 --------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------- | 0 | DELETE STATEMENT | | 996K| 15M| 2929 (2)| 00:00:36 | | 1 | DELETE | JOB_MASTER | | | | | |* 2 | TABLE ACCESS FULL| JOB_MASTER | 996K| 15M| 2929 (2)| 00:00:36 | --------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("NAME_ID"<='NN453214') Statistics ----------------------------------------------------------- 1650 CPU used by this session 1650 CPU used when call started 2276 DB time 90 enqueue conversions 441 enqueue releases 443 enqueue requests 1 enqueue waits 329 messages sent 3645 non-idle wait count 550 non-idle wait time 52 opened cursors cumulative 1 opened cursors current 3495 physical read total IO requests 1 pinned cursors current 400 recursive calls 13 recursive cpu usage 1461632 session logical reads 531 user I/O wait time 6 user calls

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sazi

2018/06/02 13:30

手順は、①[環境準備]→[実行]、②[環境準備]→[統計情報取得]→[実行]だと思いますが、②ではcreate tableから行っていますか?
ametyan

2018/06/02 14:15

はい、DROP TABLE JOB_MASTER PURGE; コマンドを使用してテーブルを削除してから再作成しています。
guest

回答1

0

ベストアンサー

実行計画を見るとINDEX RANGE SCAN → TABLE ACCESS FULLとなっていますから、この部分で遅くなっているのは明らかです。

以下でインデックス情報を取得するようにすると改善されないでしょうか。

SQL

1EXECUTE DBMS_STATS.GATHER_TABLE_STATS('SYS', 'JOB_MASTER', 'FOR ALL INDEXED');

投稿2018/06/02 14:43

sazi

総合スコア25138

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ametyan

2018/06/02 14:52

ご回答ありがとうございます。 EXECUTE DBMS_STATS.GATHER_TABLE_STATS('SYS', 'JOB_MASTER', method_opt => 'FOR ALL INDEXES'); を使用して実行計画取得したところ、コストがだいぶ下がりました。 勉強になりました。 353,216行削除されました。 Plan hash value: 3620915039 --------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------- | 0 | DELETE STATEMENT | | 50000 | 878K| 61 (0)| 00:00:01 | | 1 | DELETE | JOB_MASTER | | | | | |* 2 | INDEX RANGE SCAN| SYS_C008257 | 50000 | 878K| 61 (0)| 00:00:01 | --------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("NAME_ID"<='NN453214') Statistics ----------------------------------------------------------- 592 CPU used by this session 593 CPU used when call started 1159 DB time 353216 HSC Heap Segment Block Changes 1 IMU Flushes 14 SQL*Net roundtrips to/from client 187 buffer is not pinned count 383 bytes received via SQL*Net from client 23717 bytes sent via SQL*Net to client 51 calls to get snapshot scn: kcmgss 25 calls to kcmgas 39 calls to kcmgcs 92954624 cell physical IO interconnect bytes 279 change write time 1885 cleanout - number of ktugct calls 1885 cleanouts only - consistent read gets 6 cluster key scan block gets 5 cluster key scans 1885 commit txn count during cleanout 4357 consistent gets 1904 consistent gets - examination 4357 consistent gets from cache 134 consistent gets from cache (fastpath) 1 cursor authentications 740783 db block changes 389608 db block gets 389608 db block gets from cache 6089 db block gets from cache (fastpath) 17066 dirty buffers inspected 92 enqueue releases 94 enqueue requests 50 execute count 5569417 file io wait time 17946 free buffer inspected 17885 free buffer requested 3 hot buffers moved to head of LRU 1885 immediate (CR) block cleanout applications 8 immediate (CURRENT) block cleanout applications 7 index fetch by key 45 index scans kdiixs1 255 messages sent 490 no work - consistent read gets 8427 non-idle wait count 576 non-idle wait time 50 opened cursors cumulative 1 opened cursors current 1 parse count (hard) 50 parse count (total) 1 parse time elapsed 8453 physical read IO requests 80150528 physical read bytes 8453 physical read total IO requests 80150528 physical read total bytes 9784 physical reads 8221 physical reads cache 1563 physical reads direct 1563 physical reads direct temporary tablespace 61 physical write IO requests 12804096 physical write bytes 61 physical write total IO requests 12804096 physical write total bytes 49 physical write total multi block requests 1563 physical writes 1563 physical writes direct 1563 physical writes direct temporary tablespace 1563 physical writes non checkpoint 53 pinned buffers inspected 758 prefetched blocks aged out before use 414 recursive calls 2 recursive cpu usage 5 redo buffer allocation retries 363524 redo entries 6 redo log space requests 5 redo log space wait time 1 redo ordering marks 128038788 redo size 1 session cursor cache count 38 session cursor cache hits 393965 session logical reads 40 shared hash latch upgrades - no wait 1 sorts (disk) 7 sorts (memory) 353854 sorts (rows) 2 switch current to new buffer 44 table fetch by rowid 77044196 undo change vector size 568 user I/O wait time 15 user calls 22 write clones created in foreground
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問