I don't know what's wrong with it -- you should try some place specialising in MySQL. It seems unlikely that Perl/DBI are at fault unless RHEL has done something wonky with the relevant packages.
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:
SELECT DISTINCT ON (id) id, description
FROM list_index li
ORDER BY id, rpt_key DESC;
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 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;
But moving the distinctness to a subquery makes it fast (~7 milliseconds):
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;
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.
...My brains aren't working well enough right now to produce a way to do it without a dependent subquery.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.