forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request IQSS#10096 from GlobalDataverseCommunityConsortium…
…/IQSS/10095-download_estimate-bug IQSS/10095-download estimate bug fix
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/resources/db/migration/V6.0.0.3__10095-guestbook-at-request2.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
-- This creates a function that ESTIMATES the size of the | ||
-- GuestbookResponse table (for the metrics display), instead | ||
-- of relying on straight "SELECT COUNT(*) ..." | ||
-- It uses statistics to estimate the number of guestbook entries | ||
-- and the fraction of them related to downloads, | ||
-- i.e. those that weren't created for 'AccessRequest' events. | ||
-- Significant potential savings for an active installation. | ||
-- See https://github.com/IQSS/dataverse/issues/8840 and | ||
-- https://github.com/IQSS/dataverse/pull/8972 for more details | ||
|
||
CREATE OR REPLACE FUNCTION estimateGuestBookResponseTableSize() | ||
RETURNS bigint AS $$ | ||
DECLARE | ||
estimatedsize bigint; | ||
BEGIN | ||
SELECT CASE WHEN relpages<10 THEN 0 | ||
ELSE ((reltuples / relpages) | ||
* (pg_relation_size('public.guestbookresponse') / current_setting('block_size')::int))::bigint | ||
* (SELECT CASE WHEN ((select count(*) from pg_stats where tablename='guestbookresponse') = 0 | ||
OR (select array_position(most_common_vals::text::text[], 'AccessRequest') | ||
FROM pg_stats WHERE tablename='guestbookresponse' AND attname='eventtype') IS NULL) THEN 1 | ||
ELSE 1 - (SELECT (most_common_freqs::text::text[])[array_position(most_common_vals::text::text[], 'AccessRequest')]::float | ||
FROM pg_stats WHERE tablename='guestbookresponse' and attname='eventtype') END) | ||
END | ||
FROM pg_class | ||
WHERE oid = 'public.guestbookresponse'::regclass INTO estimatedsize; | ||
|
||
if estimatedsize = 0 then | ||
SELECT COUNT(id) FROM guestbookresponse WHERE eventtype!= 'AccessRequest' INTO estimatedsize; | ||
END if; | ||
|
||
RETURN estimatedsize; | ||
END; | ||
$$ LANGUAGE plpgsql IMMUTABLE; |