- Schemas:
QUERY_ATTRIBUTION_HISTORY view¶
This Account Usage view can be used to determine the compute cost of a given query run on warehouses in your account in the last 365 days (1 year).
For more information, see Attribute cost for queries.
Columns¶
Column name |
Data type |
Description |
---|---|---|
|
VARCHAR |
Internal/system-generated identifier for the SQL statement. |
|
VARCHAR |
Query ID of the parent query or NULL if the query does not have a parent. |
|
VARCHAR |
Query ID of the topmost query in the chain or NULL if the query does not have a parent. |
|
NUMBER |
Internal/system-generated identifier for the warehouse that the query was executed on. |
|
VARCHAR |
Name of the warehouse that the query executed on. |
|
VARCHAR |
The hash value computed based on the canonicalized SQL text. |
|
VARCHAR |
The hash value computed based on the parameterized query. |
|
VARCHAR |
Query tag set for this statement through the QUERY_TAG session parameter. |
|
VARCHAR |
User who issued the query. |
|
TIMESTAMP_LTZ |
Time when query execution started (in the local time zone). |
|
TIMESTAMP_LTZ |
Time when query execution ended (in the local time zone). |
|
NUMBER |
Number of credits attributed to this query. Includes only the credit usage for the query execution and doesn’t include any warehouse idle time. |
|
NUMBER |
Number of credits consumed by the Query Acceleration Service to accelerate the query. NULL if the query is not accelerated. . . The total cost for an accelerated query is the sum of this column and the |
Usage notes¶
Latency for this view can be up to six hours.
This view displays results for any role granted the USAGE_VIEWER or GOVERNANCE_VIEWER database role.
The value in the
credits_attributed_compute
column contains the warehouse credit usage for executing the query, inclusive of any resizing and/or autoscaling of multi-cluster warehouse(s). This cost is attributed based on the weighted average of the resource consumption.The value doesn’t include any credit usage for warehouse idle time. Idle time is a period of time in which no queries are running in the warehouse and can be measured at the warehouse level.
The value doesn’t include any other credit usage that is incurred as a result of query execution. For example, the following are not included in the query cost:
Data transfer costs
Storage costs
Cloud services costs
Costs for serverless features
Costs for tokens processed by AI services
For queries that are executed concurrently, the cost of the warehouse is attributed to individual queries based on the weighted average of their resource consumption during a given time interval.
Short-running queries (<= ~100ms) are currently too short for per query cost attribution and are not included in the view.
Data for all columns is available starting from mid-August, 2024. Some data prior to this date might be available in the view, but might be incomplete.
Examples¶
Query costs for the current user¶
To determine the costs of queries executed by the current user for the current month, execute the following statement:
SELECT user_name, SUM(credits_attributed_compute) AS credits
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_ATTRIBUTION_HISTORY
WHERE user_name = CURRENT_USER()
AND start_time >= DATE_TRUNC('MONTH', CURRENT_DATE)
AND start_time < CURRENT_DATE
GROUP BY user_name;
To attribute warehouse cost to the current user, see User-based attribution of warehouse cost.
Query costs for stored procedures¶
For stored procedures that issue multiple hierarchical queries, you can compute the attributed query costs for the procedure by using the root query ID for the procedure.
To find the root query ID for a stored procedure, use the ACCESS_HISTORY view. For example, to find the root query ID for a stored procedure, set the
query_id
and execute the following statements:SET query_id = '<query_id>'; SELECT query_id, parent_query_id, root_query_id, direct_objects_accessed FROM SNOWFLAKE.ACCOUNT_USAGE.ACCESS_HISTORY WHERE query_id = $query_id;
For more information, see Example: Ancestor queries with stored procedures.
To sum the query cost for the entire procedure, replace
<root_query_id>
and execute the following statements:SET query_id = '<root_query_id>'; SELECT SUM(credits_attributed_compute) AS total_attributed_credits FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_ATTRIBUTION_HISTORY WHERE (root_query_id = $query_id OR query_id = $query_id);
Additional examples¶
For more examples, see Cost attribution by tag, user, and query.