简体   繁体   English

更有效的更新数据库的方法

[英]Most efficient way to update database

I have a table that is auto-updating from time to time (say daily). 我有一个不时自动更新的表(比如说每天)。 All updated fields are of type TEXT , and might have lots of data. 所有更新的字段都是TEXT类型,可能包含大量数据。 What I definitely know is that the data will not change a lot. 我绝对知道的是,数据不会发生太大变化。 Usually up to 30 characters added or deleted. 通常最多添加或删除30个字符。 So what would be more efficient? 那会更有效率呢? To merge somehow the changes or delete the old data and retrieve the new one? 以某种方式合并更改或删除旧数据并检索新数据? And, if the merge way is the way to do it, how should I do that? 并且,如果合并方式是这样做的,我该怎么做? Is there any keyword or something in order to make this easier and more efficient? 是否有任何关键字或其他东西,以使这更容易和更有效?

PS I am completely new to databases in general, it's the very first time I ever create and use a database, so sorry if it is a silly question PS我对数据库一般都是新手,这是我第一次创建和使用数据库,很抱歉,如果这是一个愚蠢的问题

Due to the MVCC model, PostgreSQL always writes a new row for any set of changes applied in a single UPDATE . 由于MVCC模型,PostgreSQL 总是为在单个UPDATE应用的任何更改集写入新行。 Doesn't matter, how much you change. 没关系,你改变了多少。 There is no "merge way". 没有“合并方式”。

It's similar to (but not the same as) deleting the row and inserting a new one. 它与删除行并插入新行类似(但不相同)。

Since your columns are obviously big, they are going to be TOASTed , meaning they are compressed and stored out-of-line in a separate table. 由于您的列显然很大,它们将被转换TOASTed ,这意味着它们被压缩并在单独的表中存储。 In an UPDATE , these columns can be preserved as-is if they remain unchanged, so it's considerably cheaper to UPDATE than to DELETE and INSERT . UPDATE ,如果它们保持不变,这些列可以按原样保留,因此UPDATEDELETEINSERT便宜得多。 Quoting the manual here 在这里引用手册

During an UPDATE operation, values of unchanged fields are normally preserved as-is; UPDATE操作期间,未更改字段的值通常保持原样; so an UPDATE of a row with out-of-line values incurs no TOAST costs if none of the out-of-line values change. 因此,如果没有任何外部值发生更改,则具有外部值的行的UPDATE不会产生任何TOAST成本。

If your rows have lots of columns and only some get updated a lot, it might pay to have two separate tables with a 1:1 relationship. 如果您的行有很多列,并且只有一些列得到更新,那么可能需要两个具有1:1关系的单独表。 But that's an extreme case. 但这是一个极端的例子。

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

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