manned/util/update_indices.sql
Yorhel 1ee5c9c2df 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));
2024-04-29 21:15:40 +02:00

21 lines
966 B
PL/PgSQL

-- Create a new table before replacing in order to avoid a long-held lock on
-- the table being replaced. The site should remain responsive while these
-- queries are run.
BEGIN;
CREATE TABLE stats_cache_new AS
SELECT (SELECT count(*) FROM contents) AS hashes,
(SELECT count(distinct name) FROM mans) AS mans, *
FROM (SELECT count(*), count(distinct pkgver) FROM files) x(files, packages);
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));