简体   繁体   English

用于 MySQL 的 MEMORY 存储引擎的替代品

[英]Alternatives to the MEMORY storage engine for MySQL

I'm currently running some intensive SELECT queries against a MyISAM table.我目前正在针对 MyISAM 表运行一些密集的 SELECT 查询。 The table is around 100 MiB (800,000 rows) and it never changes.该表大约有 100 MiB(800,000 行)并且它永远不会改变。

I need to increase the performance of my script, so I was thinking on moving the table from MyISAM to the MEMORY storage engine, so I could load it completely into the memory.我需要提高脚本的性能,所以我正在考虑将表从 MyISAM 移动到 MEMORY 存储引擎,这样我就可以将它完全加载到 memory 中。

Besides the MEMORY storage engine, what are my options to load a 100 MiB table into the memory?除了 MEMORY 存储引擎之外,还有哪些选项可以将 100 MiB 表加载到 memory 中?

A table with 800k rows shouldn't be any problem to mysql, no matter what storage engine you are using.无论您使用什么存储引擎,对于 mysql 来说,一个有 800k 行的表应该没有任何问题。 With a size of 100 MB the full table (data and keys) should live in memory (mysql key cache, OS file cache, or propably in both).大小为 100 MB 的完整表(数据和键)应位于 memory(mysql 键缓存、操作系统文件缓存或两者中)。

First you check the indices.首先,您检查索引。 In most cases, optimizing the indices gives you the best performance boost.在大多数情况下,优化索引会给您带来最佳的性能提升。 Never do anything else, unless you are pretty sure they are in shape.永远不要做任何其他事情,除非你很确定它们的形状。 Invoke the queries using EXPLAIN and watch for cases where no or the wrong index is used.使用EXPLAIN调用查询并注意未使用索引或使用错误索引的情况。 This should be done with real world data and not on a server with test data.这应该使用真实世界的数据而不是在具有测试数据的服务器上完成。

After you optimized your indices the queries should finish by a fraction of a second.优化索引后,查询应在几分之一秒内完成。 If the queries are still too slow then just try to avoid running them by using a cache in your application (memcached, etc.).如果查询仍然太慢,那么只需尝试通过在应用程序中使用缓存(memcached 等)来避免运行它们。 Given that the data in the table never changes there shouldn't be any problems with old cache data etc.鉴于表中的数据永远不会改变,旧的缓存数据等应该没有任何问题。

Assuming the data rarely changes, you could potentially boost the performance of queries significantly using MySql query caching .假设数据很少更改,您可以使用MySql 查询缓存来显着提高查询的性能。

If your table is queried a lot it's probably already cached at the operating system level, depending on how much memory is in your server.如果您的表被大量查询,它可能已经在操作系统级别缓存,具体取决于您的服务器中有多少 memory。

MyISAM also allows for preloading MyISAM table indices into memory using a mechanism called the MyISAM Key Cache . MyISAM 还允许使用称为MyISAM 密钥缓存的机制将 MyISAM 表索引预加载到 memory 中。 After you've created a key cache you can load an index into the cache using the CACHE INDEX or LOAD INDEX syntax.创建键缓存后,您可以使用CACHE INDEXLOAD INDEX语法将索引加载到缓存中。

I assume that you've analyzed your table and queries and optimized your indices after the actual queries?我假设您已经分析了您的表和查询并在实际查询之后优化了您的索引? Otherwise that's really something you should do before attempting to store the entire table in memory.否则,在尝试将整个表存储在 memory 中之前,您确实应该这样做。

If you have enough memory allocated for Mysql's use - in the Innodb buffer pool, or for use by MyIsam, you can read the database into memory (just a 'SELECT * from tablename') and if there's no reason to remove it, it stays there.如果您有足够的 memory 分配给 Mysql 使用 - 在 Innodb 缓冲池中,或供 MyIsam 使用,您可以将数据库读入 memory(只是一个 'SELECT * from tablename'),如果没有理由删除它,它会保留那里。

You also get better key use, as the MEMORY table only does hash-bashed keys, rather than full btree access, which for smaller, non-unique keys might be fats enough, or not so much with such a large table.您还可以获得更好的密钥使用,因为 MEMORY 表只执行哈希键,而不是完整的 btree 访问,对于较小的非唯一键可能足够胖,或者对于这么大的表来说不够。

As usual, the best thing to do it to benchmark it.像往常一样,最好的做法是对其进行基准测试。

Another idea is, if you are using v5.1, to use an ARCHIVE table type, which can be compressed, and may also speed access to the contents, if they are easily compressible.另一个想法是,如果您使用的是 v5.1,则使用 ARCHIVE 表类型,它可以被压缩,并且还可以加快对内容的访问速度,如果它们易于压缩的话。 This swaps the CPU time to de-compress for IO/memory access.这会交换 CPU 时间来解压缩以进行 IO/内存访问。

If the data never changes you could easily duplicate the table over several database servers.如果数据从不改变,您可以轻松地将表复制到多个数据库服务器上。

This way you could offload some queries to a different server, gaining some extra breathing room for the main server.这样,您可以将一些查询卸载到不同的服务器,为主服务器获得一些额外的喘息空间。

The speed improvement depends on the current database load, there will be no improvement if your database load is very low.速度提升取决于当前的数据库负载,如果您的数据库负载非常低,则不会有任何提升。

PS: PS:
You are aware that MEMORY tables forget their contents when the database restarts!您知道 MEMORY 表在数据库重新启动时会忘记它们的内容!

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

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