I tried with Postgres 9.4. PG 9.4 is using the same plan with group_by and distinct.

the function variation is much slower at first query

Same as group_by at second query.

select * from skipscan_distinct(-1);

(10 rows)

Time: 473.639 ms

select * from skipscan_distinct(-1);

skipscan_distinct

——————-

1

2

3

4

5

6

7

8

9

10

(10 rows)

Time: 240.236 ms

http://zogovic.com/post/44856908222/optimizing-postgresql-query-for-distinct-values

]]>explain analyse

WITH RECURSIVE t(n) AS (

SELECT MIN(grouper) FROM t_distinct

UNION ALL

SELECT (SELECT t_distinct.grouper FROM t_distinct WHERE t_distinct.grouper > n ORDER BY t_distinct.grouper LIMIT 1)

FROM t WHERE n IS NOT NULL

)

SELECT n FROM t;

explain analyse

WITH RECURSIVE t(n) AS (

SELECT MIN(grouper) FROM t_distinct

UNION ALL

SELECT (SELECT t_distinct.grouper FROM t_distinct WHERE t_distinct.grouper > n LIMIT 1)

FROM t WHERE n IS NOT NULL

)

SELECT n FROM t;

“Function Scan on skipscan_distinct (cost=0.25..10.25 rows=1000 width=4) (actual time=256.244..256.245 rows=10 loops=1)”

]]>In Oracle and SQL Server, by the way, it does not work out of box also, you need some special tricks to make loose index scans work, though it’s still possible.

]]>