Skip to content

v2.3.0 - Sessions table: fix slow referrer query (1.4M rows → 25k)

Choose a tag to compare

@hummelmose hummelmose released this 17 Jul 15:56
· 37 commits to main since this release

WP VisitChart v2.3.0

Major performance fix for the referring domains query, plus two layout fixes.


v2.2.x — Number colour and domain list layout fixes

Blue colour not showing on referrer domain numbers
WordPress admin CSS was overriding .lstats-page-count { color: #2271b1 } with a more specific rule. Fixed by injecting the colour as an inline <style> via the admin_head hook, which WP Rocket's CSS optimisation cannot touch.

Domain list items not aligned
The #lstats-referrer-domains-list li elements were missing display: flex, causing numbers to wrap below the domain text. Added to the shared flex rule.


v2.3.0 — New sessions table

Root cause

EXPLAIN on the referring domains query showed type: ALL scanning 1,407,113 rows with Using temporary; Using filesort. The query used a MIN(id) GROUP BY session_id subquery returning ~25,000 IDs, then joined back to the full heartbeats table to run a CASE WHEN categorisation expression on every row.

Fix: lstats_sessions table

A new table stores one row per session, written with INSERT IGNORE on the first heartbeat. Category and referring domain are pre-computed at write time by lstats_categorize_referrer() and stored directly.

The referrers query is now:

SELECT category, COUNT(*) AS cnt
FROM wp_lstats_sessions
WHERE count_date = '2026-07-17' AND is_bot = 0
GROUP BY category;

A simple index scan on ~25,000 rows via idx_date_bot (count_date, is_bot). No subquery, no filesort, no CASE WHEN.

Changes

  • New LSTATS_SESSIONS_TABLE constant (lstats_sessions)
  • DB version bumped to 1.8 — table created automatically via dbDelta on upgrade
  • lstats_handle_heartbeat writes INSERT IGNORE to the sessions table after each heartbeat (subsequent heartbeats from the same session are silently ignored)
  • lstats_get_referrers reads from lstats_sessions — two GROUP BY queries returning 4 category rows and 15 domain rows
  • lstats_cleanup_old_data deletes sessions older than 8 days

Note: The sessions table fills from the first visit after upgrade. Traffic source numbers will repopulate within a few minutes of normal traffic.


Upgrade

Download wp-visitchart.zip below and replace the existing plugin files. The new lstats_sessions table is created automatically on first load after upgrade.