问题
I am using MySQL 5.x and there is one main table that has the stats combined for all customers. I would like to run reporting on it but dont want to run it on the same table so Im thinking of every night moving the data to a different table that will only be used for reporting. My question is would it be beneficial to have a seperate table for each customer to archive too or just have it just one archive table for all customers? There could be thousands of customers in the system which could mean thousands of archive tables if I decide to break it up by customer. Your thoughts?
回答1:
If you use the individual for each customer, tables will grow in number.
If you have statistics data then i suggest to summarize it like below
- keep last 1 month data in the same main table.
- quartely data/half yearly data in maintable_quartely table that holds the averaged or summarized data based on the report requirements
- remaing old data in maintable_archieve which holds > quartely or half yearly data averaged or summarized based on the report requirements
If you want the whole thing together you need to combine three tables.
other way, use mysql replication and master for insert,update,delete and slave for select
回答2:
I see this as all as a way of organizing your data; if you're dealing with things like customers, and orders, they should be separate tables, anyways.
Depending on the resources available, you could probably do something like
CREATE TEMPORARY TABLE `new_table` LIKE `old_table`;
INSERT INTO `new_table` SELECT * FROM `old_table`;
Where you would have a perfect copy of the table you would like to get the report to work on. If your resources are limited, you could probably batch it, like so:
CREATE TEMPORARY TABLE `new_table` LIKE `old_table`;
INSERT INTO `new_table` SELECT * FROM `old_table` LIMIT 100;
...and on the next iteration:
DROP TABLE `new_table`;
CREATE TEMPORARY TABLE `new_table` LIKE `old_table`;
INSERT INTO `new_table` SELECT * FROM `old_table` LIMIT 100 OFFSET 100;
As far as archiving the information goes, it depends on the environment you are working in. For example, in the environment my stuff works in, it's 24/7 and extremely high-demand. I have an interim data engine which keeps all the data in an in-RAM database, and when a record is seen as "closed", it is moved into a MySQL database for archiving. This way, when the need be, I can run whatever reports on the MySQL database, without impacting the operational run-time of the live data.
I can offer some ideas on how to archive your data, but that would require you to put forth a description of the type of volume and demand you're dealing with.
来源:https://stackoverflow.com/questions/5215216/mysql-archiving-data-which-solution-to-use