MySQL - InnoDB,缓存查询的一部分以加快执行时间

标签 mysql sql performance caching innodb

我有大约 4 个表,其中一个表大约有 1000 万行(它每月增加到大约 50 万行),这些表已完全优化,我的查询是这样的:

SELECT COUNT(id), select... FROM table1
INNER JOIN table2 on ...,
INNER JOIN table3 on ...
INNER JOIN table 4 on...
WHERE [ different conditions every time ]

这个查询需要大约 1 分钟的执行时间(这太长了),我想要的是缓存查询的第一部分(除了 WHERE 以外的所有内容),然后,一旦缓存完成后,将 where 应用于缓存的查询。

一般的想法是每天早上(例如)执行查询的第一部分,以将此查询放入缓存中,以便在用户执行自己的查询(使用 WHERE子句)

我认为这是可能的,因为我尝试让“bechnmarking”在没有 WHERE 的情况下执行查询(大约 1 分钟的执行时间),然后我使用 WHERE 子句运行它并且执行时间非常短,所以我认为它似乎有效。

但此时我需要帮助,我不知道如何提高性能,如何在没有 where 的情况下将查询放入缓存,或者您是否有更好的解决方案...

提前感谢您的关注

最佳答案

我想你可以准备Materialized view对于这个查询

WHAT IS A MATERIALIZED VIEW? A Materialized View (MV) is the pre-calculated (materialized) result of a query. Unlike a simple VIEW the result of a Materialized View is stored somewhere, generally in a table. Materialized Views are used when immediate response is needed and the query where the Materialized View bases on would take to long to produce a result. Materialized Views have to be refreshed once in a while. It depends on the requirements how often a Materialized View is refreshed and how actual its content is. Basically a Materialized View can be refreshed immediately or deferred, it can be refreshed fully or to a certain point in time. MySQL does not provide Materialized Views by itself. But it is easy to build Materialized Views yourself.

关于MySQL - InnoDB,缓存查询的一部分以加快执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21146357/

相关文章:

PostgreSQL 表比较

php - propel 2,mysql - 反向没有错误,没有创建 schema.xml

mysql - 在 eclipse 中创建连接 - ClassNotFoundException : com. mysql.jdbc.Driver

sql - 使用jackc/pgx将空字符串或null作为null插入到postgres中

sql - Oracle SQL开发人员: stop script running if error

performance - Perl 中的慢循环

MySQL 从两个表与一个左连接

php - 在不刷新站点的情况下重新加载 div

sql - 使用不同参数多次执行参数化 liquibase 变更集

css - 如何在chrome中获取渲染时间time