简体   繁体   English

如何使用php向MySQL中插入/更新大量数据

[英]How to insert/update a large amount of data into mysql using php

I have an excel sheet which has a large amount of data. 我有一个Excel工作表,其中包含大量数据。 I am using php to insert the data into mysql server. 我正在使用php将数据插入mysql服务器。

I have two problems 我有两个问题

1) I have to update a row if the id already exists, else insert the data. 1)如果ID已经存在,我必须更新一行,否则插入数据。

2) BIG PROBLEM : I have more than 40,000 rows and the time out on the sql server which is set by the admin is 60 seconds. 2)大问题:我有超过40,000行,并且由admin设置的sql服务器上的超时为60秒。 When i run the update/insert query it will take more than 60 seconds, and because of this there will be a timeout. 当我运行更新/插入查询时,将花费超过60秒钟的时间,因此会发生超时。 So the whole process will fail. 因此,整个过程将失败。

Is there a way I can do this ? 有办法吗?

Currently I am checking the student id if it exists, then update otherwise insert. 目前,我正在检查学生证是否存在,然后更新否则插入。 This I feel is taking a lot of time and causing the server to time out. 我觉得这花了很多时间,导致服务器超时。

Also I have this field in the mysql stating the last time the data was updated(last_update). 我在mysql中也有此字段,说明上次数据更新时间(last_update)。 I was thinking of using this date, and if it is past a particular date(ie last time i ran the program) then only those rows should be updated. 我当时正在考虑使用该日期,如果该日期已过特定日期(即我上次运行程序的日期),则仅应更新这些行。

Will this help in anyway ? 无论如何会有所帮助吗?

And what is the query i can run so as to check this date in the mysql database, that if it is past a particular date only those rows need to be updated and not everything else. 我可以运行什么查询来检查mysql数据库中的该日期,即如果该日期已过特定日期,则仅需要更新那些行,而无需更新其他所有内容。 (Please help me with an example query for the above!!!!!!!!!!!!!!!!!) (请为我提供上述示例查询的帮助!!!!!!!!!!)

Assuming that you are using InnoDB engine (which is default in most recent MySQL versions), you should simply use transactions: wrap your insert loop into BEGIN; 假设您使用的是InnoDB引擎(在最新的MySQL版本中是默认值),则应仅使用事务:将插入循环包装到BEGIN中; ... COMMIT; ...提交; block. 块。

By default, every statement is run as transaction, and server must make sure that data makes it safely to disk before continuing to next statement. 默认情况下,每个语句都作为事务运行,并且服务器必须确保数据安全地将其保存到磁盘,然后再继续执行下一条语句。 If you start transaction, then do many inserts, and then commit transaction, only then server must flush all the data onto the disk. 如果启动事务,然后执行许多插入操作,然后再提交事务,则服务器必须将所有数据刷新到磁盘上。 On modern hardware, this could amount only to few disk operations instead of 500k of them. 在现代硬件上,这仅相当于很少的磁盘操作,而不是500k。 Another consideration is to use prepared statements . 另一个考虑因素是使用准备好的语句 Server has to parse every SQL statement before executing it. 服务器必须在执行之前解析每个SQL语句。 This parsing does not come for free, and it is not unusual that parsing time could be more expensive than actual query execution time. 这种解析不是免费的,而且解析时间可能比实际查询执行时间更昂贵,这并不罕见。 Usually, this parsing is done every single time, and for your case it is done 500k times. 通常,此解析是每一次完成的,对于您而言,它是500k次。 If you use prepared statements, parsing/preparation is done only once, and cost to execute statement is only disk write (which is enhanced further if you are within active transaction, because server can batch that write by delaying it until transaction commits). 如果使用准备好的语句,则解析/准备仅执行一次,并且执行语句的成本仅是磁盘写入(如果您处于活动事务中,则会进一步提高成本,因为服务器可以通过延迟写入直到事务提交来批量写入)。

Total improvement from using these methods could be dramatic - I have seen situations when using transactions reduced total run time from 30 minutes to 20 seconds. 使用这些方法可以带来巨大的改善-我已经看到使用事务将总运行时间从30分钟减少到20秒的情况。

http://php.net/manual/en/pdo.prepared-statements.php http://php.net/manual/en/pdo.prepared-statements.php

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

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