Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Last Access Timestamp

Is there a view we can lookup to see when the table or view was last accessed?

I can only see for the storage modified time but not when the table/view last was accessed.

0 1 158
1 REPLY 1

BigQuery does not provide a direct metadata field for "last accessed" time. However, there are a few methods to determine when a BigQuery table or view was last accessed. Here's are a few approaches:

1. Cloud Audit Logs

Most Reliable Method: Cloud Audit Logs record various actions and events within your Google Cloud projects, including interactions with BigQuery tables and views.

  • Setup: Ensure Cloud Audit Logs are enabled. You'll want to analyze logs for events that indicate data access, such as query executions.

  • Filtering: To find when a table or view was last accessed, filter the logs for query events related to your specific table or view. Look for logs where the resource.labels.table_id and resource.labels.dataset_id match your table or view, and examine the methodName for query-related activities.

2. Information Schema

Approximation Method: The INFORMATION_SCHEMA.JOBS_BY_USER and JOBS_BY_PROJECT views contain information about past queries, which can indirectly indicate when a table was last accessed.

  • Example Query: Unfortunately, there was a mistake in the initial example provided. Instead, you should use these views to analyze the query text for references to specific tables. This method requires parsing the SQL text of queries to identify which tables were accessed, which can be complex and not always straightforward.

3. Custom Scripting

Tailored Solution: Develop custom scripts that utilize the BigQuery API to analyze Cloud Audit Logs or the INFORMATION_SCHEMA views. These scripts can extract information about table access and store it in a dedicated dataset for monitoring and analysis.

  • Flexibility: This approach allows for customized tracking of table access patterns, offering insights tailored to your specific requirements.

Important Notes:

  • Consumption-Based Tracking: The methods mentioned primarily track when tables or views are accessed through queries. Direct modifications (inserts, updates) that do not involve a query reading the data are not captured in this context.

  • No Direct "Last Accessed" Field: BigQuery does not include a built-in feature to directly track the "last accessed" time for tables or views.

  • Applicability: Both Cloud Audit Logs and the INFORMATION_SCHEMA can track queries regardless of their origin (BigQuery console, BI tools, client libraries, etc.), providing a comprehensive view of table access.

While BigQuery does not offer a direct "last accessed" timestamp for tables or views, using Cloud Audit Logs and the INFORMATION_SCHEMA, complemented with custom scripting, can provide a comprehensive understanding of table access patterns. These methods require some setup and analysis but can be invaluable for auditing, monitoring, and optimizing data usage within BigQuery.