简体   繁体   English

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

[英]MySQL - InnoDB, cache a part of a query for faster execution time

I have about 4 tables, one of them is about 10 millions rows ( it increase to about 500k lines per month ), the tables are fully optimized, my query is like this : 我有大约4个表,其中一个表大约有1000万行(每月增加到大约500k行),表已完全优化,我的查询是这样的:

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

This query takes about 1 minute execution time ( wich is far too long ) and what I want is to cache the first part of the query ( all but not the WHERE ) and then, once the cache is done, apply the where to the query cached. 该查询大约需要1分钟的执行时间(到现在为止太长了),我想要的是缓存查询的第一部分(所有但不是WHERE ),然后在缓存完成后将where应用于查询缓存。

The general idea is to execute the first part of the query every morning ( for exemple ) to put in cache this query to have minimal execution time when users will execute their own queries ( with the WHERE clause ) 一般的想法是每天早上(例如)执行查询的第一部分,以将该查询放入缓存中,以使用户执行自己的查询时(使用WHERE子句)的执行时间最短。

I think its possible because I tried for 'bechnmarking' to execute the query without WHERE ( about 1 minute execution time ), then I ran it with the WHERE clause and got a very low execution time so I think it seems working. 我认为这是可能的,因为我尝试了“ bechnmarking”以在不使用WHERE的情况下执行查询(大约1分钟的执行时间),然后使用WHERE子句运行了该查询,并且执行时间非常短,因此我认为它可以正常工作。

But I need help at this point, I dont know how to increase performances, how to put in the cache the query without the where or if you have a better solution... 但是我现在需要帮助,我不知道如何提高性能,如何将查询放入缓存,而又没有where或者您有更好的解决方案...

Thank you in advance for your attention 预先感谢您的关注

For query cache to work, queries must match exactly. 为了使查询缓存正常工作,查询必须完全匹配。 Consider creating a table, complete with indexes, then insert data from the first query with INSERT ... SELECT , then use that table. 考虑创建一个包含索引的表,然后使用INSERT ... SELECT从第一个查询插入数据,然后使用该表。

You could also use CREATE TABLE ... SELECT to make the table the first time. 您也可以使用CREATE TABLE ... SELECT来第一次CREATE TABLE ... SELECT

I think you can prepare Materialized view for this query 我认为您可以为此查询准备实体化视图

WHAT IS A MATERIALIZED VIEW? 什么是材料化视图? A Materialized View (MV) is the pre-calculated (materialized) result of a query. 物化视图(MV)是查询的预先计算(物化)结果。 Unlike a simple VIEW the result of a Materialized View is stored somewhere, generally in a table. 与简单的VIEW不同,物化视图的结果通常存储在某个表中。 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. MySQL本身不提供物化视图。 But it is easy to build Materialized Views yourself. 但是,自己构建物化视图很容易。

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

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