简体   繁体   English

mysql innodb vs myisam插入

[英]mysql innodb vs myisam inserts

I have a table with 17 million rows. 我有一张1700万行的表。 I need to grab 1 column of that table and insert it all into another table. 我需要抓取该表的1列并将其全部插入到另一个表中。 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 InnoDB大约需要3分45秒

However, MyISAM executes in just below 4 seconds. 但是,MyISAM仅在4秒内执行。 Why the difference? 为什么不同?

I see everyone praising InnoDB but honestly I don't see how it's better for me. 我看到每个人都赞扬InnoDB但老实说我不知道​​它对我来说有多好。 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? 我应该打扰InnoDB吗?

The difference is most likely due to configuration of innoDB, which takes a bit more tweaking than myISAM. 差异很可能是由于innoDB的配置,比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. innoDB的想法是将大部分数据保存在内存中,只有在有几个备用cpu周期时才刷新/读取到磁盘。

should you even bother with InnoDB is a really good question. 如果你甚至打扰InnoDB是一个非常好的问题。 If you're going to keep using MySQL, it's highly recommended you get some experience with InnoDB. 如果您要继续使用MySQL,强烈建议您获得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. 但是如果你为一个不会看到大量流量并且不担心规模的数据库做一个快速而肮脏的工作,那么MyISAM的易用性可能只是你的胜利。 InnoDB can be overkill in many instances where someone just wants a simple database. 在许多人只想要一个简单的数据库的情况下,InnoDB可能会有些过分。

but many of my tables will not be updated 但我的许多表都不会更新

You can still get a performance lift from InnoDB if you are doing 99% reading. 如果您正在进行99%的阅读,您仍然可以从InnoDB获得性能提升。 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. 如果您将缓冲池大小配置为将整个数据库保存在内存中,InnoDB将永远不必转到磁盘来获取您的数据,即使它错过了mysql查询缓存。 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. 在MyISAM中,您很可能必须从磁盘读取行,并且您将离开操作系统为您执行缓存和优化。

innodb-buffer-pool-size InnoDB的缓冲池大小

My first guess is to check innodb_buffer_pool_size which ships out of the box set to 8M. 我的第一个猜测是检查innodb_buffer_pool_size,它开箱即用,设置为8M。 It's recommended to have this around 80% of your total memory. 建议大约占总内存的80%。 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 一旦达到该限制,innodb性能将显着下降,因为它需要从缓冲区中清除一些东西以便为新数据腾出空间,这可能很昂贵

autocommit=0 自动提交= 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". 想想你是否真的想要调整数据库以适应“插入1700万行”。 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. 在这种情况下,MyISAM可能会更快,但当你有100个并发连接同时读取和修改这个表时,你会发现一个经过良好调整的innoDB会赢,MyISAM会阻塞表锁。

How MyISAM sees this operation MyISAM如何看待此操作
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). MyISAM在没有任何调整的情况下会非常擅长这一点,因为在封面下,您只需将每行附加到文件(并更新索引)。 Your OS and disk caching will handle all those performance problems. 您的操作系统和磁盘缓存将处理所有这些性能问题。

How InnoDB sees this operation InnoDB如何看待这个操作
Innodb will know the table needs a write, so it throws the row into the insert buffer. Innodb会知道表需要写入,因此它会将行抛出到插入缓冲区中。 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. 你在下一次插入之前没有时间给它,所以innoDB没有时间处理缓冲区,它耗尽了空间,并且在写入缓冲池并更新索引时被迫“保持”插入。 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. 接下来,你的缓冲池填满了,并且innoDB被迫“保持”插入并将一些页面从缓冲池中刷新到磁盘。 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. 请注意,当您执行此操作后调整InnoDB以快速为您提供MySQL>提示时,InnoDB仍然会陷入困境,以便赶上它的业余时间,但愿意为您执行新的事务。

MUST READ: 必读:
http://www.mysqlperformanceblog.com/2007/11/01/innodb-performance-optimization-basics/ 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) http://dev.mysql.com/doc/refman/5.0/en/innodb-tuning.html (请参阅批量数据加载提示)

You're saying right upto some extend. 你在某种程度上说得对。 InnoDB is slower than MyISAM but in which cases? InnoDB比MyISAM慢,但在哪些情况下呢? Everything is not made to meet everyone's requirements. 一切都不是为了满足每个人的要求。 INNODB is a transactional database engine while MyISAM is not. INNODB是一个事务性数据库引擎,而MyISAM则不是。 Therefore to make it ACID compliance and transactions aware storage engine, we have to pay its cost in terms of response time. 因此,为了使其成为ACID合规性和交易感知存储引擎,我们必须在响应时间方面支付其成本。

Further more InnoDB runs faster if it is properly tuned using my.ini or other configuration file. 如果使用my.ini或其他配置文件正确调整InnoDB,则运行速度更快。

At the end I am able to understand following reasons why people are praising InnoDB: 最后,我能够理解为什么人们赞扬InnoDB:

  1. It is ACID compliant and transaction supported engine 它符合ACID和事务支持引擎
  2. It take row-level locking while working on a table while MyISAM take table-level locks 在MyISAM采用表级锁时,它在处理表时采用行级锁定
  3. InnoDB is highly tunable for multi-core/multi-process machines to improve concurrency InnoDB对于多核/多进程机器具有高度可调性,可以提高并发性

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. 任何东西都可以满足“每个人”的需求,所以它完全取决于您在比较两个引擎的情况。

Check out MYISAM vs Innodb comparison on Wikipedia. 在Wikipedia上查看MYISAM与Innodb的比较。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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