in reply to DBI - need for speed

Do it all in SQL. You'll avoid at least one round-trip conversation with the DB server for each record. Even with a fully local database that's a fair amount of time savings, and if your database is on another machine it can make things enourmously faster.

You also might want to check the utilities that come with your database. Many have tools to do things just like this (as your code's not doing anything complex) and they'll be about as fast as you can possibly get, since they can cheat.