views:

32

answers:

3

I have a hits table with millions of records. I need to show some charts on the data of the last month.

Does the solution of create a view something like :

CREATE VIEW ReportMonth AS SELECT * FROM Report 
WHERE DayDate > DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 30
DAY)  

is good in terms of performance ,

Is there a better solution like cron that create this table every some hours ?

thanks

+1  A: 

MySQL doesn't support materialized views but you can simulate it by just precreating the table yourself.

A suggestion of how to do this whilst minimising concurrency problems from here

CREATE TABLE new_materialized_view 
SELECT * from Report ...;

RENAME TABLE materialized_view TO old_materialized_view, 
new_materialized_view TO materialized_view;

DROP TABLE IF EXISTS old_materialized_view;

Hopefully someone else will tackle the scheduling options.

Martin Smith
i use it , only one comment : rename table syntax is rename ... TO .. not '='
Haim Evgi
@haim Thanks - I've amended my answer to reflect your comment.
Martin Smith
A: 

I do not think MySQL has such views but you need to copy to the new table. Also ensure that you have an index on the date field.

Vagmi Mudumbai
A: 

In MySQL the view is populated each time you make a query. So it does not give much performance improvement. You can opt for creating different tables (in case you don't have to update the data later or don't need to query them across tables/months) to improve the query performance.

Kangkan