简体   繁体   English

在MYSQL上合并数据的最佳方法

[英]Best way to merge data on MYSQL

I have a table that is populated twice a day with data collected from other tables, and I use it to create some reports. 我有一个表,每天用两次从其他表收集的数据填充该表,并用它来创建一些报告。

The steps to achieve this is the following: Get all data that will be placed on the table, truncate the table and and then insert all data again. 实现此目的的步骤如下:获取将放置在表上的所有数据,截断表,然后再次插入所有数据。

Is it the best way to perform this in terms of performance? 就性能而言,这是最好的方法吗? Isn't there a way to updating only things that really changed, insert the new data and skip the rest? 是否没有办法仅更新真正更改的内容,插入新数据并跳过其余数据?

You can follow this for updating a table with merge: 您可以按照以下步骤通过合并更新表:

UPDATE multiple tables in MySQL using LEFT JOIN 使用LEFT JOIN更新MySQL中的多个表

To manage the UPDATE or INSERT you can use this method: 要管理UPDATE或INSERT,可以使用以下方法:

Insert into a MySQL table or update if exists 插入MySQL表或更新(如果存在)

Obviously all depends on your DB structure, what kind of data you are merging and, mainly, which keys are available. 显然,这一切都取决于您的数据库结构,要合并的数据类型以及主要是哪些密钥可用。

Regards 问候

CREATE TABLE new LIKE real;
INSERT INTO new ...;
RENAME TABLE real TO old, new TO real;
DROP TABLE old;

Pros/cons: 优点缺点:

  • You always have table real . 你总是有real表。 (The RENAME is 'instantaneous' and 'atomic'.) RENAME是“即时”和“原子”的。)
  • It is simple. 很简单。
  • It may be slower than a combination of UPDATE and INSERT , but does that really matter, based on my first comment? 可能UPDATEINSERT的组合要慢,但是根据我的第INSERT ,这真的很重要吗?

If there is more than you have let on, then see http://mysql.rjweb.org/doc.php/staging_table , which discusses other aspects of rapidly "ingesting" new data. 如果有更多内容,请参阅http://mysql.rjweb.org/doc.php/staging_table ,其中讨论了快速“摄取”新数据的其他方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM