LoginSignup
6
3

More than 5 years have passed since last update.

[AWS] Redshiftの便利なクエリまとめ

Last updated at Posted at 2018-10-06

Redshiftを運用する中で便利なクエリをまとめました。
詳細は"References"をご覧ください。

間違いがあれば、気軽にコメントくださいm(_ _)m

クエリをキャンセルする

-- 1. 実行中のクエリを表示し、PIDを取得する。
SELECT pid, trim(user_name), starttime, substring(query, 1, 200) 
FROM stv_recents
WHERE status = 'Running';

-- 2. クエリをキャンセルする。(pid: 12345のクエリをキャンセル)
cancel 12345;

Superuserキューを使用する

-- 1. Superuserに切り替える。
set query_group to 'superuser';
-- 2. 実行したいクエリを実行する。
cancel 18764;
-- 3. 元のクエリグループに戻る。
reset query_group;

各テーブルのディスク使用量[Mb]を表示する

SELECT name, mb
FROM (
  SELECT tbl, count(*) AS mb
  FROM stv_blocklist
  WHERE tbl in (
    SELECT id
    FROM stv_tbl_perm
  )
  GROUP BY tbl
) block_list
INNER JOIN stv_tbl_perm
ON block_list.tbl = stv_tbl_perm.id
GROUP BY tbl, stv_tbl_perm.name, block_list.mb
ORDER BY mb desc;

各テーブルのソート率を表示する

SELECT
  percentage_result.id,
  percentage_result.schemaname,
  percentage_result.tablename,
  percentage_result.rows,
  percentage_result.sorted_rows,
  percentage_result.sort_percentage
FROM (
  SELECT
    tbl_perm_info.id,
    pg_catalog.svv_table_info.schema AS schemaname,
    tbl_perm_info.tablename,
    tbl_perm_info.rows,
    tbl_perm_info.sorted_rows,
    CAST(
      CAST(tbl_perm_info.sorted_rows AS double precisiON) / CAST(tbl_perm_info.rows AS double precisiON) AS decimal(10,3)
    ) AS sort_percentage
  FROM (
    SELECT
      id,
      TRIM(name) AS tablename,
      SUM(rows) AS rows,
      SUM(sorted_rows) AS sorted_rows
    FROM
      pg_catalog.stv_tbl_perm
    WHERE
      rows != 0
      AND sorted_rows != 0
    GROUP BY
      id, name
  ) tbl_perm_info
  INNER JOIN
    pg_catalog.svv_table_info
  ON
    tbl_perm_info.id = pg_catalog.svv_table_info.table_id
) AS percentage_result
ORDER BY
  percentage_result.sort_percentage,
  percentage_result.schemaname,
  percentage_result.tablename;

 クエリの同時実行数を変更する

-- クエリの同時実行数を10に変更
set wlm_query_slot_count to 10; 

Posted on 2018-10-06

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3