简体   繁体   中英

MySQL/MariaDB - Setting global variable “innodb_flush_log_at_trx_commit = 2” doesn't affect speed

I have set global variable of innodb_flush_log_at_trx_commit to 2 but when I run command ab.exe -n 200 -c 200 -s 9999 http://127.0.0.1/index.php there is no difference in speed before and after that.

When the value of the variable is 1 当变量的值为 1

When the value of the variable is 2 当变量的值为 2

The query inside the file is written in php/laravel:

$status = ['enable', 'disable'];
try {
    \DB::beginTransaction();

    for($p = 1; $p <= 1000; $p++) {
        $price = mt_rand(1000, 5000);
        CarPrice::create([
            'car_id' => $p,
            'amount' => $price,
            'status' => $status[rand(0, 1)],
        ]);
    }

    \DB::commit();
} catch (\Exception $exception) {
    \DB::rollback();
    die('Err!');
}
  • Each time I modify innodb_flush_log_at_trx_commit , I restart the Apache and MariaDB.
  • Windows 10
  • Ram: 8
  • CPU: Pentium 3.3GHz
  • Hard: HDD (Not SSD)

You probably need to COMMIT more than 100 transactions per second to see any difference. That's with HDD; if you have SDD, it might be 1000.

Are you doing the following?

START TRANSACTION;
INSERT
INSERT
...
INSERT   -- 1000 1-row inserts in a single connection?
COMMIT;  -- This is where that setting makes a difference.

The COMMIT is such a tiny percentage of the total that it will be hard to measure.

The real speed up is to do this:

START TRANSACTION;
INSERT ... VALUES
    ( row ),
    ( row ),
    ...
    ( row );  -- one 1000-row insert?
COMMIT;  -- This is where that setting makes a difference.

That will, however, require changes to your app code.

Potential issue: Do you need to grab Last_insert_id() for each row?

PS: This would show off the diff with innodb_flush_log_at_trx_commit ; it will be much slower:

START TRANSACTION;
INSERT
COMMIT;

START TRANSACTION;
INSERT
COMMIT;
...       -- 1000 1-row inserts in a single connection
START TRANSACTION;
INSERT 
COMMIT;  -- after each row inserted!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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