Skip to content

From a query to something that lasts

Parameterize a query, read its plan, save it, and promote it to a dataset, a notebook or a SQL model with tests and lineage.

Applied ~9 min9 steps

What runs in CodeSplash

You will be able to

  • Use a parameter so a query can be rerun for different values
  • Read Explain and Analyze output well enough to find the slow step
  • Choose between saving a query, a dataset, a notebook and a SQL model

Read first

01

Parameters

Read

Don't copy a query to change one value. Write $name where the value goes, and the tab shows an input for it.

SELECT species, count(*) AS n, round(avg(body_mass), 1) AS mean_mass
FROM palmer_penguins_sample
WHERE island = $island
GROUP BY species;

Type Biscoe in the input that appears above the editor and run it, then try Dream. Values are kept with the tab, and they're bound as values rather than pasted into the text, so a quote in a value can't break your query.

02

Why is it slow?

In the app

Most analytic queries in DuckDB are fast. When one isn't, Explain shows the plan without running the query, and Analyze runs it and reports time per operator. Both appear under the Plan tab.

Read a plan from the bottom up: scans feed filters, joins and aggregates. Look for the operator with most of the time, and for a join whose output has many more rows than its inputs. That's the fan-out from an earlier lesson, showing itself in the plan.

03

Which should you keep?

Predict

Predict first

Your summary table feeds a monthly report, and the source data updates each month. How should you keep it?

Save the querySave as datasetModel
Reveal

A Model. Saving the query keeps the text only, and a saved dataset is a fixed copy that goes stale. A SQL model records what it reads, rebuilds when inputs change, and runs its data tests each time, which is what you want from anything that repeats.

04

Four ways to keep it

In the app

Use the toolbar, on any query you like:

  • Save keeps the text under Saved. History holds everything else.
  • Save as dataset, above the Results rows, makes a versioned copy of the rows.
  • Notebook gives Python or R that loads this result as a data frame.
  • Model saves it as something the warehouse can rebuild.

Try Notebook. The generated code attaches the same warehouse from outside the app, so you can see exactly how to reach your data from anywhere.

05

Models and lineage

Explore

On the Data page, the SQL models tab lists them. Create one from a query with Model, then open it:

  • Preview runs the query with a row limit, without building anything.
  • Run builds it, records a dataset version and runs its tests.
  • Run downstream also rebuilds whatever depends on it.

Model graph shows models and the datasets they read, in dependency order, with column lineage: click a column to see where it came from. When someone asks where a number came from, this answers.

06

Check it against an engine

Explore

An independent check is cheap. Add the tooth growth sample and compute the group means in SQL.

Tooth growth in guinea pigs
SELECT supp, round(avg(len), 2) AS mean_len
FROM tooth_growth_in_guinea_pigs_sample
GROUP BY supp
ORDER BY supp;

Compare them with the t test's means: 20.66 for OJ and 16.96 for VC. Three engines now agree.

07

Check your understanding

Checkpoint

Check yourself

What's the advantage of WHERE island = $island over editing the text each time?

Show the answer

Answer: a

Binding also means a value containing a quote can't change your SQL's meaning.

Check yourself

What does a SQL model do that Save as dataset doesn't?

Show the answer

Answer: b

A saved dataset is a snapshot of rows. A model is a recipe the warehouse can rerun, with lineage back to its sources.

Check yourself

A join in a plan has inputs of 1,000 and 50 rows and an output of 40,000. What should you suspect?

Show the answer

Answer: b

A lookup-style join shouldn't produce more rows than its larger input. This is a fan-out.

08

Your next query

Reflect

Which query do you rerun by hand? Give it a parameter this week, or make it a model. What would break first if its source changed, and would you notice?

09

Recap

Recap
  • $name parameters let you rerun a query without editing it.
  • Explain and Analyze show the plan. Look for the time and for rows multiplying.
  • Keep a query as text, rows, code or a model, depending on what comes next.