SQL DROP INDEX
Removes an index and its space. The syntax split is real: two engines say which table the index is on, three do not, and every engine but MySQL refuses to drop the index that backs a key constraint.
- カテゴリ
- インデックス
- In this category
- 2 of 2
構文
DROP INDEX index_name ON table_name -- SQL Server, MySQL
DROP INDEX [IF EXISTS] index_name -- SQLite, Oracle 23c+
DROP INDEX [CONCURRENTLY] [IF EXISTS] index_name -- PostgreSQL
Each block is a separate form. The support table and the examples show which engine takes which.
エンジンサポート
| エンジン | Support | メモ |
|---|---|---|
| SQL Server | はい | DROP INDEX name ON table; IF EXISTS from 2016 |
| PostgreSQL | はい | CONCURRENTLY drops without blocking writes; schema-wide names |
| MySQL | はい | DROP INDEX name ON table, or ALTER TABLE t DROP INDEX name |
| SQLite | はい | IF EXISTS; index names are database-wide |
| Oracle | Different syntax | constraint-backed indexes refuse: drop or disable the constraint instead |
例
PostgreSQL
DROP INDEX CONCURRENTLY IF EXISTS idx_orders_placed;
SQL Server
DROP INDEX IF EXISTS idx_orders_placed ON dbo.orders;
MySQL
DROP INDEX idx_orders_placed ON orders;
SQLite
DROP INDEX IF EXISTS idx_orders_placed;
Oracle
DROP INDEX idx_orders_placed;
知っておくと便利なこと
- MySQL and SQL Server need the table name in the statement (DROP INDEX idx ON t); PostgreSQL, SQLite and Oracle do not, because their index names are already unique in the schema or database.
- The index behind a PRIMARY KEY or UNIQUE constraint belongs to the constraint on SQL Server (error 3723), PostgreSQL ("constraint ... requires it"), SQLite ("index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped") and Oracle. DROP INDEX fails; drop the constraint (Oracle also lets you disable it) and the index goes with it. MySQL is the exception: DROP INDEX on a UNIQUE key, or on PRIMARY, removes the constraint too.
- PostgreSQL's DROP INDEX CONCURRENTLY, like CREATE INDEX CONCURRENTLY, keeps reads and writes flowing while the index is removed, at the cost of taking longer, being unusable inside a transaction block, and dropping only one index per statement with no CASCADE.
- IF EXISTS arrived in SQL Server 2016 and Oracle 23c; PostgreSQL and SQLite have had it for years. MySQL still has none, so MySQL scripts either catch the error or check information_schema.statistics first.
Questions about DROP INDEX
How do I drop an index without blocking queries?
PostgreSQL's DROP INDEX CONCURRENTLY is the clean option. MySQL's InnoDB drops secondary indexes online. Oracle's and SQL Server's drops of a nonclustered index are quick; dropping a SQL Server clustered index turns the table into a heap and rebuilds its nonclustered indexes, so use WITH (ONLINE = ON) there. SQLite blocks, so drop in a quiet window.
What happens to queries that used the index?
They keep running, just slower: the planner falls back to another index or a table scan. Nothing breaks; the cost shows up in the slow-query log days later, which is why it pays to confirm an index is unused before dropping it.
Why can I not drop this index?
It enforces a PRIMARY KEY or UNIQUE constraint. On SQL Server, PostgreSQL, SQLite and Oracle the index belongs to the constraint, so drop the constraint (on Oracle, or disable it) and the index disappears with it. MySQL lets DROP INDEX remove the constraint directly.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.