Question

Is it possible to limit the range over a window function is calculated?.

I'm using PostgreSql 8.4.

An example query:

select count(*) over() as TOTAL, id, .... from table limit 100

This query returns the 100 top records, its first column displays all the records without taking into account the limit.

The question is, is there a way to also limit the range inside the count?, I mean, if the original query returns a total of 5000, I only need to tell "more than 1000".

edit: the question can be replaced by, does PostgreSql has a way to know if a query has more than "n" results?.

Was it helpful?

Solution

That's actually a clever idea. Why count all the way if you are only interested in first N rows?

I ran a couple of test and window functions with a custom frame definition were faster for big result sets:

count(*) OVER (ROWS BETWEEN CURRENT ROW AND 1001 FOLLOWING) AS total_max_1001

The problem is, you may get a different count for every row, which may or may not be a problem. If you only consider the first row, this works just fine.

Either way, for big result sets, a (uncorrelated) subquery was much faster, yet:

SELECT id
     ,(SELECT count(*)::int
       FROM  (SELECT FROM tbl WHERE ... LIMIT  1001) x  -- repeat conditions
      ) AS total_max_1001
FROM   tbl
WHERE  ...
LIMIT  100;

Tests were in Postgres 9.1. I expect similar results for all versions since 8.4, up to 9.3.

Gives you an exact full count up to 1001 (which may be read as "more than 1000"), but does not count any further. Also works great with indexes. I am going to tune a couple of my own queries with this ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top