Trying to learn mysql and having tons of other material to learn, I'm considering skipping memory tables, since they have many special characteristics to consider especially that they vanish, after server restart.
In production environments, what are they used for?
1 Answer
If you are learning MySQL, skipping MEMORY storage engine is not a bad idea. InnoDB is the default storage engine and it is suitable for most cases. Only in edge cases it makes sense to use another one, and you probably shouldn't consider this option before knowing InnoDB quite well.
However, to answer your question, first let's check pro's and con's of MEMORY.
Cons:
- You could lose your data (except for table definition).
- No transactions, no consistency of any kind.
- Only supports
HASH
indexes, so there is no optimisation for queries likeWHERE col > n
,ORDER BY
orGROUP BY
. - Massive usage consumes memory. No trivial way to monitor how much memory you are using for these tables.
Pros:
- Not dramatically faster than InnoDB (in terms of latency), but still generally faster when used properly.
- Better throughput because you don't talk to storage devices.
So the question becomes, which use cases don't suffer because of the cons and take advantage of the pro's? Well, a first obvious generic answer is, avoid all use cases where data loss is a problem. More specifically, you could consider MEMORY for:
- Caches: all queries are point searches, data can always be reconstructed.
- Speed up some massive insertion. You put data into a MEMORY table temporarily, do your transformations, and copy the rows to a "regular" table. But if your transformations require
BTREE
indexes, this is not a good option. TEMPORARY
tables, when you don't needBTREE
indexes.
-
Another CON: Carving out RAM for Memory table(s) takes away from using the RAM for a bigger InnoDB buffer_pool, thereby slowing down all InnoDB operations by some amount (hard to predict how much). Mar 6, 2018 at 15:08
-
Yeah but, to some extent, this is true for every storage engine we use Mar 6, 2018 at 22:16