SQL: Add packages.c_hasman cache to speed up package listings

Going from average ~100ms to ~10ms or so. The previous query had a
tendency to be much slower sometimes, let's see if this cache also takes
care of those outliers.

Migration script:

  ALTER TABLE packages ADD COLUMN c_hasman boolean NOT NULL DEFAULT FALSE;

  DROP INDEX packages_system_name_key;
  CREATE UNIQUE INDEX packages_system_name_key ON packages (system, name) INCLUDE (id, c_hasman, dead);

  UPDATE packages SET c_hasman = NOT c_hasman
   WHERE c_hasman <> EXISTS(SELECT 1 FROM package_versions pv WHERE pv.package = packages.id AND EXISTS(SELECT 1 FROM files f WHERE f.pkgver = pv.id));
This commit is contained in:
Yorhel 2024-04-29 21:12:54 +02:00
parent 5d56bded66
commit 1ee5c9c2df
4 changed files with 34 additions and 15 deletions

View file

@ -9,3 +9,13 @@ CREATE TABLE stats_cache_new AS
DROP TABLE stats_cache;
ALTER TABLE stats_cache_new RENAME TO stats_cache;
COMMIT;
-- Update c_hasman.
-- This query is commented out because the indexer will take care to set the
-- c_hasman column automatically. It's included here as "documentation" so it
-- can be run manually when package versions or man pages are removed from the
-- database.
--
-- UPDATE packages SET c_hasman = NOT c_hasman
-- WHERE c_hasman <> EXISTS(SELECT 1 FROM package_versions pv WHERE pv.package = packages.id AND EXISTS(SELECT 1 FROM files f WHERE f.pkgver = pv.id));