LoginSignup
11
6

More than 3 years have passed since last update.

BigQueryでoffsetを指定するただ一つだけの方法

Last updated at Posted at 2015-06-15

##はじめに##

BigQueryでOffsetを指定する方法を紹介します。

公式のリファレンスにはOffsetに関する記載がありません。
https://cloud.google.com/bigquery/query-reference

##解決方法##

bigquery
SELECT
  repository.url
FROM
  [publicdata:samples.github_nested]
WHERE
  repository.url is NOT NULL
GROUP BY
  repository.url
ORDER BY
  repository.url
LIMIT 100

例えば上記のように100件ずつ取得したい場合

これだけで終了です。

bigquery
SELECT
  repository.url
FROM
  [publicdata:samples.github_nested]
WHERE
  repository.url is NOT NULL
GROUP BY
  repository.url
ORDER BY
  repository.url
LIMIT 100
OFFSET 100

そうですoffsetを普通に指定できるのです…

なんで公式のリファレンスには書いてないんでしょうね…

##検証##

以下は検証結果です。

bigquery
SELECT
  repository.url
FROM
  [publicdata:samples.github_nested]
WHERE
  repository.url is NOT NULL
GROUP BY
  repository.url
ORDER BY
  repository.url
limit 100
offset 0

の結果の100番目は
https://github.com/0x76/opensn0w

bigquery
SELECT
  repository.url
FROM
  [publicdata:samples.github_nested]
WHERE
  repository.url is NOT NULL
GROUP BY
  repository.url
ORDER BY
  repository.url
limit 100
offset 99

の1番目の結果が
https://github.com/0x76/opensn0w

ということでこの指定方法で問題なさそうです。

##追記##

6/16 追記

OFFSETが100万件を超え辺りで

Query Failed
Error: Response too large to return. Consider setting allowLargeResults to true in your job configuration. For more details, see https://cloud.google.com/bigquery/querying-data#largequeryresults

というエラーが出る場合があります。

そんな時は、必要がなくても
GROUP EACH BY を利用し取得したいカラムをGROUP BYすることでエラーを回避できます。

関連書籍
Google BigQuery
Google Cloud Platform実践ビッグデータ分析基盤開発

11
6
2

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
11
6