Amazon Redshift: 『パフォーマンスチューニングテクニック Top 10』手順の実践:オプティマイザのアラートからDBの改善点を探る

2016.09.28

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

当エントリは先日投稿したシリーズ『『Amazon Redshiftのパフォーマンスチューニングテクニック Top 10』手順実践』の"おまけ"的な内容となります。参照元エントリの末尾、Tipsで紹介されていた『オプティマイザのアラート』の内容についてもこれまで紹介してきた10個のトピック同様有用なものですので、その内容についても見て行きたいと思います。


該当パートで紹介されているSQLの内容を見易く整形したものが以下となります。定期的にアラートとして出力されている内容をテーブル単位で集約し、期間を『直近1週間』としています。

SELECT
  TRIM(s.perm_table_name) AS table ,
  (SUM
    (ABS
      (DATEDIFF(seconds, coalesce(b.starttime,d.starttime,s.starttime),
        CASE
          WHEN COALESCE(b.endtime,d.endtime,s.endtime) > COALESCE(b.starttime,d.starttime,s.starttime)
          THEN COALESCE(b.endtime,d.endtime,s.endtime)
          ELSE COALESCE(b.starttime,d.starttime,s.starttime)
        END )
      )
    )/60
  )::numeric(24,0) AS minutes,
  SUM(COALESCE(b.rows,d.rows,s.rows)) AS rows,
  TRIM(SPLIT_PART(l.event,':',1)) AS event,
  SUBSTRING(TRIM(l.solution),1,200) AS solution ,
  MAX(l.query) AS sample_query,
  COUNT(distinct l.query)
FROM
  stl_alert_event_log AS l
    left join stl_scan AS s ON s.query = l.query AND s.slice = l.slice AND s.segment = l.segment
    left join stl_dist AS d ON d.query = l.query AND d.slice = l.slice AND d.segment = l.segment
    left join stl_bcast AS b ON b.query = l.query AND b.slice = l.slice AND b.segment = l.segment
WHERE
  l.userid >1
  AND  l.event_time >=  DATEADD(day, -7, current_Date)
  -- AND s.perm_table_name not like 'volt_tt%'
GROUP BY 1,4,5
ORDER BY 2 desc,6 desc;

上記クエリを実行した際の出力結果サンプルは以下となります。テーブル名が空欄のものは対処しようが無いのでアレですが、テーブル名が特定出来るものについてはその内容と解決策が提示される形となっています。

      table        | minutes | rows  |                 event                  |                                                        solution                                                         | sample_query | count 
-------------------+---------+-------+----------------------------------------+-------------------------------------------------------------------------------------------------------------------------+--------------+-------
                   |         |       | Missing query planner statistics       | Run the ANALYZE command                                                                                                 |       558768 |   253
                   |         |       | Nested Loop Join in the query plan     | Review the join predicates to avoid Cartesian products                                                                  |       557680 |    47
                   |         |       | Scanned a large number of deleted rows | Run the VACUUM command to reclaim deleted space                                                                         |       535481 |     7
 table_aaa         |       0 | 29137 | Very selective query filter            | Review the choice of sort key to enable range restricted scans, or run the VACUUM command to ensure the table is sorted |       557699 |     3
 table_bbb         |       0 | 48679 | Very selective query filter            | Review the choice of sort key to enable range restricted scans, or run the VACUUM command to ensure the table is sorted |       557698 |     6
 table_ccc         |       0 | 36012 | Very selective query filter            | Review the choice of sort key to enable range restricted scans, or run the VACUUM command to ensure the table is sorted |       557694 |     3
 table_ddd         |       0 | 39457 | Scanned a large number of deleted rows | Run the VACUUM command to reclaim deleted space                                                                         |       553624 |     1
 table_eee         |       0 |  2552 | Very selective query filter            | Review the choice of sort key to enable range restricted scans, or run the VACUUM command to ensure the table is sorted |       552789 |     4
(8 rows)

表示されるアラートの一覧としては、このような形になるようです。

                         event                         |                                                        solution                                                         
-------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------
 Broadcasted a large number of rows across the network | Review the choice of distribution key to collocate the join and consider using distributed tables
 Missing query planner statistics                      | Run the ANALYZE command
 Nested Loop Join in the query plan                    | Review the join predicates to avoid Cartesian products
 Distributed a large number of rows across the network | Review the choice of distribution key to collocate the join or aggregation
 Very selective query filter                           | Review the choice of sort key to enable range restricted scans, or run the VACUUM command to ensure the table is sorted
 Scanned a large number of deleted rows                | Run the VACUUM command to reclaim deleted space

上記内容の様に表示されるアラートは英語ですが、こちらはAWS公式ドキュメントに日本語の情報が展開されています。下記に出力結果として表示されるイベント名(STL_ALERT_EVENT_LOG.event)とAWS公式ドキュメント記載のイベント名(日本語)の対応表をまとめておきます。作業の際の参考にして頂ければ幸いです。

イベント名(英語) イベント名(日本語)
Broadcasted a large number of rows across the network 直列実行
Missing query planner statistics 見つからない統計
Nested Loop Join in the query plan Nested Loop
Distributed a large number of rows across the network サイズの大きな分散
Very selective query filter 非常に選択的なフィルタ
Scanned a large number of deleted rows 過剰な数の非実体行

stl_alert_event_log_alertslist

まとめ

『Amazon Redshiftのパフォーマンスチューニングテクニック Top 10』、延長戦的なトピックとなる『クエリオプティマイザのアラート』のご紹介でした。対処法まで詳細に解説が用意されていますので、まずはこちらの内容を見て思い当たるものから適宜対処を行っていく、という風に進めてみるのも良いかも知れません。1つの指針として上手く活用して行きたいですね。こちらからは以上です。