I have an old database and a new database. The old records were converted to the new database recently. All our old applications continue to point to the old database, but the new applications point to the new database.
Currently the old database is the only one being updated, so throughout the day the new database becomes out of sync. It is acceptable for the new database to be out of sync for a day, so until all our applications are pointed to the new database I just need to write a nightly cron job that will bring it up to date.
I do not want to purge the new database and run the complete conversion script each night, as that would reduce uptime and would create a mess in our auditing of that table.
I’m thinking about selecting all the data from the old database, converting it to the new database structure in memory, and then checking for the existence of each record before inserting it in the new database. After that’s done, I’d select everything from the new database and check if it exists in the old one, and if not delete it.
Is this the simplest way to do this?
This method double-comparison method will get unwieldy if you have very high volume at all. If you don’t have high volume, then doing a complete replacement every night is “simpler”.
Assuming that you do have to worry about volume, this is what the data warehouse guys call a change data capture problem. If your old database has a timestamp on the records, then you could at least save half of your comparison (you still might need to look for the deletes). If not, then here are a couple of other suggestions.
You could put a trigger on the old database to write out any changes (insert, update, delete) to the old database and then apply those to the new database every night.
Alternatively, your database system probably has a log (redo log, transaction log, etc.) that has a record of every database transaction. If you have access to this log, then you could query it every night for the transactions since the previous night. Then appy those transactions to the new database.