Local data with DuckDB

功能的

The notebook's own DuckDB workspace: run SQL on local data, materialize results, join across connections, attach files, encrypt it, and manage saved results.

A cell that joins SQL Server revenue with Postgres targets: SQLly asks before copying the two inputs into the notebook's local DuckDB data, runs the join there, and labels where every column came from.

Real recording · typing sped up · Northwind demo data

Open the walkthrough on its own page

Every notebook can keep its own small DuckDB database on your computer: Local (this notebook). Copy any cell's result into it as a named table, bring in CSV, Parquet, JSON or SQLite files, and query those tables together with ordinary SQL, even when they came from different databases. It's how a notebook joins SQL Server orders with Postgres targets without a linked server, and how transforms run without sending anything back to the database.

Run a SQL cell on local data

Open a SQL cell's ⋯ menu and choose Run on Local Data (或者 Notebook: Run Cell on Local Data in the palette); Run on Its Connection switches it back. The cell then shows a Local data badge instead of an environment, runs DuckDB SQL, and gets IntelliSense for the local tables and their columns. Local cells run through the same grid as any query, with streaming, spill to disk and Cancel.

-- a cell on Local (this notebook)
SELECT r.country, r.revenue, t.target, r.revenue - t.target AS gap
FROM {{revenue}} AS r
JOIN {{targets}} AS t USING (country)
ORDER BY gap

Join across connections

