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.147 db/functions/count_estimate_v01.sql
renalware-core-2.0.146 db/functions/count_estimate_v01.sql
renalware-core-2.0.145 db/functions/count_estimate_v01.sql
renalware-core-2.0.144 db/functions/count_estimate_v01.sql
renalware-core-2.0.143 db/functions/count_estimate_v01.sql
renalware-core-2.0.142 db/functions/count_estimate_v01.sql
renalware-core-2.0.141 db/functions/count_estimate_v01.sql
renalware-core-2.0.140 db/functions/count_estimate_v01.sql
renalware-core-2.0.139 db/functions/count_estimate_v01.sql
renalware-core-2.0.138 db/functions/count_estimate_v01.sql
renalware-core-2.0.137 db/functions/count_estimate_v01.sql
renalware-core-2.0.136 db/functions/count_estimate_v01.sql
renalware-core-2.0.135 db/functions/count_estimate_v01.sql
renalware-core-2.0.134 db/functions/count_estimate_v01.sql
renalware-core-2.0.133 db/functions/count_estimate_v01.sql
renalware-core-2.0.132 db/functions/count_estimate_v01.sql
renalware-core-2.0.131 db/functions/count_estimate_v01.sql
renalware-core-2.0.130 db/functions/count_estimate_v01.sql
renalware-core-2.0.129 db/functions/count_estimate_v01.sql
renalware-core-2.0.128 db/functions/count_estimate_v01.sql