Schemas:

ACCOUNT_USAGE

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

query_id

VARCHAR

Internal/system-generated identifier for the SQL statement.

parent_query_id

VARCHAR

Query ID of the parent query or NULL if the query does not have a parent.

root_query_id

VARCHAR

Query ID of the topmost query in the chain or NULL if the query does not have a parent.

warehouse_id

NUMBER

Internal/system-generated identifier for the warehouse that the query was executed on.

warehouse_name

VARCHAR

Name of the warehouse that the query executed on.

query_hash

VARCHAR

The hash value computed based on the canonicalized SQL text.

query_parameterized_hash

VARCHAR

The hash value computed based on the parameterized query.

query_tag

VARCHAR

Query tag set for this statement through the QUERY_TAG session parameter.

user_name

VARCHAR

User who issued the query.

start_time

TIMESTAMP_LTZ

Time when query execution started (in the local time zone).

end_time

TIMESTAMP_LTZ

Time when query execution ended (in the local time zone).

credits_attributed_compute

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.

credits_used_query_acceleration

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 credits_attributed_compute column.

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 credits_attributed_compute column 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 cost per query is the warehouse credit usage for executing the query. This cost does not 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;
Copy

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.

  1. 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;
    
    Copy

    For more information, see Example: Ancestor queries with stored procedures.

  2. 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);
    
    Copy

Additional examples¶

For more examples, see Cost attribution by tag, user, and query.