brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: EXPLAIN QUERY PLAN shows whether a SQLite database query uses an index

Database
~/tips/tip-database-sqlite-explain-query-plan

sqlite3 app.db "EXPLAIN QUERY PLAN SELECT * FROM users WHERE email = '[email protected]';"

Shows whether SQLite reads a whole table or uses an index for a given query.

SQLite has no big tuning tools, but one command already tells me if a query is scanning the whole table.

sqlite3 app.db "EXPLAIN QUERY PLAN SELECT * FROM users WHERE email = '[email protected]';"

The output has one line per step. SCAN users means every row is read, while SEARCH users USING INDEX idx_email (email=?) means an index is used. If you see SCAN on a column you filter on often, create an index with CREATE INDEX idx_email ON users(email) and run the plan again.

The planner only uses an index when the query can use it. Wrapping the column in a function, for example lower(email), makes it fall back to a scan unless the index is on that same expression.

Further reading: SQLite: EXPLAIN QUERY PLAN

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link