简体   繁体   English

MySQL性能差INSERT或PHP PDO

[英]MySQL Poor performance INSERT or PHP PDO

Usually when a user comes to my website he 'subscribes' to around an average of ~50 events at once, which we save in a mapping_table that links many users to many events. 通常,当用户访问我的网站时,他会平均一次平均“订阅”约50个事件,我们将其保存在一个可将许多用户链接到许多事件的mapping_table中。 maping_table has around 50k entries, growing by several hundreds each day. maping_table大约有5万个条目,每天增长数百个。

INSERT ing those ~50 lines in the mapping table takes around 10 to 20 seconds which makes my website appear very slow. INSERT映射表中的〜50行大约需要10到20秒,这使我的网站显得很慢。

I reach a point where I can't figure out how to improve the following PHP or SQL in order to lower the INSERT time as it seems pretty basic. 我似乎无法解决如何改善以下PHP或SQL以减少INSERT时间的问题,因为这似乎很基本。

Here is the SHOW CREATE statement : 这是SHOW CREATE语句:

    CREATE TABLE `mapping_table` (
     `user_id` char(21) NOT NULL,
     `event_id` char(21) NOT NULL,
     `update_id` char(10) NOT NULL,
     PRIMARY KEY (`user_id`,`event_id`),
     KEY `event` (`event_id`),
     CONSTRAINT `mapping_table_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users_table` (`user_id`) ON DELETE CASCADE ON UPDATE NO ACTION,
     CONSTRAINT `mapping_table_ibfk_2` FOREIGN KEY (`event_id`) REFERENCES `events_table` (`event_id`) ON DELETE CASCADE ON UPDATE NO ACTION
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1

My php script for the insert part : 我的PHP脚本的插入部分:

    $update_id = 'RandomString';
    $conn = new PDO($pdo_serv, $db_user, $db_pwd);
    $stmt = $conn->prepare("INSERT INTO `mapping_table` (`user_id`, `event_id`, `update_id`)
                    VALUES (:uid,:eid,:upd)");

    //GO THROUGH EACH USER SUBSCRIBED EVENTS AND INSERT THEM
    foreach($subscribed_events AS $event) {
        $stmt->bindParam(':uid', $userid);
        $stmt->bindParam(':eid', $event->id);
        $stmt->bindParam(':upd', $update_id);
        $stmt->execute();
    }

I'm open to any suggestions. 我愿意接受任何建议。

Additional information : 附加信息 :

If I microtime each insert I get an average of 300ms to 1sec per insert. 如果我microtime每个插入我的300ms的平均得到每个刀片1秒。

I have tried deleting the UNIQUE KEY , then all FOREIGN KEYS , then all INDEXES but those did not make any difference in performace. 我尝试删除UNIQUE KEY ,然后删除所有FOREIGN KEYS ,然后删除所有INDEXES但是这些在性能上没有任何区别。

Following comments on my questions I modified my php as follow for batch INSERT : 在对我的问题发表评论之后,我对php进行了如下修改,以进行batch INSERT

$update_id = 'RandomString';
$conn = new PDO($pdo_serv, $db_user, $db_pwd);

$sql = "INSERT INTO `mapping_table` (`user_id`, `event_id`, `update_id`)
                VALUES ";
$values = "";

//GO THROUGH EACH USER SUBSCRIBED EVENTS GENERATE VALUES STRING
foreach($subscribed_events AS $event) {
    $values .= "('".$userid."','".$event->id."','".$update_id."'),"
}

//Proceed batch insert
$values = rtrim($values, ",");
$sql .= $values;
$stmt = $conn->prepare($sql);
$stmt->execute();

This decreased my average insert to less than 10ms per line inserted (as opposed to 300ms to 1sec per line previously). 这将我的平均插入时间减少到每行插入少于10毫秒(之前的每行300毫秒减少到1秒)。 Never thought the difference would be that big. 没想到差异会那么大。 Thanks. 谢谢。

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

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