in reply to Much slower DBI on RHEL6
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Much slower DBI on RHEL6
by Anonymous Monk on Feb 07, 2014 at 07:12 UTC | |
by MPM (Novice) on Feb 07, 2014 at 11:32 UTC | |
by MPM (Novice) on Feb 07, 2014 at 12:44 UTC | |
by Anonymous Monk on Feb 07, 2014 at 13:47 UTC | |
Re^2: Much slower DBI on RHEL6
by Anonymous Monk on Feb 06, 2014 at 10:24 UTC |