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.1.1 db/functions/count_estimate_v01.sql
renalware-core-2.1.0 db/functions/count_estimate_v01.sql
renalware-core-2.0.167 db/functions/count_estimate_v01.sql
renalware-core-2.0.166 db/functions/count_estimate_v01.sql
renalware-core-2.0.165 db/functions/count_estimate_v01.sql
renalware-core-2.0.164 db/functions/count_estimate_v01.sql
renalware-core-2.0.163 db/functions/count_estimate_v01.sql
renalware-core-2.0.162 db/functions/count_estimate_v01.sql
renalware-core-2.0.161 db/functions/count_estimate_v01.sql
renalware-core-2.0.160 db/functions/count_estimate_v01.sql
renalware-core-2.0.159 db/functions/count_estimate_v01.sql
renalware-core-2.0.158 db/functions/count_estimate_v01.sql
renalware-core-2.0.157 db/functions/count_estimate_v01.sql
renalware-core-2.0.156 db/functions/count_estimate_v01.sql
renalware-core-2.0.155 db/functions/count_estimate_v01.sql
renalware-core-2.0.153 db/functions/count_estimate_v01.sql
renalware-core-2.0.152 db/functions/count_estimate_v01.sql
renalware-core-2.0.151 db/functions/count_estimate_v01.sql
renalware-core-2.0.149 db/functions/count_estimate_v01.sql
renalware-core-2.0.148 db/functions/count_estimate_v01.sql