跳转到内容

Compare schemas and read query plans

此内容尚不支持你的语言。

Two tools answer “what is different?” and “why is this slow?”. Schema comparison lists what differs between two schemas and writes a migration script you review and run yourself. The query plan shows the database’s execution plan for one statement, estimated or measured. Schema comparison with a migration script and the query plan: Works.

  1. In the Database tool window, right-click the connection (or a schema) to compare from, and choose Compare Schemas…. The same is in Tools › Database.
  2. Popups ask for the source schema (when there is more than one), the target connection and the target schema. Only connections of the same system are offered; your own connection is listed first as “shop · other schema” (with its name) to compare two schemas of one database (PostgreSQL, DuckDB, MySQL and MariaDB).
  3. The Schema Diff tab opens in the Services tool window. Its head line names the direction: the target becomes like the source. Swap turns it around.

The list shows each table as + (only in the source), − (only in the target) or ~ (different), with the differing columns, primary key, indexes, foreign keys and constraints indented under it, as “~ old → new”. Views are compared too. Equal schemas say “No differences”.

What is compared: tables, columns (type, nullability, default), primary keys, indexes and foreign keys (matched by what they cover, not by name), views; on PostgreSQL also check and unique constraints and storage parameters.

To compare one table or view with another, right-click it and choose Compare With….

Each statement has a checkbox. Statements that lose data are marked “data loss” and start unchecked. Checking one asks first and names what would be lost, for example “drop table item: all its rows are lost”; Include (↩) or Cancel (Esc). Tools › Database › Include Destructive Changes… checks all of them after one question. ↑ ↓ and Space work in the list.

Click Open Script (Tools › Database › Open Migration Script). The script is written to .ferris-idea/migrations/<source>-to-<target>.sql, opens in the editor and is bound to the target connection. Read it, then run it like any SQL file with ⌘↩. The comparison itself never changes a database.

The script runs in dependency order: drop views, foreign keys, indexes and tables, create tables, alter columns, create indexes, add foreign keys, create views. On PostgreSQL, SQLite and SQL Server it is wrapped in BEGIN … COMMIT; on MySQL, MariaDB and Oracle, where DDL commits on its own, a comment says it cannot be rolled back. Its first statement pins the target schema (SET LOCAL search_path on PostgreSQL, USE on MySQL and MariaDB). SQLite tables that need a change are rebuilt between PRAGMA foreign_keys = OFF and ON.

Triggers, routines, sequences, partitions and exclusion constraints are not compared; a renamed column shows as one removed and one added; MySQL and SQL Server table options are not compared; systems cannot be mixed. There is no migration framework: no Flyway or Liquibase changesets and no history of applied migrations. The script is plain SQL that you keep in the project if you like.

  1. Put the caret in a statement in a console or a bound .sql file.
  2. Right-click and choose Explain Plan, or Explain Analyze to measure it. Both are in Tools › Database as well.
  3. The Plan tab opens in the Services tool window.

The plan has four views; ⌥R switches between them:

  • Tree (the default): one row per step with operation, table or index, conditions, estimated rows and cost; a measured plan adds the actual rows, time in milliseconds and loops. Full scans are shown in the warning colour.
  • Diagram: boxes with operation, object and share of the cost. Fit and 100 % set the zoom; Fit does not go below a readable 60 % when the plan opens.
  • Flame: the steps by Total cost, Startup cost or, for a measured plan, Time.
  • Raw: the plan as the database returned it.

Below the view, the selected step lists every field the database reported, its share, and hints: a full scan of an object with at least 1,000 rows, or an estimate off by a factor of 10 or more. Several hints fold into “2 more hints ▾” (⌥H). ⌘C copies the plan.

Explain Analyze runs the statement. It runs inside BEGIN … ROLLBACK, also for a query, and the tab says “Measured; the statement’s changes were rolled back”. Statements that change the schema are never measured. SQLite has no costs and refuses Analyze. MySQL tables with the MyISAM or MEMORY engine keep their changes despite the rollback; a hint says so.

Plans come from PostgreSQL’s JSON plan, MySQL’s FORMAT=TREE and SQLite’s EXPLAIN QUERY PLAN. One statement at a time; a selection that is not a whole statement is ignored and the statement at the caret is explained.

compare_schemas returns the differences and the script and never runs it; it needs read-schema on both connections. explain_sql returns the plan tree; with analyze: true it also needs the permission the statement itself needs, because it runs it.

  • compare_schemasreadssshdatabase-permissiontrusted-projectCompare the schemas of two connections of one dialect, or two schemas of one connection (source_schema/target_schema; PostgreSQL, DuckDB, MySQL, MariaDB): differences in tables, columns, keys, indexes, foreign keys and views, and the statements that turn the target into the source in dependency order.
  • explain_sqlchangessshdatabase-permissiontrusted-projectShow the execution plan of one statement as a tree (operation, table/index, conditions, estimated rows and cost; with analyze=true also actual rows, time and loops) plus hints such as full scans of big tables and misestimates.
  • generate_ddlreadssshdatabase-permissiontrusted-projectGenerate the CREATE script (tables, primary keys, indexes, foreign keys) of a connection's schema or of selected tables; nothing is run (needs the read-schema permission).