Get the Last Access date for all BigQuery tables in a BigQuery Project

Viewed 1856

I know how to get the date a table was last modified, but not accessed. Is it possible to get the last time a table was read ? Is there query or an API to get this ?

1 Answers

If you have audit logs in BigQuery, you can write a query like this:

WITH tables AS (
  SELECT FORMAT("%s.%s.%s", table.projectId, table.datasetId, table.tableId) table
    , MAX(timestamp) last_access
  FROM (
    SELECT timestamp
      , protopayload_auditlog.servicedata_v1_bigquery.jobCompletedEvent.job.jobStatistics.referencedTables  
    FROM `fh-bigquery.audit.cloudaudit_googleapis_com_data_access_201811*`
  ), UNNEST(referencedTables) table
  GROUP BY 1
)

SELECT * 
FROM tables

enter image description here

Related