In a local cell, {{name}} means "that cell's result, copied into local data". The cells can be on any connections. When the cell runs, SQLly checks each input: if its copy is missing or out of date (the source cell's SQL changed, or it has a newer result than the copy), it copies it in first, then runs the local SQL.

  1. The first time a notebook copies results from a connection, SQLly asks "Keep copies of results from Storefront Prod on this computer?" and explains where they're kept. You're asked once per connection; a production connection is called out ("This includes a production connection.").
  2. Then it asks "Copy 2 inputs for vs_target into local data?" before each such run, with Copy and Run。勾选 Copy inputs from other connections automatically in this notebook to skip that question for this notebook.
  3. Each input that isn't wholly on screen (cut short by a row limit, kept on disk, or with long values shortened) is fetched again with its query, read-only, so the copy has every row. A cell allowed to change data is never run again for this: it is copied only from its complete result on screen, and otherwise the run stops with "… changes data, so it is not run again to copy its rows. Run it yourself, then copy its complete result."
  4. The cell header shows a chip per input naming its source connection and environment, and under the results one line per input says where it came from, when it was read, how many rows it has, and whether a limit cut it short.
SQLly
Prompt asking to copy two inputs into local data before running the cellPrompt asking to copy two inputs into local data before running the cell
A local cell asks before copying its two inputs, one from SQL Server and one from Postgres.
SQLly
Local data cell joining a SQL Server result with a Postgres resultLocal data cell joining a SQL Server result with a Postgres result
The join ran locally: chips in the header name each input's connection and environment.
SQLly
Provenance lines under a local data cell's resultsProvenance lines under a local data cell's results
Under a local cell's results, each input lists its connection, environment, when it was read and its row count.

A cell that isn't local and reads a cell on another connection stops with "This cell reads … from another connection. Run it on Local (this notebook) to join results from different connections." Its menu offers the switch. Copies never contain passwords or connection details: only the connection's name, environment and database, and an opaque id.

Materialize a result

To keep a copy of one result under a name of your choice, choose Materialize Locally As… in a SQL cell's ⋯ menu (or Notebook: Materialize Cell Locally).

  1. After the per-connection question above (first time only), name the table: "Materialize orders locally as", suggested orders_local.
  2. A whole on-screen result is copied straight away. If only part of it is on screen, SQLly asks "Run orders again to copy every row?" and, with Run and Copy, runs the query again read-only into local data. A cell allowed to change data is never run again to copy it; run it yourself and copy its complete result.
  3. Progress shows in the activity panel ("Copying orders into orders_local: 48,000 rows so far") with 取消.
  4. The source cell's header then says "Materialized as orders_local, 2 min ago, 48,210 rows", plus "partial" if a limit stopped the copy.
SQLly
Prompt asking to keep copies of a connection's results on this computerPrompt asking to keep copies of a connection's results on this computer
The first copy from a connection asks once whether results from it may be kept on this computer.
SQLly
Prompt for the name of the local tablePrompt for the name of the local table
Name the local table; revenue_local is suggested.
SQLly
Cell header showing Materialized as revenue_localCell header showing Materialized as revenue_local
The source cell's header names the local table, its age and its row count.

Attachments: data files and pictures

一个 Attachment cell brings a file into the notebook. Click 选择文件… and pick a CSV (also .tsv and gzip or zstd compressed), Parquet, JSON, NDJSON or SQLite file, or a PNG or JPEG picture.

  • Give the attachment a 表名; for a SQLite file also enter the SQLite table to read.
  • Running a data attachment imports the file into that local table and shows the row count, the column types it detected (columns kept as text are marked), anything that cut the import short, and a preview of the first 1,000 rows. Other cells read it by its name, like any cell: {{segments}} in a local cell reads the table the attachment imports into, even when that table's name differs from the cell's. A 配置文件 of an imported table takes its column statistics from the whole table, not only the preview.
  • The file is copied into the notebook's private folder before DuckDB reads it, and its hash is kept: when the file changes, SQLly notices and imports the new version.
  • A file inside the notebook's folder is stored by its relative path, so the attachment travels with the notebook in Git. A file from anywhere else is stored by its full path and read again only after it has been picked on this computer ("This file is outside the notebook's folder, so only this computer can import it again.").
  • PNG and JPEG pictures are checked by their content and shown in the cell, up to 8 MiB and 8,192 pixels on a side.
  • A Markdown cell shows an attachment's picture with ![caption](attachment:trend), where trend is the attachment cell's name (or id); use ![caption](<attachment:sales chart>) for a name with spaces. The picture is checked the same way, loads without running the attachment cell, and is read only in a trusted notebook. Markdown cells never load pictures from the web, files or anywhere else: those show as a small placeholder. See Pictures in Markdown cells.
SQLly
Markdown cell with an attachment picture and an image placeholderMarkdown cell with an attachment picture and an image placeholder
A Markdown cell showing the trend attachment's picture through attachment:trend, and a web image below it as a placeholder that is never fetched.
SQLly
Attachment cell with an imported CSV file and its previewAttachment cell with an imported CSV file and its preview
An attachment cell imported segments.csv: row count, detected column types and a preview.

The Local data page

The left rail's Local data page shows and manages everything in the notebook's database:

  • How much space is used against the limit ("120 MB of 4 GB used · 6 tables").
  • Encrypt local data, with its status (Encrypted on this computer 或者 Not encrypted): while the notebook has no local tables, turning it on recreates the database encrypted, with a new key kept in your system keychain; turning it off recreates it unencrypted and only then forgets the key. A DuckDB file can't be encrypted in place, so with tables the switch stays as it is and the page says "Encryption can only be switched while this notebook has no local tables. Clear local data first; tables come back when their cells run again.", with Clear Local Data…. The command line opens an encrypted database with the same key. A notebook you haven't trusted has no local data, so it can't switch.
  • Copy inputs from other connections automatically in this notebook,以及 Cells at once per connection (1 to 4, default 2; see Running notebooks).
  • 重新加载, Clear All… (asks first; the cells that made the tables can make them again) and Clear Saved Results (the saved cell results described below).
  • Every table with its rows, columns, size and source. Click one for its full provenance (source, connection, environment, database, the cell and query it came from, when it was read and saved, row count, whether it's complete, checksum, columns kept as text) and 刷新, Export Parquet, Export CSV 或者 删除.
SQLly
Notebook left rail on the Local data pageNotebook left rail on the Local data page
The Local data page: usage against the limit, encryption status, settings, and each table with its source.
SQLly
Local data page with the Encrypt local data switch onLocal data page with the Encrypt local data switch on
Encrypt local data switched on for a notebook with no local tables: the status reads Encrypted on this computer.
SQLly
Local data page explaining that encryption switches only with no local tablesLocal data page explaining that encryption switches only with no local tables
With a local table in the notebook, the switch stays off and the page explains that local data has to be cleared first.

Saved results (the cache)

Separately from local tables, each SQL cell can keep its last result between sessions. In the inspector's Cache 部分:

Always run
Never reuse a result; run every time.
Reuse
The default. Reuse the result while the cell's SQL, inputs and connection are unchanged.
For a time
Reuse it for a number of seconds (3600 suggested). "That doesn't prove the data is unchanged", so such results are labelled as saved, not fresh.
已固定
Keep it until you run this cell on its own; other runs reuse it.

For cells on a connection, choose where the result is kept: Memory only (this session) or Encrypted on disk, and whether to Clear when the connection disconnects. An encrypted result comes back after a restart under "Saved result from 10:32, kept encrypted on this computer. Run the cell to refresh it." Encrypted results are filed under the connection, database, login and safety settings that fetched them, so a result fetched by one login never comes back for another, with the key in your system keychain. Saved results come back when the notebook tab opens, and after you trust it, never for a notebook you haven't trusted. See Security model.

What local SQL can and can't do

Local queries can only read the notebook's own tables. Each local database runs with extensions off (no install, no auto-load, no community extensions), no access to files outside its own exchange folder, its settings locked, and every query inside a read-only transaction. Only reading statements are accepted: SELECT, WITH, VALUES, FROM, TABLE, SHOW, DESCRIBE (或者 DESC), SUMMARIZE, EXPLAIN, PIVOT 和 UNPIVOT. Reading a file with read_csv, COPY … TO, ATTACH, INSTALL, SET 和 CREATE SECRET are all refused, so a notebook from someone else can't use local SQL to read your files.

限制默认
Memory per local database1 GiB
Rows per local table10,000,000 (a copy that hits it is marked partial)
Size per local table2 GiB
Disk per notebook4 GiB
Imported file2 GiB
Rows returned by a local query to a cell100,000
All notebooks' local data together20 GiB; past that, the notebooks used least recently are cleared first (never one that's open)

Local databases live in SQLLY/notebooks/local under your user data folder, one per notebook. Each copied table records its provenance: the connection's name, opaque id, engine, database and environment, the source SQL's hash, the cell and run, when it was read and saved, row counts, column types (and any kept as text), whether it is complete and why not, and a checksum of its rows. Values that don't fit the column type SQLly picked are kept by falling that column back to text rather than lost, and a local query that returns mixed-type values shows them as text instead of failing.

限制

  • The CLI doesn't run Materialize; its local cells make their own copies. It runs data attachments (not pictures) under the same file rules as the app, and a scheduled run reads only files inside the notebook's folder.
  • Arrow IPC files aren't imported (DuckDB would need a downloadable extension); use Parquet. A SQLite file's pending write-ahead log isn't read.
  • On Windows, importing and exporting through long \\?\ paths hasn't been verified yet.
  • Local data isn't available in the browser build.