简体   繁体   English

根据CSV,每隔x分钟用PHP更新整个表格

[英]Update a whole table with PHP every x minutes according to a CSV

I have to update a big table (products) in a MySQL database, every 10 minutes with PHP. 我必须每10分钟用PHP更新MySQL数据库中的一个大表(产品)。 I have to run the PHP script with cron job, and I get the most up to date products from a CSV file. 我必须使用cron作业运行PHP脚本,并从CSV文件中获取最新的产品。 The table has currently ~18000 rows, and unfortunately I can not tell how much it will change in a 10 min period. 该表目前有大约18000行,不幸的是我不知道它会在10分钟内变化多少。 The most important thing is of course I do not want the users to notice the update in the background. 最重要的当然是我不希望用户在后台注意到更新。

These are my ideas and fears: 这些是我的想法和恐惧:

  • Idea1: I know that there is a way to load a csv file into a table with MySQL, so maybe I can use a transaction to truncate the table, and import the CSV. Idea1:我知道有一种方法可以将csv文件加载到带有MySQL 的表中 ,所以也许我可以使用事务来截断表,然后导入CSV。 But even if I use transactions, as long as the table is large, I'm afraid that there will be a little chance for some users to see the empty database. 但即使我使用事务,只要表很大,我担心某些用户看到空数据库的可能性很小。

  • Idea2: I could compare the old and the new csv file with a library and only update/add/remove the changed rows. Idea2:我可以将旧的和新的csv文件与库进行比较 ,只更新/添加/删除更改的行。 This way I think there it's not possible for a user to see an empty database, but I'm afraid this method will cost a lot of RAM and CPU, and I'm on a shared hosting. 这种方式我觉得用户不可能看到一个空的数据库,但我担心这种方法会耗费大量的RAM和CPU,而且我在共享主机上。

So basically I would like to know which method is the most secure to update a table completely without the users noticing it. 所以基本上我想知道哪种方法最安全地更新表而没有用户注意到它。

Assuming InnoDB and default isolation level, you can start a transaction, delete all rows, insert your new rows, then commit. 假设InnoDB和默认隔离级别,您可以启动事务,删除所有行,插入新行,然后提交。 Before the commit completes, users will see the previous state. 在提交完成之前,用户将看到以前的状态。

While the transaction is open (after the deletes), updates will block, but SELECTs will not. 事务处于打开状态(删除后),更新将阻止,但SELECT不会。 Since it's a read only table for the user, it won't be an issue. 由于它是用户的只读表,因此不会出现问题。 They'll still be able to SELECT while the transaction is open. 在交易开放时,他们仍然可以SELECT

You can learn the details by reading about MVCC . 您可以通过阅读MVCC了解详细信息。 The gist of it is that any time someone performs a SELECT , MySQL uses the data in the database plus the rollback segment to fetch the previous state until the transaction is committed or rolled back. 它的要点是,只要有人执行SELECT ,MySQL就会使用数据库中的数据和回滚段来获取先前的状态,直到提交或回滚事务为止。

From MySQL docs : 来自MySQL文档

InnoDB uses the information in the rollback segment to perform the undo operations needed in a transaction rollback. InnoDB使用回滚段中的信息来执行事务回滚中所需的撤消操作。 It also uses the information to build earlier versions of a row for a consistent read. 它还使用该信息构建行的早期版本以进行一致读取。

Only after the commit completes will the users see the new data instead of the old data, and they won't see the new data until their current transaction is over. 只有在提交完成后,用户才会看到新数据而不是旧数据,并且在当前事务结束之前,他们不会看到新数据。

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

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