

# Querying the usage system tables

After the first snapshot arrives, you can query the usage system tables with Amazon Athena or another Iceberg-compatible engine. Turning on system table integration creates the tables, but it doesn't register them with the AWS Glue Data Catalog or give anyone read access.

If you use only the Domain and project usage dashboard, and the admin project's role is an AWS Lake Formation administrator, you don't need these steps, because the dashboard handles its own read access. For other readers, and for your own queries, complete the following steps.

**Note**  
Every snapshot contains the full inventory of the domain. A query without a `snapshot_time` filter counts each entity once for each snapshot. Always filter to the snapshot or date range that you need.

## Prerequisites
<a name="usage-system-tables-query-prereqs"></a>
+ System table integration is on for the domain, and at least one snapshot has arrived. For more information, see [Turning on usage system tables for a domain](usage-system-tables-enable.md).

## To query the usage system tables
<a name="usage-system-tables-query-steps"></a>

1. Integrate Amazon S3 Tables with the AWS Glue Data Catalog, if your account isn't already integrated. This one-time setup for each account and Region is separate from turning on system table integration. It registers the `s3tablescatalog` catalog with the Data Catalog, which lets Athena and other engines see the tables. For instructions, see [Integrating Amazon S3 Tables with AWS analytics services](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-integrating-aws.html) in the *Amazon S3 User Guide*.

1. Give read access to the role that runs your queries, using the method that governs the `s3tablescatalog` catalog in your account:
   + **AWS Lake Formation**: Grant `DESCRIBE` on the domain's database (namespace), and `DESCRIBE` and `SELECT` on its tables. A Lake Formation data lake administrator can see table metadata but can't read data until granted `SELECT`. For a project in Amazon SageMaker Unified Studio, grant access to the project's user role, `datazone_usr_role_my-project-id_my-environment-id`. You can find this role in the project's `project.iam` connection.
   + **IAM**: Give the role AWS Glue read permissions on the catalog, database, and tables. Also give it these S3 Tables actions on the table bucket: `s3tables:GetTableBucket`, `s3tables:ListNamespaces`, `s3tables:GetNamespace`, `s3tables:ListTables`, `s3tables:GetTable`, `s3tables:GetTableMetadataLocation`, and `s3tables:GetTableData`.

1. Run your queries against the catalog `s3tablescatalog/aws-sagemaker-unified-studio`, using the domain's namespace as the database.

## Expected results
<a name="usage-system-tables-query-results"></a>

Your queries return one row for each entity or user profile in each snapshot in the range that you filter on. To analyze several domains, combine their namespaces in one query. To analyze across accounts, share the catalog with AWS Glue Data Catalog cross-account sharing.

## Example – Grant Lake Formation read access to one domain
<a name="usage-system-tables-query-example-lf"></a>

The following AWS Command Line Interface (AWS CLI) commands grant a query role read access to one domain's usage system tables. Replace {{111122223333}} with your AWS account ID, {{my-query-role}} with the role that runs your queries, {{dzd\_1234567890abcd}} with your domain's namespace, and {{us-east-1}} with your Region.

```
aws lakeformation grant-permissions \
    --principal DataLakePrincipalIdentifier=arn:aws:iam::111122223333:role/my-query-role \
    --resource '{"Database": {"CatalogId": "111122223333:s3tablescatalog/aws-sagemaker-unified-studio", "Name": "dzd_1234567890abcd"}}' \
    --permissions DESCRIBE \
    --region us-east-1

aws lakeformation grant-permissions \
    --principal DataLakePrincipalIdentifier=arn:aws:iam::111122223333:role/my-query-role \
    --resource '{"Table": {"CatalogId": "111122223333:s3tablescatalog/aws-sagemaker-unified-studio", "DatabaseName": "dzd_1234567890abcd", "TableWildcard": {}}}' \
    --permissions DESCRIBE SELECT \
    --region us-east-1
```

## Example queries
<a name="usage-system-tables-query-examples"></a>

The following examples use Athena. Replace {{dzd\_1234567890abcd}} with your domain's namespace.

### Example – Count entities by category in the latest snapshot
<a name="usage-system-tables-query-example-count"></a>

The following Athena query counts the entities in each category in the most recent snapshot.

```
SELECT category_enum, COUNT(*) AS entity_count
FROM "s3tablescatalog/aws-sagemaker-unified-studio"."dzd_1234567890abcd"."domain_entity"
WHERE snapshot_time = (
    SELECT MAX(snapshot_time)
    FROM "s3tablescatalog/aws-sagemaker-unified-studio"."dzd_1234567890abcd"."domain_entity"
)
GROUP BY category_enum
ORDER BY entity_count DESC;
```

### Example – Count the blueprints each project uses
<a name="usage-system-tables-query-example-blueprints"></a>

The following Athena query counts the distinct environment blueprints that each project's environments use, in the most recent snapshot.

```
WITH latest AS (
    SELECT * FROM "s3tablescatalog/aws-sagemaker-unified-studio"."dzd_1234567890abcd"."domain_entity"
    WHERE snapshot_time = (SELECT MAX(snapshot_time)
                           FROM "s3tablescatalog/aws-sagemaker-unified-studio"."dzd_1234567890abcd"."domain_entity")
)
SELECT owning_project_id,
       MAX(owning_project_name)         AS project_name,
       COUNT(DISTINCT env_blueprint_id) AS distinct_blueprints
FROM latest
WHERE category_enum = 'ENVIRONMENT'
GROUP BY owning_project_id
ORDER BY distinct_blueprints DESC;
```

### Example – Count user profiles by identity source over the last 30 days
<a name="usage-system-tables-query-example-users"></a>

The following Athena query counts active user profiles for each day of the last 30 days, by how each user authenticates.

```
SELECT DATE(snapshot_time) AS snapshot_date, user_type_enum, COUNT(*) AS users
FROM "s3tablescatalog/aws-sagemaker-unified-studio"."dzd_1234567890abcd"."user_profile"
WHERE snapshot_time >= current_timestamp - INTERVAL '30' DAY
  AND user_status_enum IN ('ASSIGNED', 'ACTIVATED')
GROUP BY DATE(snapshot_time), user_type_enum
ORDER BY snapshot_date, user_type_enum;
```

## Troubleshooting
<a name="usage-system-tables-query-troubleshooting"></a>
+ **Athena can't find the catalog or tables**

  Integrate S3 Tables with the AWS Glue Data Catalog in that account and Region (step 1).
+ **You can see the tables but can't read data**

  Grant `SELECT` on the tables to the role that runs the query (step 2).