How to arbitrariarly sort MySQL result set?

Do you put the whole DB at risk by enabling transaction log flush delay - e.g set innodb_flush_log_at_trx_commit to 0?

  • Is it true that risks of having such an option turned on are actually higher than loosing just last N seconds of transactions? Let me explain why I'm asking this question. Imagine that DB server just processed a transaction T, and modified a set of index pages as result of this. Few of these index pages are already flushed to disk, but few others aren't. But all the operations made by T aren't flushed to on-disk transaction log yet. And now we turn off the power (or terminate MySQL process immediately). So when DB server restarts and tries to recover, it doesn't see any of T operations in its transaction log. But some of its indexes actually reflect the changes made by these operations, since some index pages modified by them were flushed before power failure. I.e. as result, transaction T is partially applied - and probably, in such a way that leads to inconsistent index structure (e.g. primary index of table X doesn't reflect any changes made by T, but some of its secondary indexes does - and that's definitely not the worst possible effect of this). So I wonder of something like this is really possible if I delay transaction log flushing, and if not, what exactly database server does to prevent this (MySQL 5.5 w/InnoDB is the most interesting case for me).

  • Answer:

    Checkpoints (dirty page flushing) fsync transaction log too, to avoid the situation you mentioned. The background thread timer isn't the only source of log flushes. Do note, this would raise insane amount of warnings, as LSNs in pages would be ahead of your transaction log LSNs. Generally it is safe to run InnoDB with transaction log flushing disabled, as long as you really don't need that last data (and if you run replicated setup, you have to deal with data missing on the master, that may exist on slave).

Domas Mituzas at Quora Visit the source

Was this solution helpful to you?

Other answers

The answer to your question is right here, as per documentation for 5.5: For ACID compliance the default value since 4.1 is 1: http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_flush_log_at_trx_commit If the value of http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_flush_log_at_trx_commit is 0, the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit. When the value is 1 (the default), the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. When the value is 2, the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. However, the flushing on the log file takes place once per second also when the value is 2. Note that the once-per-second flushing is not 100% guaranteed to happen every second, due to process scheduling issues. The default value of 1 is required for full ACID compliance. You can achieve better performance by setting the value different from 1, but then you can lose up to one second worth of transactions in a crash. With a value of 0, any http://dev.mysql.com/doc/refman/5.5/en/mysqld.html process crash can erase the last second of transactions. With a value of 2, only an operating system crash or a power outage can erase the last second of transactions. InnoDB's http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_crash_recovery works regardless of the value. For the greatest possible durability and consistency in a replication setup using InnoDB with transactions, useinnodb_flush_log_at_trx_commit=1 and sync_binlog=1 in your master server my.cnf file. Caution Many operating systems and some disk hardware fool the flush-to-disk operation. They may tell http://dev.mysql.com/doc/refman/5.5/en/mysqld.html that the flush has taken place, even though it has not. Then the durability of transactions is not guaranteed even with the setting 1, and in the worst case a power outage can even corrupt the InnoDB database. Using a battery-backed disk cache in the SCSI disk controller or in the disk itself speeds up file flushes, and makes the operation safer. You can also try using the Unix command hdparm to disable the caching of disk writes in hardware caches, or use some other command specific to the hardware vendor.

Mohinish Basha

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.