Why do so many people think they can out-smart a database server? :-)
How long is "far too long"?
Did you optimize your database server?
Do you use indexes?
Loading everything into memory is doable, but you
are just re-writing the database engine, and I doubt PHP's array-handling
is much faster than your database.
(after all, you'll be reading 40k records, editing 8k, then re-loading 40k)
If you are using PG-SQL or some other transaction-enabled, use only a few transactions, so you can get a few hunderd updates in one transaction.
And for any kind of database: give it lots and lots of memory.