Aggregate data tables

前端 未结 1 1747
时光取名叫无心
时光取名叫无心 2021-02-15 14:40

I am building a front-end to a largish db (10\'s of millions of rows). The data is water usage for loads of different companies and the table looks something like:



        
1条回答
  •  北荒
    北荒 (楼主)
    2021-02-15 15:35

    This functionality would be best served by a feature called materialized view, which MySQL unfortunately lacks. You could consider migrating to a different database system, such as PostgreSQL.

    There are ways to emulate materialized views in MySQL using stored procedures, triggers, and events. You create a stored procedure that updates the aggregate data. If the aggregate data has to be updated on every insert you could define a trigger to call the procedure. If the data has to be updated every few hours you could define a MySQL scheduler event or a cron job to do it.

    There is a combined approach, similar to your option 3, that does not depend on the dates of the input data; imagine what would happen if some new data arrives a moment too late and does not make it into the aggregation. (You might not have this problem, I don't know.) You could define a trigger that inserts new data into a "backlog," and have the procedure update the aggregate table from the backlog only.

    All these methods are described in detail in this article: http://www.fromdual.com/mysql-materialized-views

    0 讨论(0)
提交回复
热议问题