Frage

I have a MySQl database with a few tables, all UTF-8 and MyISAM storage. In PHP I am parsing an XML file which writes a lot of data to the tables. I am using just simple Insert statements and the mysqli functions.

There not so many read actions on the table and no one of them are during the inserts. First the performance was very very slow so I added SET AUTOCOMMIT = 0 at the beginning of the script.

The issue I have now is that all my inserts which are in e.g. the third foreach loop are ignored and do not appear in the mysql tables. Everything before that is fine.

So my question is, what am I doing wrong and how should I do it?

With autocommit on = Everything is inserted but very very slow With autocommit off = Everything is very fast but a lot of inserts are ignored

Hopefully someone have an idea and can help.

War es hilfreich?

Lösung

MySQL is faster with autocommit turned off because INSERTs are not written to your database immediately; the data is only saved when you execute a COMMIT statement. Do you have a COMMIT statement after inserting your data?

Andere Tipps

You should try like this:

<?php
try {
    $db->beginTransaction();

    $stmt = $db->prepare("SOME QUERY?");
    $stmt->execute(array($value1));

    $stmt = $db->prepare("YET ANOTHER QUERY??");
    $stmt->execute(array($value2, $value3));

    $db->commit();
} catch(PDOException $ex) {
    //Something went wrong then rollback!
    $db->rollBack();
    echo $ex->getMessage();
}

Note: calling bindTransaction() turns off auto commit automatically.


While with mysqli, you can use following instead:

mysqli_autocommit($dbh, FALSE);    // turn off auto-commit
mysqli_rollback($dbh);    // if error, roll back transaction
mysqli_commit($dbh);    // commit transaction
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top