I have an article table with more than 2000000 rows, because the count was so slow, I am using the estimate rows like this:
select count_estimate('select * from article');
this works for the article pagination, now I am facing a problem is that I want to add some conditions with the pagination query. For example, I need to filter the article using the channel id like this:
select count_estimate('select * from article where sub_source_id = 308');
this seems could not work. when I changed the channel id, the rows count did not changed. the nuber is not precisely. what should I do to get the rows count using filter? is it possible? This is the function:
CREATE OR REPLACE FUNCTION public.count_estimate(query text)
RETURNS bigint
LANGUAGE plpgsql
AS $function$
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
$function$
;