php – 加快大量的mysql更新和插入

前端之家收集整理的这篇文章主要介绍了php – 加快大量的mysql更新和插入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序需要通过大量的条目更新大量的数据.基本上它做了大约7,000个插入和/或更新,但是它需要一个漫长的时间(比如近9分钟,平均每个查询约0.08秒).本质上,我正在寻找一般的加速,以提出多个这样的请求(我不期望一个具体的答案我的模糊的例子…这只是希望,有帮助的解释).

以下是分析请求的一些示例:

SELECT `habitable_planets`.* FROM `habitable_planets` WHERE (timestamp = '2010-10-15T07:30:00-07:00') AND (planet_id = '2010_Gl_581_c')

INSERT INTO `habitable_planets` (`planet_id`,`timestamp`,`weather_air_temp`,`weather_cell_temp`,`weather_irradiance`,`weather_wind_float`,`biolumin_to_date`,`biolumin_detected`,`craft_energy_usage`,`craft_energy_consumed_to_date`) VALUES (?,?,?)

SELECT `habitable_planets`.* FROM `habitable_planets` WHERE (timestamp = '2010-10-15T07:45:00-07:00') AND (planet_id = '2010_Gl_581_c')

INSERT INTO `habitable_planets` (`planet_id`,?)

重复恶作剧(约7,000次).这是一个更新,收集在24小时内间隔生成的数据,然后每天对数据库进行大量更新.鉴于我所看到的有限的一点,你们有什么建议来加速这个过程?

例如…它是有意义的,而不是为每个时间戳选择一个,一个选择一个范围,然后在脚本中迭代它们;

模糊地喜欢:

SELECT `habitable_planets`.* FROM `habitable_planets` WHERE (planet_id = '2010_Gl_581_c')

将该结果分配给$foo,然后执行以下操作:

foreach ($foo as $bar)
{
    if ($bar['timestamp'] == $baz) // where $baz is the needed timestamp
    {
    // do the insert here
    }
}

编辑:为了增加一点,在我的情况下提高响应能力的一件事是改变一堆代码来检查现有的记录,并根据使用INSERT... ON DUPLICATE KEY UPDATE SQL查询的结果进行插入或更新.这导致了我的特殊情况下大概30%的速度增长,因为它至少将一个数据库的行程从该方程中删除,并且数千个请求真的加起来.

一些有用的链接

> 32 Tips To Speed Up Your MySQL Queries
> Turn on MySQL query cache to speed up query performance?
> Multiple Insert in Single Query – PHP/MySQL
> 3 Ways to Speed Up MySQL

MysqL文档:

Speed of INSERT Statements说:

  • If you are inserting many rows from the same client at the same time,use
    INSERT statements with multiple VALUES
    lists to insert several rows at a
    time. This is considerably faster
    (many times faster in some cases) than
    using separate single-row INSERT
    statements. If you are adding data to
    a nonempty table,you can tune the
    bulk_insert_buffer_size variable to
    make data insertion even faster.

  • If multiple clients are inserting a lot of rows,you can get higher speed
    by using the INSERT DELAYED statement.

  • For a MyISAM table,you can use concurrent inserts to add rows at the
    same time that SELECT statements are
    running,if there are no deleted rows
    in middle of the data file.

  • When loading a table from a text file,use LOAD DATA INFILE. This is
    usually 20 times faster than using
    INSERT statements.

  • With some extra work,it is possible to make LOAD DATA INFILE run even faster for a MyISAM table when the table has many indexes.

原文链接:https://www.f2er.com/php/132573.html

猜你在找的PHP相关文章