The query takes ~1.8 seconds because the planner uses the orders_created_at_idx B‑Tree index to satisfy ORDER BY created_at DESC LIMIT 20, but that index does not include customer_id or status; as a result the workers perform a “Parallel Index Scan Backward” and then apply the WHERE predicates as a filter, which removed 1,399,958 rows according to the plan shown in your image. This is consistent with the general guidance to look for slow parts in an EXPLAIN plan, such as sequential scans or high‑cost operators, and to evaluate whether an index actually reduces the plan cost .
From the supplied Supabase docs, you should prefer an index type that matches your data and query pattern, e.g. B‑Tree, Hash, GIN, or BRIN . For this particular query, the most directly useful index would need to combine the filter columns and the sort column. The source passages do not explicitly state the recommended index columns for a query like customer_id = … AND status = … ORDER BY created_at DESC, so I cannot confirm an exact “should add” statement from the docs. What the docs do say is: use EXPLAIN to analyze the plan , choose the appropriate index type for the data/query pattern , and if an index does not reduce the plan cost, remove it . They also note that building indexes on large tables can be slow and that CREATE INDEX CONCURRENTLY avoids locking the table for writes .
One passage in the sources mentions that a BRIN index on a monotonic field such as created_at on an orders table can be >10x smaller than a B‑Tree index , but that example is about created_at alone on orders/customers and does not specifically address a composite filter+sort query. Therefore, based only on the provided passages:
- Why it is slow: the existing index supports the
ORDER BYbut not theWHEREfilters, causing a large filtered index scan . - What index to add: the docs advise selecting the index type that best suits your data and query pattern ; the passages do not specify a concrete composite index for
customer_id,status, andcreated_at. The part of your question about the exact index to add is not fully covered by the sources.