Below is my sql that I am trying to run. But while I am executing this, the cost is very high. How can I optimize this query? This query also sometimes takes more than 6 minutes to execute. The table that I am querying has more than 1 million data.
SELECT * FROM application app
WHERE app.user_id = '123456*******'
AND app.updated_at is not null
AND app.status IN ('YES', 'NO')
AND app.amount > 0
ORDER BY app.created_at desc limit 1
Production:
Limit (cost=0.56..2017.59 rows=1 width=982) (actual time=421864.746..421864.748 rows=1 loops=1)
-> Index Scan Backward using application_created_at on application app (cost=0.56..7533571.71 rows=3735 width=982) (actual time=421864.745..421864.745 rows=1 loops=1)
Filter: ((updated_at IS NOT NULL) AND ((status)::text = ANY ('{YES,NO}'::text[])) AND (amount > '0'::numeric) AND ((user_id)::text = '123456*****'::text))
Rows Removed by Filter: 27483657
Planning time: 0.194 ms
Execution time: 421864.773 ms
I have also added the below index, but somehow its not using this index and using another index application_created_at.
CREATE INDEX Concurrently application_user_info ON public.application USING btree (user_id,status,updated_at,amount);
It’s probably not going to use the index you want because of this line (which is not providing a value in the index)
Try altering your index to move this column to the end i.e.