Putting memory pages to swap is the decision of the linux virtual memory
manager not postgres. If your db machine is swapping, first make sure
you have set sensible values for the ram related settings like
shared_buffers (25%), effective_cache_size (75%) and work_mem (keep it
modest at 4 to 16
Divide and conquer. Get rid of the CTE temporarily.
create temp table temp_search as ;
-- index temp_search *appropriately*
analyze temp_search;
Use it instead of the CTE.
Remove the ORDER BY temporarily.
Work on putting the right indices in place to make the above run fast.
I assume you h