mysql 的 MEMORY 引擎为什么应用没有 redis 的应用广泛?

2016-09-01 11:05:54 +08:00
 changwei

我看很多人宁愿用 redis 存储数据的 json 也不会用 mysql 的 MEMORY 引擎,请问是这个引擎性能差还是什么原因吗?我观察过国内一些开源项目也基本上没有用到过这个引擎,这是为什么呐?

2546 次点击
所在节点    问与答
2 条回复
shiji
2016-09-01 13:30:57 +08:00
MySQL 官方文档:
http://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html

The MEMORY storage engine (formerly known as HEAP) creates special-purpose tables with contents that are stored in memory. Because the data is vulnerable to crashes, hardware issues, or power outages, only use these tables as temporary work areas or read-only caches for data pulled from other tables.
特点:断电 /异常 /崩溃,你存进去的数据就没啦
..............
典型应用
A typical use case for the MEMORY engine involves these characteristics:

Operations involving transient, non-critical data such as session management or caching. When the MySQL server halts or restarts, the data in MEMORY tables is lost.
存放不重要的数据。

In-memory storage for fast access and low latency. Data volume can fit entirely in memory without causing the operating system to swap out virtual memory pages.
快速,低延迟读数据

A read-only or read-mostly data access pattern (limited updates).
只读数据。

总结:数据容易丢失, insert/update 性能不佳,写入锁表,不如 InnoDB ,读取性能貌似比 Redis 快一丢丢。

~~~~~~~~~
Redis :
http://stackoverflow.com/questions/7888880/what-is-redis-and-what-do-i-use-it-for

特点: NoSQL (之 key-value 型),数据出现异常不会丢失,支持数据类型多,跨进程跨程序跨服务器共享数据方便快捷。写数据的速度比 MySQL 的 memory 引擎快多了。

单单从性能上说,和在 RamDisk 上的 InnoDB 引擎速度差不多。

不过还是觉得, RDBMS 和 NoSQL 哪个更适合你的项目更重要一些。


MySQL 的 memory 真的很少用,真遇到了读的频繁,写入特别少,数据还不怎么重要的情况可以一试。
msg7086
2016-09-01 15:28:05 +08:00
应该还是有用到的吧。用数据库的那些程序如果要存 session 的话还是会经常去用 memory 表的。
不用的原因我能想到的:
- 太重。装一个 MySQL 并且跑起来,比跑一个 Redis 要重一些。
- 数据易失。没有 Redis 那样 Snapshot ,如果想要半持久化存储的话,要自己写逻辑做 dump 和 init 。
至于用的原因:
- MySQL 自带。如果原本就已经在用 MySQL 的话,使用更方便。
- 支持查询。能 SQL 联合查询简直不能更方便。

所以还是要看应用场景的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/303137

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX