LoginSignup
4
2

More than 1 year has passed since last update.

BigQuery Viewの参照状況を調べる方法

Last updated at Posted at 2022-12-05

以下の記事の手法を使うとBigQueryの特定のテーブルを参照しているクエリを見つけることができます。
しかし、この方法はVIEWに対して使うことができません。
VIEWの参照状況はJOBS_BY_ORGANIZATIONSには格納されず、その代わりにVIEW経由で参照されているテーブルがreferrenced_tablesに格納されています。

では、VIEWに対する参照状況を調べるにはどうしたら良いのでしょうか?
監査ログにはVIEWを参照した情報が格納されているので、これを活用します。

まずは、以下の記事を参考にし、監査ログをBigQueryに保存します。

その後、以下のSQLを実行するとVIEWの参照を行っているクエリ一覧を取得できます。

select
  protopayload_auditlog.authenticationInfo.principalEmail as email,
  resource.labels.project_id,
  timestamp,
  protopayload_auditlog.servicedata_v1_bigquery.jobGetQueryResultsResponse.job.jobStatistics.referencedViews as referenced_views,
  protopayload_auditlog.servicedata_v1_bigquery.jobGetQueryResultsResponse.job.jobConfiguration.query.query,
from `<プロジェクトID>.audit_log.cloudaudit_googleapis_com_data_access_*`
where
protopayload_auditlog.serviceName = "bigquery.googleapis.com" and
protopayload_auditlog.methodName = "jobservice.getqueryresults" and
array_length(protopayload_auditlog.servicedata_v1_bigquery.jobGetQueryResultsResponse.job.jobStatistics.referencedViews) <> 0
4
2
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
4
2