collecting annual aggregated data for later quick access

流过昼夜 提交于 2020-12-15 20:04:56

问题


I have a number of sql queries which take year as a parameter and generate various annual reports for the given year.

Those queries are quite cumbersome and take a considerable amount of time to execute (20 min - 40 min).

In order to give my users the ability to view annual report whenever they need to, I am considering to pre-execute these queries and store the results for later use.

One solution would be to schedule execution of these queries and insert the results in some temp tables.

But I am looking for more clever approach one that would not involve writing dozens of execute immediate statements or custom inserts for all these queries.

Any idea would be appreciated. Also I don't know if materialized views can be used to that end.

expected result would be a table or a view with a year column so that a user could execute quick search for any year.

e.g.

product_id |annual_sales|max_price|min_price|year
124|1200,56|80|50|2019
124|1400,00|85|55|2020

回答1:


A materialized view would be a great option for what you are looking to do. This way you can write the query once for the view, then have the data in the materialized view refresh as often as you'd like. You can have a job that refreshes the data once per night, on the weekends, or whatever frequency you choose.

After the materialized view is created, you can also add indexes on top of the materialized view to assist with query performance if you so choose.

A quick example on how to create a materialized view can be seen below.

CREATE TABLE sale
(
    product_id     NUMBER,
    sale_date      DATE,
    sale_amount    NUMBER
);

INSERT INTO sale (product_id, sale_date, sale_amount)
     VALUES (124, DATE '2019-02-01', 40.25);

INSERT INTO sale (product_id, sale_date, sale_amount)
     VALUES (124, DATE '2019-02-01', 80.99);

INSERT INTO sale (product_id, sale_date, sale_amount)
     VALUES (124, DATE '2020-02-01', 30.50);

INSERT INTO sale (product_id, sale_date, sale_amount)
     VALUES (124, DATE '2020-02-01', 46.75);

CREATE MATERIALIZED VIEW sales_summary
BUILD IMMEDIATE
REFRESH FORCE ON DEMAND
AS
      SELECT product_id,
             SUM (sale_amount)                 AS annual_sales,
             MAX (sale_amount)                 AS max_price,
             MIN (sale_amount)                 AS min_price,
             EXTRACT (YEAR FROM sale_date)     AS year
        FROM sale
    GROUP BY product_id, EXTRACT (YEAR FROM sale_date);

Result

select * from sales_summary;

   PRODUCT_ID    ANNUAL_SALES    MAX_PRICE    MIN_PRICE    YEAR
_____________ _______________ ____________ ____________ _______
          124          121.24        80.99        40.25    2019
          124           77.25        46.75         30.5    2020


来源:https://stackoverflow.com/questions/64286481/collecting-annual-aggregated-data-for-later-quick-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!