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: SHOW EXPLAIN shows the plan of a query that is still running in MariaDB

Mariadb
~/tips/tip-mariadb-show-explain-for

SHOW FULL PROCESSLIST;

Reveals how a slow query is executing without waiting for it to finish.

A query has been running for four minutes. Running EXPLAIN on a copy might give a different plan than the one actually in use. You can ask about the live one.

Find the thread ID first:

SHOW FULL PROCESSLIST;

Then ask for the plan of that connection, using its number from the Id column:

SHOW EXPLAIN FOR 11;

The output looks like ordinary EXPLAIN: tables in join order, the access type, the index chosen and the estimated rows. A type of ALL on a big table with no key is usually the answer to “why is this so slow”.

There is a stronger variant that also reports what has happened so far:

SHOW ANALYZE FOR 11;

Its r_rows columns hold measured values up to that moment. If the estimate says a few hundred and the real figure is in the millions, the optimizer is working from stale or misleading statistics.

Both read the other thread’s work without stopping it. Seeing other users’ queries needs the right privileges, so run this as an administrator. The thread may also be between statements or doing something with no plan, in which case you get an error and can simply try again. Once you have the plan, I’d decide whether to let it finish or end it with KILL QUERY, which stops the statement and keeps the connection.

Further reading: SHOW EXPLAIN documentation

More Mariadb tips ALTER TABLE t ADD COLUMN z INT INVISIBLE; CREATE SEQUENCE s START WITH 100 INCREMENT BY 10; CREATE OR REPLACE TABLE scratch ( mariadb --init-command="SET SESSION sql_mode='STRICT_ALL_TABLES'" mydb mariadb-admin -u root -p extended-status --relative --sleep=10 \ All Mariadb tips (15) All topics

Link