View a markdown version of this page

Querying S3 Storage Lens data with analytics tools - Amazon Simple Storage Service

Querying S3 Storage Lens data with analytics tools

Before you can query S3 Storage Lens data exported to S3 Tables using AWS analytics services like Amazon Athena or Amazon EMR, you must enable analytics integration on the AWS-managed aws-s3 table bucket and configure AWS Lake Formation permissions.

Important

Enabling analytics integration on the "aws-s3" table bucket is a required step. Without this configuration, you will not be able to query your S3 Storage Lens tables using AWS analytics services.

Prerequisites

Before you begin, ensure that you have:

  • An S3 Storage Lens configuration with S3 Tables export enabled. For more information, see Exporting S3 Storage Lens metrics to S3 Tables.

  • Access to Amazon Athena or another analytics service.

  • Waited up to 48 hours after enabling export for the first data to be available.

Integration overview

For detailed information about integrating S3 Tables with AWS analytics services, including prerequisites, IAM role configuration, and step-by-step procedures, see Integrating Amazon S3 Tables with AWS analytics services.

After you enable S3 Tables export and set up analytics integration, you can query your S3 Storage Lens data using AWS analytics services such as Amazon Athena, Amazon Redshift, and Amazon EMR. This enables you to perform custom analysis, create dashboards, and derive insights from your storage data using standard SQL.

Querying with Amazon Athena

Amazon Athena is a serverless interactive query service that makes it easy to analyze data using standard SQL. Use the following steps to query S3 Storage Lens data in Athena.

Note

In all query examples, replace s3tables_lens_my_config_exp with your actual S3 Storage Lens configuration namespace. For more information about namespace naming, see Table naming for S3 Storage Lens export to S3 Tables.

Example: Query top storage-consuming tables

The following query identifies the top 10 tables by storage consumption from the storage_metrics system table:

SELECT table_bucket_name, table_namespace, table_name, storage_bytes / POWER(1024, 3) AS storage_gb, table_file_count FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."storage_metrics" WHERE report_time = ( SELECT MAX(report_time) FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."storage_metrics" ) AND record_type = 'TABLE' ORDER BY storage_gb DESC LIMIT 10

Example: Analyze storage growth over time

The following query analyzes account-level storage growth over the last 30 days by using the report_time trend:

SELECT CAST(report_time AS date) AS report_date, SUM(storage_bytes) / POWER(1024, 3) AS total_storage_gb FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."storage_metrics" WHERE report_time >= current_date - interval '30' day AND record_type = 'ACCOUNT' GROUP BY CAST(report_time AS date) ORDER BY report_date DESC;

Example: Identify tables with large non-current storage

Non-current files are files that are no longer referenced by the current snapshot but are retained per the table's removal policies. Tables with a large share of non-current storage are candidates for unreferenced file removal and snapshot management. The following query finds the tables where non-current files account for the most storage:

SELECT table_bucket_name, table_namespace, table_name, non_current_file_storage_bytes / POWER(1024, 3) AS non_current_gb, non_current_file_count, storage_bytes / POWER(1024, 3) AS total_gb FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."storage_metrics" WHERE report_time = ( SELECT MAX(report_time) FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."storage_metrics" ) AND record_type = 'TABLE' AND non_current_file_storage_bytes > 0 ORDER BY non_current_gb DESC LIMIT 20;

Example: Find tables with small-file fragmentation

Tables with a high proportion of small data files are candidates for compaction. The following query uses the live data file size-bucket columns in the table_health_metrics system table to find the tables with the most data files smaller than 128 KB relative to their total live data file count. This query uses a fixed 128 KB threshold as a simple approximation; the dashboard's small data file percentage instead counts files below each table's compaction target file size.

SELECT table_bucket_name, table_namespace, table_name, live_data_file_0kb_to_128kb_count AS small_file_count, live_data_file_count, ROUND(100.0 * live_data_file_0kb_to_128kb_count / NULLIF(live_data_file_count, 0), 2) AS small_file_percentage FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."table_health_metrics" WHERE report_time = ( SELECT MAX(report_time) FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."table_health_metrics" ) AND record_type = 'TABLE' AND live_data_file_count > 0 ORDER BY small_file_percentage DESC, small_file_count DESC LIMIT 20;

Example: Analyze request patterns

The following query analyzes request patterns in the activity_metrics system table to understand table access frequency. Activity metrics are an advanced-tier metric and are available only when the activity metrics category is enabled for your configuration.

