The SQL tool runs real queries against the environment’s database. Use it to answer questions (“orders per day”), find duplicates before adding a unique index, backfill a column, or understand why a query is slow.
SQL editor

Data → SQL: Schema, Saved and History tabs, Allow writes, Query builder, Save, Explain and Run.

In Studio

Data → Tools → SQL. It has:
  • Schema, Saved and History tabs on the left: Schema is the table browser with Find a table; Saved holds saved queries; History your recent runs.
  • An editor that completes table and column names as you type.
  • A builder for people who would rather click: choose a Table, Columns, a Join type, Operator and values, Combine with AND/OR, and Order by. The builder writes the SQL for you.
  • Results below, with No rows. when empty. Long values are truncated; open a row to see everything.
  • Saved queries, shared with your team: give a query a Name (“Orders per day”) and a Description (“What it answers”), and load it later. Delete with the trash action.
  • Show how the database would run this query (explain): the plan, to see whether an index is used.
The buttons above the editor: Allow writes (a checkbox: until it’s ticked, statements that change data are refused), Query builder, Save, Explain, Run. Writes are audited and clear the environment’s caches. ⌘/Ctrl + Enter runs the query (or just the selection); ⌘S saves.

Create tables

The Create tables button at the top creates or extends the tables for every resource at once. It adds only: nothing is dropped or retyped.

Useful queries

Safety habits

  1. Select before you update. Run the SELECT with the same WHERE, check the count, then change SELECT to UPDATE.
  2. Take a backup before a large change. See Backups.
  3. Writes bypass access rules and events at the SQL level. If you need events to fire, use the table editor, an import, or enable Capture changes made outside the API on the resource (Events and realtime).
  4. Run risky statements in development first.

Reading a plan

Run explain on a slow query:
  • Index scan / index only scan: good: the index is used.
  • Sequential scan on a big table: no usable index. Add one that matches the WHERE and ORDER BY (Indexes).
  • A sort on millions of rows: add an index that already has the order.

Table editor

Edit single rows.

Indexes

Speed up what explain shows.