in reply to while (my @row = $sth->fetchrow_array)

I'll bet that if you benchmark, 99% of the runtime is spent in the database. It almost always is.

As for why it's slow ... the reasons are very simple and boil down to "programmers don't understand databases". I assume you're using MySQL, but the points are applicable to most other RDBMSes.

  1. I'll bet you don't have an index on the aggregated_area column.
  2. I'll bet that column is TEXT, meaning that even if you are indexing it, you're indexing it improperly. You have to index the first N characters (up to 255 for MySQL).
  3. If you're using LIKE to find substrings, then you'd be better off breaking that column out into a separate table and indexing that table.
  4. If they do have % or _ signs and you have them as the first character, then indexes are completely ignored. (They have to be, if you think about it.)

The bottom line is LIKE has extremely poor performance characteristics and should be avoided whenever possible. If you're using LIKE, you had better have exhausted all other possibilities, like normalization.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
  • Comment on Re: while (my @row = $sth->fetchrow_array)