Metadata and the query optimizer
Metadata describes data. The optimizer uses that information to choose an execution plan.
Understand the idea
Metadata is information about data. Object metadata describes table names, columns, data types and permissions. Storage metadata can describe ranges of values and other properties of stored partitions. These descriptions support resolution, access checks and efficient processing.
| Concept | What it means |
|---|---|
| Object metadata | Names, types, objects and permissions. |
| Partition metadata | Value ranges and other storage statistics. |
| Optimizer | Chooses scans, joins, filters and aggregation work. |
| Evidence | EXPLAIN shows a plan; Query Profile shows execution behavior. |
A concrete example
A January revenue query can skip irrelevant date ranges and read only the columns its calculation needs.
Remember: A possible match in a value range does not prove that a matching row exists.
Explain it step by step · full theory
Metadata describes the objects and data
Metadata is information about data. Object metadata describes table names, columns, data types and permissions. Storage metadata can describe ranges of values and other properties of stored partitions. These descriptions support resolution, access checks and efficient processing.
Think of a table containing millions of orders. A catalog entry tells Snowflake what AMOUNT means as a column type. Partition statistics help identify data that could match a date condition. Neither description should be confused with the actual order records returned by a SELECT.
From SQL text to an execution plan
SQL states the requested result. The optimizer chooses an execution plan for obtaining it. It can consider predicates, joins, available statistics and operator costs. A plan describes operations such as scans, filters, joins and aggregations.
For a daily sales total, an efficient plan can eliminate irrelevant date ranges and aggregate only needed columns. For a join, the size and distribution of each input can affect how work is arranged. The precise plan is produced for the actual query and context.
Reason about a simple plan before measuring it
Suppose ORDERS has order_id, order_date, amount and customer_id. The request asks for January revenue by customer. First identify the needed date range and columns. Then explain that rows are grouped by customer and known amounts are summed.
The written order of clauses is not a promise about every physical execution step. Use EXPLAIN to inspect a plan and Query Profile after execution to inspect what happened. An estimate is a planning aid, while a profile records execution behavior.
Use evidence when diagnosing a slow query
A narrow date filter can still scan a lot if value ranges overlap or the expression prevents useful pruning. A join can produce many more rows than expected if keys are duplicated. These are hypotheses to inspect in the plan and profile.
For the shop, ask what one input row means, which rows qualify and how many rows each join can produce. This connects SQL correctness to execution work before considering a warehouse change.
Words used in this lesson
- Metadata
- Descriptions of objects and their data.
- Optimizer
- The component that chooses an execution plan.
- Operator
- A unit of work such as a scan or join.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Metadata and the query optimizer
Describe
Catalog and storage information describe the input.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Describe
Catalog and storage information describe the input.
- Plan
The optimizer selects operations for the requested result.
- Observe
Compare the planned work with execution evidence.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
See a plan without creating a table
EXPLAIN USING TEXT
SELECT COLUMN1 AS CUSTOMER_ID, SUM(COLUMN2) AS TOTAL
FROM VALUES ('A',40),('A',60),('B',25)
GROUP BY COLUMN1;What to expect and why
EXPLAIN describes a plan rather than returning customer totals. Operator names and formatting can vary. This VALUES example introduces planning; it does not demonstrate pruning of stored table micro-partitions. The stored-table example below requires its separately stated source table.
Inspect a plan for an existing authorized table
-- Prerequisite: SALES.PUBLIC.ORDERS exists and your role can read it.
EXPLAIN USING TEXT
SELECT customer_id, SUM(amount) AS january_amount
FROM SALES.PUBLIC.ORDERS
WHERE order_date >= '2025-01-01'::DATE
AND order_date < '2025-02-01'::DATE
GROUP BY customer_id;What to expect and why
Use your own authorized table with these columns, or read the example conceptually. Look for scans, filters and aggregation. The actual plan depends on the table and context, so no invented plan output is supplied.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Explain the roles of object metadata, partition metadata and the optimizer for a January sales query.
Compare your reasoning
Object metadata resolves columns and permissions. Partition metadata helps narrow potential input. The optimizer chooses how the necessary operations execute. Check the resulting plan and profile to evaluate the actual work.