Batch MYSQL Inserts with parent relations in transaction

dirk :

Considering two tables (I've simplified it a bit, in reality there are more related tables and more columns):

TABLE 'visit'
-------------
- id (auto increment)
- visit_country
- visit_browser

TABLE 'pageview'
----------------
- id
- visit_id (relates to visit.id)
- page_url

Every day, I want to insert a PHP array with around 100.000 visits and 400.000 hits into these tables. The way to speed this process up is by

  • adding multiple inserts into one query
  • and wrapping up the entire bunch of queries into a transaction.

However, my problem is that in order to insert the pageview, the Last Insert ID of the parent visit is required. In a transaction where let's say a 1000 visits are inserted using one query, I can no longer use PDO's lastInsertId() method.

Ideally I keep everything into one transaction. A less ideal solution would be to insert all visits and add a reference column, then execute, check which ID each visit got and then use a new transaction to insert the hits. But that is far from ideal.

What would be the way to approach this? How do ORM's like Doctrine solve this problem (I need to rely on raw SQL through PDO here)?

Ronak Dhoot :

you can do make the query something like this

$sql = "INSERT INTO `visit` (`id`, `visit_country`, `visit_browser`) 
VALUES (NULL, :visit_country, :visit_browser);
INSERT INTO `pageview` (`visit_id`, `page_url`) 
VALUES (NULL, LAST_INSERT_ID(), :page_url)";

than prepare, bindparam & execute

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216994&siteId=1