I have a simple table in postgresql, say
| id | fname |
|---|---|
| abc | bert |
| def | jaap |
| ghi | kees |
| jkl | jan |
| etc | piet |
...etc...
With a string primary key id.
My table has millions of rows.
I want to get a list of every 10_000th (give or take) row.
Basically:
SELECT id
FROM (
SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS rownum
FROM mytable
) as t
WHERE ((t.rownum - 1) % 10000) = 0;
But that seems to be very slow. Is there an efficient alternative?
TABLESAMPLEcomes into mind. But if that's useful to you depends on how accurate the every 10k have to be.EXPLAIN (ANALYZE, BUFFERS) <query>, preferably after turning track_io_timing on?