我应该总是更喜欢 MySQL InnoDB 而不是 MyISAM 吗?

发布于 2024-08-16 01:44:03 字数 94 浏览 8 评论 0原文

刚刚有人告诉我 InnoDB 比 MyISAM 好得多。那么当我创建表时,我应该总是尝试使用InnoDB引擎而不是MyISAM吗?或者两者都有很大的好处吗?

Someone just told me that InnoDB is much better than MyISAM. So when I create a table, should I always try to use InnoDB Engine instead of MyISAM? Or do both have it's big benefits?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

江南烟雨〆相思醉 2024-08-23 01:44:03

MyISAM 是无事务的并且是堆组织的。记录由表中的行偏移量标识,索引将该偏移量存储为行指针。

InnoDB 支持事务并且是索引组织的。记录由PRIMARY KEY 的值标识(如果没有定义PRIMARY KEY,则为隐藏的内部列),并存储在B-Tree 中。二级索引将PRIMARY KEY的值存储为行指针。

涉及全表扫描或二级索引查找的查询在 MyISAM 表上通常更快。

涉及 PRIMARY KEY 搜索的查询通常在 InnoDB 表上更快。

MyISAM 表将表中的记录数存储在表的元数据中,这就是为什么像这样的查询

SELECT  COUNT(*)
FROM    myisamtable

是即时的。

MyISAM 表完全锁定在DML 操作上(有几个例外)。

InnoDB 表锁定单个记录和索引间隙,但是这些是扫描的记录和间隙,而不仅仅是那些与 WHERE 条件匹配的记录和间隙。这可能会导致记录被锁定,尽管它们并不匹配。

InnoDB 表支持引用完整性(FOREIGN KEY)。 MyISAM 表则不然。

有几种场景可以显示这两种引擎的优势。

MyISAM is transactionless and heap-organized. The records are identified by the row offset in the table and the indexes store this offset as a row pointer.

InnoDB supports transactions and is index-organized. The records are identified by the value of the PRIMARY KEY (or a hidden internal column is there is no PRIMARY KEY defined) and are stored in a B-Tree. The secondary indexes store the value of the PRIMARY KEY as a row pointer.

Queries that involve full table scans or secondary index lookups are usually faster on MyISAM tables.

Queries that involve PRIMARY KEY seeks are usually faster on InnoDB tables.

MyISAM tables store the number of records in the table in the table's metadata, that's why the queries like this:

SELECT  COUNT(*)
FROM    myisamtable

are instant.

MyISAM tables are completely locked on the DML operations (with several exceptions).

InnoDB tables lock individual records and index gaps, however these are the records and the gaps that are scanned, not only those matched by the WHERE condition. This can lead to the records being locked despite the fact they don't match.

InnoDB tables support referential integrity (FOREIGN KEYs) . MyISAM tables don't.

There are several scenarios that can show benefits of both engines.

披肩女神 2024-08-23 01:44:03

简单的回答:不,你不应该。

关于每个引擎的几点:

InnoDB

  • 完全支持外键约束和事务。 (符合 ACID)
  • 行级锁定。
  • 可以说更快的 INSERT 语句。 (也可能是 UPDATE)

MyISAM

  • 更快的 SELECT 语句。
  • 支持全文搜索
  • BLOB 和 TEXT 列可以索引
  • 与 InnoDB 相比,最大存储限制为 4 倍(256TB 与 64TB)

请参阅手册中的更多信息:

Simple answer: No, you shouldn't.

A few points about each engine:

InnoDB

  • Full support for Foreign Key restraints and transactions. (ACID compliant)
  • Row-level locking.
  • Arguably faster INSERT statements. (And possibly UPDATE as well)

MyISAM

  • Faster SELECT statements.
  • Support for Full-Text searches
  • BLOB and TEXT columns can be indexed
  • 4x max storage limit, compared to InnoDB (256TB vs 64TB)

See more in the Manual:

无尽的现实 2024-08-23 01:44:03

简单地说:

你应该使用 InnoDB:

  • 如果你需要事务支持
  • 如果你需要外键

你应该使用 MyISAM:

  • 如果你不需要上述并且
  • 你需要速度(更快的数据库操作)

To put it simply:

You should use InnoDB:

  • if you need transaction support
  • if you need foreign keys

You should use MyISAM:

  • if you don't need the above AND
  • you need speed (faster database operations)
怪我入戏太深 2024-08-23 01:44:03

恕我直言,您应该总是更喜欢 InnoDB 而不是 MyISAM,因为事务支持是每个关系数据库系统的核心。

IMHO you should always prefer InnoDB over MyISAM because of the transactional support which is at the heart of every relational database system.

ま昔日黯然 2024-08-23 01:44:03

InnoDB 是一个完全符合 ACID 的数据库引擎,因此提供对事务等的支持。因此,它可能比 MyISAM 数据库慢,而 MyISAM 数据库往往在不同的方向进行优化。

因此,如果您需要事务,InnoDB(或其他 RDBMS,例如 PostgreSQL)是显而易见的选择。

Wikipedia 上有一个合理的比较

InnoDB is a fully ACID compliant database engine and therefore offers support for transactions, etc. As such, it can therefore be slower than the MyISAM database which tends to be optimised in a different direction.

Therefore if you need transactions InnoDB (or another RDBMS such as PostgreSQL) is the obvious choice.

There's a reasonable comparison over on Wikipedia

狼性发作 2024-08-23 01:44:03

你可以两者都用。您可以使用 InnoDB 进行写入数据库/事务,但从非规范化的 MyISAM 数据库读回。这提供了两全其美的效果。您知道您的数据通过 InnoDB 的引用完整性是安全的,并且您可以使用 MyISAM 尽可能快地读取数据。

You could use both. You could use InnoDB for your write database/transactions, but read back from an MyISAM database, which is denormalized. This gives the best of both worlds. You know you're data is safe with referential integrity from InnoDB, and you can read it as fast as you'd like with MyISAM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文