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: Handler_read_rnd_next shows when a database query reads the whole table

Database
~/tips/tip-database-handler-read-rnd-next-full-scan

SHOW SESSION STATUS LIKE 'Handler_read%';

Counts the rows a query actually read, so full table scans stand out right away.

EXPLAIN gives a plan, but sometimes I just want to know how much work one query did. The handler counters tell you, per session.

FLUSH STATUS;
SELECT * FROM orders WHERE total > 5;
SHOW SESSION STATUS LIKE 'Handler_read%';

FLUSH STATUS resets the session counters, then you run the query and read them back. The interesting one is Handler_read_rnd_next: it counts rows read one by one in data file order, which is what a full table scan does. A filter on a column without an index makes the counter one higher than the number of rows in the table: every row, plus one read that hit the end.

If the query used an index, Handler_read_key and Handler_read_next go up instead. So a big rnd_next for a query that returns three rows is a strong hint that an index is missing.

The counters are for your session only and they add up everything you ran since the flush. Run the steps in this exact order, in one session, with nothing in between. FLUSH STATUS needs the RELOAD privilege. A global look with SHOW GLOBAL STATUS mixes all clients together, which is fine for a trend but useless for one query.

Further reading: Server status variables

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