query-optimization

  • A test that asserts on a query count

    An N+1 does not fail a test — the page renders correctly, just slowly — so the regression ships and is found by a customer three releases later.…

  • LAG and LEAD replace a self join on the same table

    Comparing a row with the previous one — time between orders, change since the last reading — was a self join on a correlated subquery finding the preceding…

  • A CTE is not a temporary table, and the plan says so

    A common table expression reads like a named intermediate result, and in MySQL 8.0 it may be merged into the outer query or materialised into a temporary table…

  • EXPLAIN ANALYZE tells you what happened, not what might

    EXPLAIN shows the plan the optimiser chose and its estimates, and the estimates are frequently wrong — a query planned for 40 rows that reads 400,000 looks fine…

  • The N+1 that does not appear in the query log

    340 queries on a page showing twenty rows, and none of them are slow. Every one is under the threshold, which is why the slow log has nothing…

  • A window frame defaults to something you did not intend

    A window with ORDER BY and no frame clause defaults to everything from the start of the partition to the current row, so SUM gives a running total…

  • wc_get_orders instead of a WP_Query on shop_order

    Querying orders with WP_Query couples the code to orders being posts, which is the thing WooCommerce spent 3.0 making replaceable. It returns order objects rather than posts, so…

  • Walk a category tree with a recursive CTE

  • Window functions replace a habit

    8.0 went GA in April and the correlated subquery can retire. The plans before and after, the recursive CTE that replaces a loop, and the upgrade nobody expects.

  • Descending indexes are real in 8.0

    Earlier versions accepted DESC in an index definition and ignored it, which is worse than rejecting it because the query looked optimised and was doing a filesort. It…