Sha256: 0371f7e0f1902fb6ede22cf4574ba5943732373e6d244141e4dc237e4ce7aa32

Contents?: true

Size: 600 Bytes

Versions: 161

Compression:

Stored size: 600 Bytes

Contents

/*
A plpgsql function by Michael Fuhr for faster counting.
See https://wiki.postgresql.org/wiki/Count_estimate
Normal count(*) can be slow where that are millions of rows.
This is a faster way to count filtered rows by parsing the output of an explain query.
*/
CREATE FUNCTION count_estimate(query text) RETURNS INTEGER AS
$func$
DECLARE
    rec   record;
    ROWS  INTEGER;
BEGIN
    FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP
        ROWS := SUBSTRING(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)');
        EXIT WHEN ROWS IS NOT NULL;
    END LOOP;

    RETURN ROWS;
END
$func$ LANGUAGE plpgsql;

Version data entries

161 entries across 161 versions & 1 rubygems

Version Path
renalware-core-2.0.104 db/functions/count_estimate_v01.sql
renalware-core-2.0.103 db/functions/count_estimate_v01.sql
renalware-core-2.0.102 db/functions/count_estimate_v01.sql
renalware-core-2.0.101 db/functions/count_estimate_v01.sql
renalware-core-2.0.100 db/functions/count_estimate_v01.sql
renalware-core-2.0.99 db/functions/count_estimate_v01.sql
renalware-core-2.0.98 db/functions/count_estimate_v01.sql
renalware-core-2.0.97 db/functions/count_estimate_v01.sql
renalware-core-2.0.96 db/functions/count_estimate_v01.sql
renalware-core-2.0.95 db/functions/count_estimate_v01.sql
renalware-core-2.0.94 db/functions/count_estimate_v01.sql
renalware-core-2.0.93 db/functions/count_estimate_v01.sql
renalware-core-2.0.92 db/functions/count_estimate_v01.sql
renalware-core-2.0.91 db/functions/count_estimate_v01.sql
renalware-core-2.0.90 db/functions/count_estimate_v01.sql
renalware-core-2.0.89 db/functions/count_estimate_v01.sql
renalware-core-2.0.88 db/functions/count_estimate_v01.sql
renalware-core-2.0.87 db/functions/count_estimate_v01.sql
renalware-core-2.0.86 db/functions/count_estimate_v01.sql
renalware-core-2.0.85 db/functions/count_estimate_v01.sql