Mysql - Archiving data, which solution to use?

被刻印的时光 ゝ 提交于 2020-03-05 11:01:12

问题


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

  1. keep last 1 month data in the same main table.
  2. quartely data/half yearly data in maintable_quartely table that holds the averaged or summarized data based on the report requirements
  3. 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

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