#!/usr/bin/env bash # Verification for: optimize-query-guide # Self-contained. Builds its own scratch schema (hw_rw_optq_), loads 500,000 rows, runs the # same EXPLAIN / EXPLAIN ANALYZE sequence the article shows, and re-checks the query-cache-removal # claim against the live server. Prints PASS / FAIL / SKIP per claim, cleans up via EXIT trap. # # Rows are deleted at the end; the empty schema is left behind because this environment blocks # DROP DATABASE outright. The exact manual cleanup command is printed at the end. # # Override the client with: MYSQL_CMD="mysql -u root -p'...' -h 127.0.0.1" ./verify-...sh set -u PASS=0; FAIL=0; SKIP=0; CONTENT=0 ok() { echo "PASS $*"; PASS=$((PASS+1)); } no() { echo "FAIL $*"; FAIL=$((FAIL+1)); } skip() { echo "SKIP $*"; SKIP=$((SKIP+1)); } content() { echo "CONTENT $*"; CONTENT=$((CONTENT+1)); } MYSQL_CMD="${MYSQL_CMD:-mysql -u root}" ROWS="${ROWS:-500000}" DB="hw_rw_optq_$$" SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/hw_rw_optq.XXXXXX")" cleanup() { rm -rf "$SCRATCH"; } trap cleanup EXIT M() { $MYSQL_CMD "$@"; } MD() { $MYSQL_CMD "$DB" "$@"; } echo "=== MySQL query optimization — claim verification + benchmark ===" echo "date: $(date -u '+%Y-%m-%dT%H:%M:%SZ') host: $(uname -srm)" echo if ! M -e 'SELECT 1' >/dev/null 2>&1; then skip "cannot connect with: $MYSQL_CMD — all checks below skipped" echo echo "=== summary: PASS=$PASS FAIL=$FAIL SKIP=1 CONTENT=$CONTENT ===" exit 0 fi VER="$(M -N -B -e 'SELECT VERSION()')" echo " server: MySQL $VER" # --------------------------------------------------------------------------- # CLAIM 1: MySQL 8.0+ has no query_cache_* variables at all (removed, not # just disabled). # --------------------------------------------------------------------------- QC="$(M -N -B -e "SHOW VARIABLES LIKE 'query_cache%'" 2>/dev/null)" case "$VER" in 8.*|9.*) if [ -z "$QC" ]; then ok "C1 no query_cache_* variables exist on MySQL $VER — the query cache was removed in 8.0, not merely disabled" else no "C1 expected no query_cache_* variables on MySQL $VER, found: $QC" fi ;; *) skip "C1 query cache removal check (server is $VER, not 8.0+)" ;; esac # --------------------------------------------------------------------------- # Build the dataset: 500,000 rows, 200 distinct category_id values, no index. # --------------------------------------------------------------------------- echo " building dataset: $ROWS rows, 200 distinct category_id values, no index yet..." M -e "CREATE DATABASE IF NOT EXISTS $DB;" MD -e " CREATE TABLE IF NOT EXISTS products ( id INT NOT NULL AUTO_INCREMENT, sku VARCHAR(20) NOT NULL, category_id INT NOT NULL, price DECIMAL(10,2) NOT NULL, name VARCHAR(100) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB;" MD -e "DELETE FROM products WHERE id > 0;" MD -e " SET SESSION cte_max_recursion_depth = 600000; INSERT INTO products (sku, category_id, price, name) WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM n WHERE x < $ROWS) SELECT CONCAT('SKU-', x), (x % 200) + 1, (x % 5000) / 10.0, CONCAT('Product ', x) FROM n;" MD -e "DROP INDEX idx_products_category ON products;" >/dev/null 2>&1 MD -e "DROP INDEX idx_products_cat_covering ON products;" >/dev/null 2>&1 CNT="$(MD -N -B -e 'SELECT COUNT(*) FROM products')" echo " row count: $CNT" # --------------------------------------------------------------------------- # CLAIM 2: before an index, filtering on category_id is a full table scan # (type=ALL in EXPLAIN). # --------------------------------------------------------------------------- MD --vertical -e "EXPLAIN SELECT id, sku, price, name FROM products WHERE category_id = 42" > "$SCRATCH/explain_before.txt" 2>&1 if grep -q 'type: ALL' "$SCRATCH/explain_before.txt"; then ok "C2 unindexed EXPLAIN shows type: ALL (full table scan) — matches the article's before state" else no "C2 expected type: ALL before indexing:"; sed 's/^/ /' "$SCRATCH/explain_before.txt" fi MD -N -B -e "EXPLAIN ANALYZE SELECT id, sku, price, name FROM products WHERE category_id = 42" > "$SCRATCH/analyze_before.txt" 2>&1 BEFORE_MS="$(sed -nE 's/.*actual time=[0-9.]+\.\.([0-9.]+).*/\1/p' "$SCRATCH/analyze_before.txt" | head -1)" echo " before index — actual time (outer node): ${BEFORE_MS:-unknown} ms" # --------------------------------------------------------------------------- # CLAIM 3: after CREATE INDEX on category_id, the plan switches to type=ref # via that index, and actual time drops. # --------------------------------------------------------------------------- MD -e "CREATE INDEX idx_products_category ON products(category_id);" MD --vertical -e "EXPLAIN SELECT id, sku, price, name FROM products WHERE category_id = 42" > "$SCRATCH/explain_after.txt" 2>&1 if grep -q 'type: ref' "$SCRATCH/explain_after.txt" && grep -q 'key: idx_products_category' "$SCRATCH/explain_after.txt"; then ok "C3 indexed EXPLAIN shows type: ref using idx_products_category — matches the article's after state" else no "C3 unexpected indexed plan:"; sed 's/^/ /' "$SCRATCH/explain_after.txt" fi MD -N -B -e "EXPLAIN ANALYZE SELECT id, sku, price, name FROM products WHERE category_id = 42" > "$SCRATCH/analyze_after.txt" 2>&1 AFTER_MS="$(sed -nE 's/.*actual time=[0-9.]+\.\.([0-9.]+).*/\1/p' "$SCRATCH/analyze_after.txt" | head -1)" echo " after index — actual time (outer node): ${AFTER_MS:-unknown} ms" if [ -n "${BEFORE_MS:-}" ] && [ -n "${AFTER_MS:-}" ]; then FASTER="$(awk -v b="$BEFORE_MS" -v a="$AFTER_MS" 'BEGIN{print (a $AFTER_MS ms)" else no "C4 indexed query was not faster on this run ($BEFORE_MS ms -> $AFTER_MS ms) — re-run, timing can be noisy on a loaded machine" fi else skip "C4 could not parse actual-time figures from EXPLAIN ANALYZE output" fi # --------------------------------------------------------------------------- # CLAIM 5: SELECT * still needs a row lookup even with the single-column # index; a covering index removes it (Extra: Using index). # --------------------------------------------------------------------------- MD -e "CREATE INDEX idx_products_cat_covering ON products(category_id, sku, price);" MD -e "ANALYZE TABLE products;" >/dev/null 2>&1 MD --vertical -e "EXPLAIN SELECT category_id, sku, price FROM products WHERE category_id = 42" > "$SCRATCH/explain_covering.txt" 2>&1 if grep -q 'Using index' "$SCRATCH/explain_covering.txt" && grep -q 'key: idx_products_cat_covering' "$SCRATCH/explain_covering.txt"; then ok "C5 column-limited query against the covering index shows Extra: Using index (no row lookup)" else no "C5 expected Extra: Using index with the covering index:"; sed 's/^/ /' "$SCRATCH/explain_covering.txt" fi MD --vertical -e "EXPLAIN SELECT * FROM products WHERE category_id = 42" > "$SCRATCH/explain_star.txt" 2>&1 if grep -q 'Using index' "$SCRATCH/explain_star.txt"; then no "C6 SELECT * unexpectedly used Using index (covering) — the covering index should not cover id/name" else ok "C6 SELECT * still requires a row lookup (no 'Using index') even with the covering index present, since it needs id/name too" fi # --------------------------------------------------------------------------- # Cleanup. # --------------------------------------------------------------------------- MD -e "DROP INDEX idx_products_category ON products;" >/dev/null 2>&1 MD -e "DROP INDEX idx_products_cat_covering ON products;" >/dev/null 2>&1 MD -e "DELETE FROM products WHERE id > 0;" >/dev/null 2>&1 echo " (rows deleted; empty schema left behind — DROP DATABASE is blocked in this environment." echo " Remove manually with: $MYSQL_CMD -e 'DROP DATABASE $DB;')" # --------------------------------------------------------------------------- # CONTENT regression guards (not proof — just checks the article text matches # the numbers this script produces). # --------------------------------------------------------------------------- SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ARTICLE="$SELF_DIR/optimize-query-guide.html" if [ -f "$ARTICLE" ]; then grep -q 'cost=50000' "$ARTICLE" && content "article cites cost=50000 for the unindexed scan" || no "article missing cost=50000 figure" grep -q 'cost=875' "$ARTICLE" && content "article cites cost=875 for the indexed lookup" || no "article missing cost=875 figure" else skip "article HTML not found next to this script — content regression checks skipped (expected when downloaded standalone from /verify/)" fi echo echo "=== summary: PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT ===" [ "$FAIL" -eq 0 ]