简体   繁体   English

MyISAM 什么时候比 InnoDB 更好?

[英]When MyISAM is better than InnoDB?

Sometimes I got asked on some interviews: what benefits does InnoDB have against MyISAM and when MyISAM is better than InnoDB?有时我在一些采访中被问到:InnoDB 对 MyISAM 有什么好处,什么时候 MyISAM 比 InnoDB 更好? It's all clear about the first part of question: InnoDB is transaction compliant, row-level blocking instead of table-level blocking, foreign key support and some others, these points just came to mind immidiately.问题的第一部分很清楚:InnoDB 是事务兼容的,行级阻塞而不是表级阻塞,外键支持和其他一些,这些点立即浮现在脑海中。

But when MyISAM is really better than InnoDB?但是什么时候 MyISAM 真的比 InnoDB 更好呢?

MyISAM is better than InnoDB when you don't need those advanced features and storage speed is more important than other concerns.当您不需要那些高级功能并且存储速度比其他问题更重要时,MyISAM 比 InnoDB 更好。 MyISAM also allows full-text searches to be performed inside the database engine itself, instead of needing to query results and then search them as an array or whatever in your application. MyISAM 还允许在数据库引擎本身内部执行全文搜索,而不需要查询结果,然后将它们作为数组或应用程序中的任何内容进行搜索。

InnoDB is a reasonable choice if you need to store data with a high degree of fidelity with complicated interactions and relationships.如果您需要以高度保真度存储具有复杂交互和关系的数据,InnoDB 是一个合理的选择。 MyISAM is a reasonable choice if you need to save or load a large number of records in a small amount of time.如果您需要在短时间内保存或加载大量记录,MyISAM 是一个合理的选择。

I wouldn't recommend using MyISAM for data that matters.我不建议将 MyISAM 用于重要的数据。 It's great for logging or comments fields or anything where you don't particularly care if a record vanishes into the twisting nether.它非常适合记录或评论字段或任何您不特别关心的记录是否消失在扭曲的地狱中。 InnoDB is good for when you care about your data, don't need fast searches and have to use MySQL.当您关心您的数据、不需要快速搜索并且必须使用 MySQL 时,InnoDB 非常适合。

It's also worth mentioning that InnoDB supports row-level locking, while MyISAM only supports table-level locking - which is to say that for many common situations, InnoDB can be dramatically faster due to more queries executing in parallel.还值得一提的是,InnoDB 支持行级锁定,而 MyISAM 只支持表级锁定——也就是说,对于许多常见的情况,InnoDB 可以因为并行执行更多的查询而显着加快。

The bottom line: Use InnoDB unless you absolutely have to use MyISAM.底线:除非您绝对必须使用 MyISAM,否则请使用 InnoDB。 Alternatively, develop against PostgreSQL and get the best of both.或者,针对 PostgreSQL 进行开发并充分利用两者。

MyISAM doesn't support transactions (and the other things mentioned) so it can work faster. MyISAM 不支持事务(以及提到的其他内容),因此它可以更快地工作。 MyISAM is a way to achieve higher performance in those situations when you do not need these features. MyISAM 是一种在您不需要这些功能的情况下实现更高性能的方法。

MyISAM supports full text, as mentioned, but also supports the MERGE table type.如前所述,MyISAM 支持全文,但也支持 MERGE 表类型。 This is handy when you have a large table and would like to "swap" out/archive parts of it periodically.当您有一张大桌子并希望定期“交换”/归档其中的一部分时,这很方便。 Think about a logging or report data that you want to keep the last quarter and/or year.考虑您想要保留最后一个季度和/或年度的日志记录或报告数据。 MyISAM handles large amounts of data like this better, when you are mainly inserting and rarely updating or deleting.当您主要是插入而很少更新或删除时,MyISAM 可以更好地处理这样的大量数据。

InnoDB performance drops pretty quickly and dramatically once you can't fit the indexes in memory.一旦你无法适应 memory 中的索引,InnoDB 的性能就会迅速下降。 If your primary key is not going to be a number (ie auto increment), then you may want to rethink using InnoDB.如果您的主键不是数字(即自动递增),那么您可能需要重新考虑使用 InnoDB。 The primary key is replicated for every index on an InnoDB table.为 InnoDB 表上的每个索引复制主键。 So if you have a large primary key and a few other indexes, your InnoDB table will get very large very quick.因此,如果您有一个大的主键和一些其他索引,您的 InnoDB 表将很快变得非常大。

There are a few features that MySQL only has implemented for MyISAM (such as native fulltext indexing). MySQL 仅针对 MyISAM 实现了一些功能(例如原生全文索引)。

That said, InnoDB is still typically better for most production apps.也就是说,对于大多数生产应用程序来说,InnoDB 通常仍然更好。

Also: Full-text search in mySQL is only supported in myISAM tables.另外:仅 myISAM 表支持 mySQL 中的全文搜索。

MyISAM has a very simple structure, when compared with InnoDB.与 InnoDB 相比,MyISAM 的结构非常简单。 There is no row versioning, there's one file per table and rows are stored sequentially.没有行版本控制,每个表有一个文件,行按顺序存储。 However, while it supports concurrent inserts (SELECTs and 1 INSERT can run together), it also has table-level locks (if there are 2 INSERTs on the same table, 1 has to wait).但是,虽然它支持并发插入(SELECT 和 1 个 INSERT 可以一起运行),但它还具有表级锁(如果同一张表上有 2 个 INSERT,则 1 个必须等待)。 Also, UPDATEs and DELETEs are slow because of the structure of the data files.此外,由于数据文件的结构,UPDATE 和 DELETE 很慢。

MyISAM doesn't support transactions or foreign keys. MyISAM 不支持事务或外键。

Generally, MyISAM should be better if you work on general trends (so you don't care about the correctness of individual rows) and data is updated by night or never.一般来说,如果您研究一般趋势(因此您不关心各个行的正确性)并且数据在夜间更新或从不更新,MyISAM应该会更好。 Also, it allows to move individual tables from one server to another, via the filesystem.此外,它允许通过文件系统将单个表从一台服务器移动到另一台服务器。

InnoDB supports very well concurrency and transactions. InnoDB 很好地支持并发和事务。 Has a decent support for fulltext and an almost-decent support for foreign keys.对全文有不错的支持,对外键也有几乎不错的支持。

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

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