I use PostgreSQL and i have a table like this:

```
create table transactions (
  uuid uuid,
  user_id integer,
  created_at timestamp,
  amount numeric
);
create index idx_transactions_user_id_created_at on transactions (user_id, created_at);

```

I'd like to get the user's "biggest transactions" for the past N days with a query like this:

```
select *
from transactions
where user_id=1234
  and created_at >= current_timestamp - interval '7 days'
order by amount desc
limit 10;

```

The user can choose the number of days and the limit he is interested in.

The query plan is like this:

```
                                                   QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
 Limit  (cost=9.54..9.55 rows=2 width=60)
   ->  Sort  (cost=9.54..9.55 rows=2 width=60)
         Sort Key: amount DESC
         ->  Bitmap Heap Scan on transactions  (cost=4.18..9.53 rows=2 width=60)
               Recheck Cond: ((user_id = 1234) AND (created_at >= (CURRENT_TIMESTAMP - '7 days'::interval)))
               ->  Bitmap Index Scan on idx_transactions_user_id_created_at  (cost=0.00..4.17 rows=2 width=0)
                     Index Cond: ((user_id = 1234) AND (created_at >= (CURRENT_TIMESTAMP - '7 days'::interval)))
(7 rows)

```

So postgres gets all rows which match the user\_id and the created\_at range, than sorts all that and returns the top N.

Is there's a better way to index this? So it scales nicely when there are millions of users and millions of transactions per user?
So far my query is not too slow, but i just wonder if it is theoretically possible to index this table in such a way that postgres will know exactly which 10 row to read from the disk for arbitrary `created_at` condition?