简体   繁体   English

增量MySQL

[英]Incremental MySQL

I have 2 MySQL databases on the same Linux box. 我在同一个Linux机器上有2个MySQL数据库。 They aren't super large, but some tables hold around 500,000 rows, increasing by about 40,000 rows per month. 它们不是超大,但有些表格大约有500,000行,每月增加约40,000行。

What I need to do is to write a partial backup from one database to the other once per day. 我需要做的是每天从一个数据库写一个部分备份到另一个。 This partial backup is a snapshot and apart from the backups will not have any fresh data written to it. 此部分备份是一个快照,除备份外,不会向其写入任何新数据。 It contains only some of the tables of the main db, and from those tables only some of the fields. 它只包含主数据库的一些表,而这些表只包含一些字段。

It is easy enough to write a PHP script that deletes the backup database, and then recreates it with the desired data to get a fresh snapshot, however i am wondering if there is a way to do this incrementally with PHP and only write new or changed data. 编写一个删除备份数据库的PHP脚本很容易,然后用所需的数据重新创建它以获得一个新的快照,但是我想知道是否有办法用PHP逐步执行此操作并且只写新的或更改的数据。

Do you have a timestamp field on your database? 你的数据库上有时间戳字段吗? Using a timestamp with ON UPDATE CURRENT_TIMESTAMP clause would allow you to know the modification time of each row. 使用带有ON UPDATE CURRENT_TIMESTAMP子句的时间戳可以让您知道每行的修改时间。 That way, you could easily do a SELECT query on rows WHERE timestamp is greater than a given value. 这样,您可以轻松地对行WHERE时间戳大于给定值执行SELECT查询。

If you are checking for new or changed data on that many records, it will be heavy on the machine. 如果要检查许多记录上的新数据或已更改的数据,则计算机上的数据会很大。 Each record will have to be compared to a record in the other database. 每条记录都必须与另一个数据库中的记录进行比较。 Will be very slow and huge performance impact. 将会非常缓慢和巨大的性能影响。

The best way to go would be to use software for backup that is designed for this task. 最好的方法是使用专为此任务设计的备份软件。 If you realy want a PHP solution I would add a new table to the database called changes. 如果你真的想要一个PHP解决方案,我会在数据库中添加一个名为changes的新表。 It will hold a table name and key index. 它将保存表名和密钥索引。

Then add a trigger on each table that needs a backup that will enter a new entry in the changes table with the table name and the key. 然后在需要备份的每个表上添加一个触发器,该备份将使用表名和键在更改表中输入新条目。 Then your PHP script can query this table to find changes and new items and only query these. 然后,您的PHP脚本可以查询此表以查找更改和新项目,并仅查询这些项目。

Instead of using a seperate table you can always add a tiny int column to the existing tables called lastAction or something and store 0 for no change, 1 for update and 2 for new record. 您可以随时将一个小的int列添加到名为lastAction或其他内容的现有表中,而不是使用单独的表,并将0存储为无更改,1表示更新,2表示新记录。

Then again you can query only the needed data. 然后,您只能查询所需的数据。

Keep in mind that a PHP solution with two database is never reliable. 请记住,具有两个数据库的PHP解决方案永远不可靠。 You would need a structure like: 你需要一个像这样的结构:

1 Query record from old database
2 Update/insert record in new database
3 Mark old database record as completed.

It could very well be that after you are done with step 2, that you lose connection to the old database and can no longer set it to completed. 很可能是在完成第2步之后,您将失去与旧数据库的连接,并且无法再将其设置为已完成。 This will result in the record being updated or added again on the next run. 这将导致记录在下次运行时被更新或再次添加。

Another issue could be that between step 1 and 2, somebody alters the record again. 另一个问题可能是在第1步和第2步之间,有人再次改变记录。 You then insert an older value in the backup and mark it as completed. 然后在备份中插入旧值并将其标记为已完成。 While the newest update will no longer be backed up. 虽然最新的更新将不再备份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM