mysql innodb vs myisam inserts
Asked Answered
G

3

5

I have a table with 17 million rows. I need to grab 1 column of that table and insert it all into another table. Here's what I did:

INSERT IGNORE INTO table1(name) SELECT name FROM main WHERE ID < 500001

InnoDB executes in around 3 minutes and 45 seconds

However, MyISAM executes in just below 4 seconds. Why the difference?

I see everyone praising InnoDB but honestly I don't see how it's better for me. It's so much slower. I understand that it's great for integrity and whatnot, but many of my tables will not be updated (just read). Should I even bother with InnoDB?

Globate answered 16/3, 2012 at 20:26 Comment(7)
I only use InnoDB when I am working with relational tables. Otherwise, if you don't have any foreign keys, MyISAM is what I prefer!Deuced
just to point out, there is an index on both tables. the "main" table is currently myisam.Globate
ben, i would like to use relational tables but i will be dealing with possibly hundreds of millions of rows - i need to index dozens of columns too, so I dont know which direction to go. the integrity is not much of an issue. not for this part at least.Globate
@BenAshton: MyISAM tables are "relational" as well, they are simply not transactional.Partlet
@nick: MyISAM will come to a screeching halt when you have a lot of concurrent reads and writes.Partlet
@a_horse_with_no_name I never said that MyISAM tables aren't can't be relational. I simply said I prefer to use them for relational tables. It was a comment not an answer!Deuced
@BenAshton: But your comment made it sound like you don't see MyISAM tables as relational.Partlet
W
12

The difference is most likely due to configuration of innoDB, which takes a bit more tweaking than myISAM. The idea of innoDB is to keep most of your data in memory, and flushing/reading to disk only when you have a few spare cpu cycles.

should you even bother with InnoDB is a really good question. If you're going to keep using MySQL, it's highly recommended you get some experience with InnoDB. But if you're doing a quick-and-dirty job for a database that won't see a lot of traffic and not worried about scale, then the ease of MyISAM may just be a win for you. InnoDB can be overkill in many instances where someone just wants a simple database.

but many of my tables will not be updated

You can still get a performance lift from InnoDB if you are doing 99% reading. If you configure your buffer pool size to hold your entire database in memory, InnoDB will NEVER have to go to disk to get your data, even if it misses the mysql query cache. In MyISAM, there is a good chance you have to read the row from disk, and you're leaving the operating system to do the caching and optimization for you.

innodb-buffer-pool-size

My first guess is to check innodb_buffer_pool_size which ships out of the box set to 8M. It's recommended to have this around 80% of your total memory. Once you hit that limit, innodb performance will drop significantly because it needs to flush something out of the buffer to make room for the new data, which can be expensive

autocommit=0
Also, make sure autocommit is turned off while you load your table, or flushing will happen on every insert. You can turn it back on after you're done, and it's a client-side setting. very safe.

Loading tables typically happens once
Think about if you really want to tune your database to accommodate "inserting 17million rows". How often do you do this? MyISAM might be quicker in this instance, but when you have 100 concurrent connections all reading and modifying this table at the same time, you'll find a well-tuned innoDB will win and MyISAM will choke on table locks.

How MyISAM sees this operation
MyISAM will be very good at this without any tuning, because under the covers, you're simply appending each row to a file (and updating an index). Your OS and disk caching will handle all those performance problems.

How InnoDB sees this operation
Innodb will know the table needs a write, so it throws the row into the insert buffer. You give it no time before the next insert, so innoDB has no time to deal with the buffer, it runs out of room and is forced to 'hold up' the insert while it writes to the buffer pool and updates indexes. Next, your buffer pool fills up, and innoDB is forced to 'hold up' the insert and flush some page out of the buffer pool to disk. And you keep throwing inserts at it like crazy. Note that when you do tune InnoDB to give you a MySQL> prompt very fast after you do this, InnoDB will still be scrambling underneath the covers to catch up in it's spare time, but will be willing to execute a new transaction for you.

MUST READ:
http://www.mysqlperformanceblog.com/2007/11/01/innodb-performance-optimization-basics/
http://dev.mysql.com/doc/refman/5.0/en/innodb-tuning.html (see bulk data loading tips)

Wringer answered 16/3, 2012 at 20:51 Comment(3)
Please, any MySQL performance experts (especially from Percona) are welcome to correct me if I've gone wrong or left anything out. I will update the answer.Wringer
A bit inaccurate with "hitting a limit on innodb-buffer-pool-size" Flushing is actually related to hitting "innodb_max_dirty_pages_pct". But that's splitting hairs for this question, I guess.Wringer
Also a good read for you: mysqlperformanceblog.com/2007/05/24/…Wringer
K
1

You're saying right upto some extend. InnoDB is slower than MyISAM but in which cases? Everything is not made to meet everyone's requirements. INNODB is a transactional database engine while MyISAM is not. Therefore to make it ACID compliance and transactions aware storage engine, we have to pay its cost in terms of response time.

Further more InnoDB runs faster if it is properly tuned using my.ini or other configuration file.

At the end I am able to understand following reasons why people are praising InnoDB:

  1. It is ACID compliant and transaction supported engine
  2. It take row-level locking while working on a table while MyISAM take table-level locks
  3. InnoDB is highly tunable for multi-core/multi-process machines to improve concurrency

Last but not the least comment from my side; anything can meet "everyone's" needs so its solely depends in which scenario you're comparing both engines.

Kleper answered 16/3, 2012 at 20:35 Comment(0)
R
0

Check out MYISAM vs Innodb comparison on Wikipedia.

http://en.wikipedia.org/wiki/Comparison_of_MySQL_database_engines

Rescission answered 16/3, 2012 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.