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. maping_table
has around 50k entries, growing by several hundreds each day.
INSERT
ing those ~50 lines in the mapping table takes around 10 to 20 seconds which makes my website appear very slow.
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.
Here is the SHOW CREATE
statement :
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 :
$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.
I have tried deleting the UNIQUE KEY
, then all FOREIGN KEYS
, then all INDEXES
but those did not make any difference in performace.