Files
zoneminder/db/zm_update-1.39.18.sql
T
Isaac ConnorandClaude Sonnet 5 06801955a8 fix: add ServerId index to Server_Stats to speed up per-server stats lookup
Server.php's ReadStats() query (SELECT ... WHERE ServerId=? ORDER BY
TimeStamp DESC LIMIT 1) had no supporting index, forcing a full table
scan on every page load that shows server stats. Add a composite
(ServerId, TimeStamp) index via zm_update-1.39.18.sql (idempotent,
checked against INFORMATION_SCHEMA.STATISTICS) and zm_create.sql.in
for fresh installs. Keep the existing TimeStamp-only index, since
zmstats.pl's prune DELETE filters by TimeStamp alone.

Also query ReadStats() with the server's actual Id() instead of
coercing Id() <= 1 to 0.

Bump version.txt and the redhat spec Version to 1.39.18.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q7jeoG8ptXRpXxif2JqkD7
2026-07-23 14:01:24 -04:00

20 lines
666 B
SQL

--
-- Add a composite index on Server_Stats to speed up the per-server latest-stats
-- lookup (SELECT * FROM Server_Stats WHERE ServerId=? ORDER BY TimeStamp DESC LIMIT 1),
-- which was previously a full table scan.
--
SET @s = (SELECT IF(
(SELECT COUNT(*)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_name = 'Server_Stats'
AND table_schema = DATABASE()
AND index_name = 'Server_Stats_ServerId_idx'
) > 0,
"SELECT 'Server_Stats_ServerId_idx already exists on Server_Stats table'",
"ALTER TABLE `Server_Stats` ADD INDEX `Server_Stats_ServerId_idx` (`ServerId`, `TimeStamp`)"
));
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;