USERS 和 QUERY_ HISTORY 视图 (ACCOUNT_ USAGE) 及 QUERY_ HISTORY 函数:新列¶
Attention
This behavior change is in the 2024_08 bundle.
For the current status of the bundle, refer to Bundle History.
When this behavior change bundle is enabled, the USERS and QUERY_HISTORY views and the output of the QUERY_HISTORY function include the following new columns.
USERS 视图 (ACCOUNT_ USAGE)¶
When the user TYPE is SNOWFLAKE_SERVICE, indicating it is a service user, the following new columns provide the service’s database and schema information. Otherwise, these columns are null.
| 列名称 | 类型 | 描述 |
|---|---|---|
| DATABASE_NAME | VARCHAR | 当用户 TYPE 是 SNOWFLAKE_SERVICE 时,它会指定服务的数据库名称;否则就是 NULL。 |
| DATABASE_ID | NUMBER | 当用户 TYPE 是 SNOWFLAKE_SERVICE 时,它会为服务的数据库指定 Snowflake 生成的内部标识符;否则就是 NULL。 |
| SCHEMA_NAME | VARCHAR | 当用户类型为 SNOWFLAKE_SERVICE 时,它会指定服务的架构名称;否则就是 NULL。 |
| SCHEMA_ID | NUMBER | When the user type is SNOWFLAKE_SERVICE, it specifies the internal, Snowflake-generated identifier for the service’s schema; otherwise, it’s NULL. |
QUERY_ HISTORY 视图 (ACCOUNT_ USAGE)¶
When the QUERY_HISTORY USER_TYPE is SNOWFLAKE_SERVICE, indicating the query is executed by a Snowpark Container Services service, the other columns provide the service’s database and schema information.
| 列名称 | 类型 | 描述 |
|---|---|---|
| USER_TYPE | VARCHAR | Specifies the type of the user executing the query. It is the same as the type field on the user entity (see USERS view). If a Snowpark Container Services service executes the query, the user type is SNOWFLAKE_SERVICE. |
| USER_DATABASE_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s database name; otherwise, it’s NULL. |
| USER_DATABASE_ID | NUMBER | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the internal, Snowflake-generated identifier for the service’s database; otherwise, it’s NULL. |
| USER_SCHEMA_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s schema name; otherwise, it’s NULL. |
| USER_SCHEMA_ID | NUMBER | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the internal, Snowflake-generated identifier for the service’s schema, otherwise it’s NULL. |
QUERY_ HISTORY 函数¶
The following new columns are added to the output (see QUERY_HISTORY function). They identify the user type (USER_TYPE). When the USER_TYPE is SNOWFLAKE_SERVICE, the other columns identify the service’s database and schema.
| 列名称 | 类型 | 描述 |
|---|---|---|
| USER_TYPE | VARCHAR | Specifies the type of the user executing the query. It’s the same as the type field on the user entity (see USERS view). If a Snowpark Container Services service executes the query, the user type is SNOWFLAKE_SERVICE. |
| USER_DATABASE_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s database name; otherwise, it’s NULL. |
| USER_SCHEMA_NAME | VARCHAR | When the value in the user_type column is SNOWFLAKE_SERVICE, it specifies the service’s schema name; otherwise, it’s NULL. |
示例
The service’s database and schema information, along with the user name (which, for the SNOWFLAKE_SERVICE user type, is also the service name) can help during debugging. For example, you can write a query to retrieve information about the service that executed the queries. The following are some example queries:
示例 1: 提取一项服务运行的查询。
在 WHERE 子句中:
user_name = '<service-name>'because a service executes queries as the service user, and the service user’s name is the same as the service name.user_type = 'SNOWFLAKE_SERVICE'anduser_name = '<service-name>'retrieve only queries executed by a service.user_database_nameanduser_schema_namenames, for a service user, are the service’s database and schema.
通过查询 information_schema.query_history 函数,您可以获得同样的结果。
在 WHERE 子句中:
user_type = 'SNOWFLAKE_SERVICE'anduser_name = '<service-name>'retrieves only queries executed by a service.user_database_nameanduser_schema_namenames, for a service user, are the service’s database and schema.
示例 2: 获取多项服务运行的查询和相应的服务信息。
The query joins the QUERY_HISTORY and SERVICES views to retrieve information about the queries and services that executed the queries. Note the following:
- For queries run by services, the
query_history.user_nameis the service user’s name, which is the same as the service name. - 该查询使用架构 IDs(而非架构名称)联接视图,以确保您引用相同的架构,因为如果删除并重新创建架构,架构 ID 会发生变化,但名称保持不变。
您可以向查询添加可选的筛选器。例如:
- Filter
query_historyto retrieve only services that executed specific queries. - Filter
servicesto retrieve only queries executed by specific services.
示例 3: 获取每项服务的服务用户信息。
查询在 ACCOUNT_USAGE 架构中联接 SERVICES 和 USERS 视图,以检索服务和服务用户信息。请注意以下事项:
- When a service runs queries, it runs the queries as service user and the service user’s name is the same as the service name. Therefore, you specify the join condition:
users.name = services.service_name. - Service names are unique only within a schema. Therefore, the query specifies the join condition (
users.schema_id = services.service_schema_id) to ensure each service user is matched against the specific service they belong to (and not any other same-named service running in different schemas).
参考:1771