Skip to content

Multiple data combined #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
premento opened this issue Jul 19, 2016 · 12 comments
Closed

Multiple data combined #502

premento opened this issue Jul 19, 2016 · 12 comments

Comments

@premento
Copy link

Is it possible to combine multiple arrays and insert in one INSERT statement? If so, how to do the combining of arrays?

@ricwein
Copy link
Contributor

ricwein commented Jul 20, 2016

It's currently not implemented, but you can use transactions to improve overall insert-performance for multiple statements. Also see Issue #483 and #208.

@premento
Copy link
Author

Any plans to implement? Highly useful :)

@ghost
Copy link

ghost commented Jul 20, 2016

why not simply using array_merge($array1, $array2) right before calling insert?

@ricwein
Copy link
Contributor

ricwein commented Jul 21, 2016

I guess, @premento means inserting several datasets at once into a database like:

$data = [
  ['username1', $id1, 'password_hash1'],
  ['username2', $id2, 'password_hash2'],
];
$db->insert('users', $data);

which is currently not supported. An array_merge() doesn't help with that at all.

@premento
Copy link
Author

yes, I was looking into this. Is there a sample to see how transactions could be used for the same?

@ricwein
Copy link
Contributor

ricwein commented Jul 21, 2016

You can use transaction like described in the Readme. It should work with something like:

$success = true;
$db->startTransaction();

foreach($data as $dataSet) {
   if (!$db->insert ('tableName', $dataSet)) {

     // something went wrong

      $db->rollback();
      $success = false;
      break;
   }
}

if ($success) {
    $db->commit();
}

@ricwein
Copy link
Contributor

ricwein commented Jul 21, 2016

If I'll find the time, I'll take a look at implementing a multi-dataSet insertion feature later today, if @avbdr has no problem with that.

@avbdr
Copy link
Collaborator

avbdr commented Jul 21, 2016

sure, go ahead. Ill glad to merge. We can call that method multiInsert or insertMulti(). We can optimize that later, but initial performance is not that bad when using transactions

@ricwein
Copy link
Contributor

ricwein commented Jul 21, 2016

See merge request #504

@avbdr
Copy link
Collaborator

avbdr commented Jul 22, 2016

fixed in master

@avbdr avbdr closed this as completed Jul 22, 2016
@premento
Copy link
Author

Just a doubt, instead of using 10 separate inserts, will there be any advantage in using this new multiInsert? Will the mysql write transactions reduce?

@avbdr
Copy link
Collaborator

avbdr commented Jul 22, 2016

if you are not doing all inserts in 1 transaction then most likely Richards work will be faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants