Anyway, you seem to be doing a death by a thousand queries there and that means _many_ round-trips. I'm sure the script can be rewritten to perform a single query. This is how I'd do it with Postgres-specific syntax:
Writing it with a correlated subquery, we get a query that is terribly slow (one second on my couple-of-thousand-rows dataset):SELECT DISTINCT ON (id) id, description FROM list_index li ORDER BY id, rpt_key DESC;
But moving the distinctness to a subquery makes it fast (~7 milliseconds):SELECT DISTINCT id, (SELECT description from list_index li_i WHERE li_i.id = li_o.id ORDER BY created_at DESC limit 1) AS description FROM list_index li_o;
The SQL should work on every dialect, but of course, these speed measurements apply only to the PostgreSQL query planner. MySQL's is different. Try and see.SELECT id, (SELECT description from list_index li_i WHERE li_i.id = li_o.id ORDER BY created_at DESC limit 1) AS description FROM (SELECT DISTINCT id FROM list_index) li_o;
...My brains aren't working well enough right now to produce a way to do it without a dependent subquery.
In reply to Re: Much slower DBI on RHEL6
by Anonymous Monk
in thread Much slower DBI on RHEL6
by MPM
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |