开发者

Python + MySQLDB Batch Insert/Update command for two of the same databases

开发者 https://www.devze.com 2023-01-12 03:05 出处:网络
I\'m working with two databases, a local version and the version on the server.The server is the most up to date version and instead of recopying all values on all tables from the server to my local v

I'm working with two databases, a local version and the version on the server. The server is the most up to date version and instead of recopying all values on all tables from the server to my local version,

I would like to enter each table and only insert/update the values that have changed, from server, and copy those values to my local version.

Is there some simple method to hand开发者_C百科ling such a case? Some sort of batch insert/update? Googl'ing up the answer isn't working and I've tried my hand at coding one but am starting to get tied up in error handling..

I'm using Python and MySQLDB... Thanks for any insight

Steve


If all of your tables' records had timestamps, you could identify "the values that have changed in the server" -- otherwise, it's not clear how you plan to do that part (which has nothing to do with insert or update, it's a question of "selecting things right").

Once you have all the important values, somecursor.executemany will let you apply them all as a batch. Depending on your indexing it may be faster to put them into a non-indexed auxiliary temporary table, then insert/update from all of that table into the real one (before dropping the aux/temp one), the latter of course being a single somecursor.execute.

You can reduce wall-clock time for the whole job by using one (or a few) threads to do the selects and put the results onto a Queue.Queue, and a few worker threads to apply results plucked from the queue into the internal/local server. (Best balance of reading vs writing threads is best obtained by trying a few and measuring -- writing per se is slower than reading, but your bandwidth to your local server may be higher than to the other one, so it's difficult to predict).

However, all of this is moot unless you do have a strategy to identify "the values that have changed in the server", so it's not necessarily very useful to enter into more discussion about details "downstream" from that identification.

0

精彩评论

暂无评论...
验证码 换一张
取 消