SELECT table_bucket_name, table_namespace, table_name, SUM(all_request_count) AS total_requests, SUM(get_request_count) AS get_requests, SUM(put_request_count) AS put_requests, ROUND(100.0 * SUM(get_request_count) / NULLIF(SUM(all_request_count), 0), 2) AS get_percentage, SUM(downloaded_bytes) / POWER(1024, 3) AS downloaded_gb FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."activity_metrics" WHERE report_time >= current_date - interval '7' day AND record_type = 'TABLE' GROUP BY table_bucket_name, table_namespace, table_name HAVING SUM(all_request_count) > 0 ORDER BY total_requests DESC LIMIT 10;

Querying with Apache Spark on Amazon EMR

Amazon EMR provides a managed Hadoop framework that makes it easy to process vast amounts of data using Apache Spark. You can use the Iceberg connector to read S3 Storage Lens tables directly.

Read S3 Tables with Spark

Use the following Python code to read S3 Storage Lens data with Spark. This example assumes an Amazon EMR Iceberg cluster that connects to the AWS managed table bucket that holds your exported metrics. For the cluster and Spark session setup, see Connecting to S3 table buckets with Spark on an Amazon EMR Iceberg cluster.

from pyspark.sql import SparkSession spark = SparkSession.builder \ .appName("S3StorageLensAnalysis") \ .config("spark.sql.extensions", "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions") \ .config("spark.sql.catalog.s3tablescatalog", "org.apache.iceberg.spark.SparkCatalog") \ .config("spark.sql.catalog.s3tablescatalog.catalog-impl", "org.apache.iceberg.aws.glue.GlueCatalog") \ .config("spark.sql.catalog.s3tablescatalog.glue.id", "111122223333:s3tablescatalog/aws-s3") \ .config("spark.sql.catalog.s3tablescatalog.client.region", "us-east-1") \ .getOrCreate() # Read S3 Storage Lens for Tables storage metrics df = spark.read \ .format("iceberg") \ .load("s3tablescatalog.s3tables_lens_my_config_exp.storage_metrics") # Analyze storage by table df.filter("record_type = 'TABLE'") \ .groupBy("table_bucket_name", "table_namespace", "table_name") \ .sum("storage_bytes") \ .orderBy("sum(storage_bytes)", ascending=False) \ .show(10)

Query optimization best practices

Follow these best practices to optimize query performance and reduce costs:

  • Filter by report_time – Always include date filters to reduce the amount of data scanned. This is especially important for tables with long retention periods.

    WHERE report_time >= current_date - interval '7' day
  • Use record_type filters – Specify the appropriate aggregation level (ACCOUNT, BUCKET, NAMESPACE, TABLE, or PARTITION) to query only the data that you need.

    WHERE record_type = 'TABLE'
  • Include LIMIT clauses – Use LIMIT for exploratory queries to control result size and reduce query costs.

    LIMIT 100
  • Filter empty records – Use conditions to exclude empty or zero-value records.

    WHERE storage_bytes > 0
  • Use the latest data – When analyzing current state, filter for the most recent report_time to avoid scanning historical data.

    WHERE report_time = (SELECT MAX(report_time) FROM table_name)

Example optimized query pattern

The following query demonstrates best practices for optimization:

SELECT table_bucket_name, table_namespace, table_name, storage_bytes / POWER(1024, 3) AS storage_gb FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."storage_metrics" WHERE report_time >= current_date - interval '7' day -- Date filter AND record_type = 'TABLE' -- Record type filter AND storage_bytes > 0 -- Non-empty filter ORDER BY storage_gb DESC LIMIT 100; -- Result limit

Troubleshooting

Query returns no results

Problem: Your query completes successfully but returns no results.

Solution:

  • Verify that data is available by checking the latest report_time:

    SELECT MAX(report_time) AS latest_data FROM "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"."storage_metrics";
  • Ensure that you're using the correct namespace name. Use SHOW TABLES IN "s3tablescatalog/aws-s3"."s3tables_lens_my_config_exp"; to list available tables.

  • Wait up to 48 hours after enabling S3 Tables export for the first data to be available.

Access denied errors

Problem: You receive access denied errors when running queries.

Solution: Verify that AWS Lake Formation permissions are correctly configured. For more information, see Integrating Amazon S3 Tables with AWS analytics services.

Next steps