すべてのプロダクト
Search
ドキュメントセンター

:SortKey を使用する

最終更新日:Mar 19, 2020

SortKey は、ディスク上のファイルに SortKey 順でデータを格納するためのテーブル属性です。SortKey には、次の利点があります。

  • 列ストアの最適化が高速化されます。収集される最小メタ情報と最大メタ情報が重複することはほとんどなく、便利なフィルターとして使用できます。

  • 「order by」や「group by」を使用してデータを並べ替える必要がありません。ディスクから直接読み込まれるデータは、ソート条件に従って並んでいます。

このドキュメントは、SortKey のさまざまな使用方法について説明します。

SortKey の定義

CREATE TABLE コマンドを使用して、SortKey を含む新しいテーブルを定義できます。構文は次のとおりです。

  1. CREATE [[GLOBAL | LOCAL] {TEMPORARY | TEMP}] TABLE table_name (
  2. [ { column_name data_type [ DEFAULT default_expr ] [column_constraint [ ... ]
  3. [ ENCODING ( storage_directive [,...] ) ]
  4. ]
  5. | table_constraint
  6. | LIKE other_table [{INCLUDING | EXCLUDING}
  7. {DEFAULTS | CONSTRAINTS}] ...}
  8. [, ... ] ]
  9. [column_reference_storage_directive [, ] ]
  10. )
  11. [ INHERITS ( parent_table [, ... ] ) ]
  12. [ WITH ( storage_parameter=value [, ... ] )
  13. [ ON COMMIT {PRESERVE ROWS | DELETE ROWS | DROP} ]
  14. [ TABLESPACE tablespace ]
  15. [ DISTRIBUTED BY (column, [ ... ] ) | DISTRIBUTED RANDOMLY ]
  16. [ SORTKEY (column, [ ... ] )]
  17. [ PARTITION BY partition_type (column)
  18. [ SUBPARTITION BY partition_type (column) ]
  19. [ SUBPARTITION TEMPLATE ( template_spec ) ]
  20. [...]
  21. ( partition_spec )
  22. | [ SUBPARTITION BY partition_type (column) ]
  23. [...]
  24. ( partition_spec
  25. [ ( subpartition_spec
  26. [(...)]
  27. ) ]
  28. )

  1. create table test(date text, time text, open float, high float, low float, volume int) with(APPENDONLY=true,ORIENTATION=column) sortkey (volume);

テーブルの並べ替え

コマンドは、次のとおりです。

  1. VACUUM SORT ONLY [tablename]

SortKey の変更

コマンドは、次のとおりです。

  1. ALTER [[GLOBAL | LOCAL] {TEMPORARY | TEMP}] TABLE table_name SET SORTKEY (column, [ ... ] )

:このコマンドは、データを直接並べ替えず、カタログだけを変更します。データを並べ替えるには、VACUUM SORT ONLY コマンドを使用する必要があります。

  1. alter table test set sortkey (high,low);

テーブルを更新 (Insert、Update、Delete など) すると、テーブル内のデータは SortKey 順に並んでいるとはみなされず、クエリでディスクから読み込まれたデータも、SortKey 順に並んでいるとはみなされません。この場合、VACUUM SORT ONLY コマンドを再実行し、テーブル内のデータを再度並べ替える必要があります。