Q:
 
Hibernate generates UPDATE statements, which include all columns, regardless of whether I'm changing the value in that columns, eg:
tx.begin();
Item i = em.find(Item.class, 12345);
i.setA("a-value");
tx.commit();

issues this UPDATE statement:

update Item set A = $1, B = $2, C = $3, D = $4 where id = $5

so columns B, C, D are updated, while I didn't change them.

Say, Items are updated frequently and all columns are indexed. The question is: does it make sense to optimize the Hibernate part to something like this:

tx.begin();
em.createQuery("update Item i set i.a = :a where i.id = :id")
.setParameter("a", "a-value")
.setParameter("id", 12345)
.executeUpdate();
tx.commit();
What confuses me most is that the EXPLAIN plans of the 'unoptimized' and the 'optimized' query version are identical!
A:
 

Due to PostgreSQL MVCC, an UPDATE is effectively a DELETE plus an INSERT. (To be precise, the "deleted" row is just invisible to any transaction starting after the delete and vacuumed later.) Therefore, on the database side, including index manipulation, there is in effect no difference between the two statements. It increases network traffic a bit (depending on your data) and needs a bit of parsing.

I studied HOT updates after araqnid's input and ran some tests. Updates on columns that don't actually change the value make no difference whatsoever as far as HOT updates are concerned. My answer holds. See details below.

However, if you use per-column triggers (introduced with v9.0), this my have undesired side effects!

I quote the manual on triggers:

... a command such as UPDATE ... SET x = x ... will fire a trigger on column x, even though the column's value did not change.

Abstraction layers are for convenience. They are useful for SQL-illiterate developers or if the application needs to be portable between different RDBMS. On the downside, they can butcher performance and introduce additional points of failure. I avoid them wherever possible.

Concerning HOT (Heap-only tuple) updates

Heap-Only Tuples were introduced with Postgres 8.3, with important improvements in 8.3.4 and 8.4.9.
The release notes for Postgres 8.3:

UPDATEs and DELETEs leave dead tuples behind, as do failed INSERTs. Previously only VACUUM could reclaim space taken by dead tuples. 
With HOT dead tuple space can be automatically reclaimed at the time of INSERT or UPDATE if no
changes are made to indexed columns. This allows for more consistent performance. Also, HOT avoids adding duplicate index entries.

Emphasis mine. And "no changes" includes cases where columns are updated with the same value as they already hold. I actually tested that just now, as I wasn't sure.

You don't have to take my word for it. See for yourself, Postgres provides a couple of functions to check statistics. Run your UPDATE with and without all columns and check if it makes any difference.

-- Number of rows HOT-updated in table:
SELECT pg_stat_get_tuples_hot_updated('table_name'::regclass::oid) -- Number of rows HOT-updated in table, in the current transaction:
SELECT pg_stat_get_xact_tuples_hot_updated('table_name'::regclass::oid)

Or use pgAdmin. Select your table and inspect the "Statistics" tab in the main window.

Be aware that HOT updates are only when there is room for the new tuple version on the same page. One simple way to force that condition is to test with a small table that holds only a few rows. Page size is typically 8k, so there must be free space on the page.

其中araqnid论证的过程如下:

create temp table t1(t1_id serial primary key, reference varchar(16) not null unique, value varchar(16) not null);
copy t1(reference, value) from stdin;
FOO foo
BAR bar
QUUX quux
\. create temp view t1_combined as
select t1_id, reference, value, ctid, lp_flags, lp_off, case when t_ctid <> ctid then t_ctid end as t_ctid,
t_xmin, xmin_visible, case when t_xmax::text <> '' then t_xmax end as t_xmax, xmax_visible,
xmin_visible and (xmax_visible is null or not xmax_visible or t_locked <> '') as visible, t_hot_updated, t_heap_only
from (select *,
t_xmin_valid and txid_visible_in_snapshot(t_xmin::text::bigint, txid_current_snapshot()) as xmin_visible,
t_xmax_valid and txid_visible_in_snapshot(t_xmax::text::bigint, txid_current_snapshot()) as xmax_visible
from (select ('(' || 0 || ',' || lp || ')')::tid as ctid,
lp, lp_off, case lp_flags when 0 then 'UNUSED' when 1 then 'NORMAL' when 2 then 'REDIRECT' when 3 then 'DEAD' end as lp_flags,
lp_len, t_xmin, t_xmax, t_field3, t_ctid, (t_infomask&1)<>0 as t_hasnull, (t_infomask&2)<>0 as t_hasvarwidth,
(t_infomask&4)<>0 as t_hasexternal, (t_infomask&8)<>0 as t_hasoid, (t_infomask&32)<>0 as t_combocid,
case t_infomask & 192 when 64 then 'EXCL' when 128 then 'SHARE' when 0 then '' when 192 then 'INVALID' end as t_locked,
(t_infomask&256)<>0 as t_xmin_committed, (t_infomask&512)=0 as t_xmin_valid,
(t_infomask&1024)<>0 as t_xmax_committed, (t_infomask&2048)=0 as t_xmax_valid,
(t_infomask&4096)<>0 as t_xmax_is_multi, (t_infomask&8192)<>0 as t_updated,
(t_infomask&16384)<>0 as t_moved_off, (t_infomask&32768)<>0 as t_moved_in,
t_infomask2&2047 as t_natts, (t_infomask2&16384)<>0 as t_hot_updated,
(t_infomask2&32768)<>0 as t_heap_only,
t_hoff, t_bits, t_oid
from heap_page_items(get_raw_page('t1', 0))) format_heap_page_items
) heap
full outer join (select ctid, * from t1) t1 using (ctid); create temp view t1_indices as
select ctid, pkey_content.itemoffset as pkey_itemoffset, pkey_content.data as pkey_data, auxkey_content.itemoffset as auxkey_itemoffset, auxkey_content.data as auxkey_data
from bt_page_items('t1_pkey', 1) pkey_content
full outer join bt_page_items('t1_reference_key', 1) auxkey_content using (ctid); \echo ********************************************************************************
\echo * Initial table
\echo
select * from t1_combined;
select * from t1_indices; \echo ********************************************************************************
\echo * Update non-indexed column
\echo * - index entries untouched
\echo * - old tuple at ctid (0,1) has t_hot_updated set
\echo * - new tuple at ctid (0,4) has t_heap_only set
\echo * - t_ctid of (0,1) points to (0,4)
\echo begin;
update t1 set value = 'mumble' where t1_id = 1;
end; select * from t1_combined;
select * from t1_indices; \echo ********************************************************************************
\echo * Update non-indexed column again
\echo * - tuple at ctid (0,4) now just points to ctid (0,5) and is redundant
\echo begin;
update t1 set value = 'womble' where t1_id = 1;
end; select * from t1_combined;
select * from t1_indices; \echo ********************************************************************************
\echo * Vacuum table
\echo * - line pointer ctid (0,1) converted to REDIRECT since index entries still point to it
\echo * - redundant tuple at ctid (0,4) reclaimed for reuse
\echo vacuum t1; select * from t1_combined;
select * from t1_indices; \echo ********************************************************************************
\echo * Update indexed column
\echo * - New index entries written for new tuple at ctid (0,4) which is now reused
\echo update t1 set reference = 'WOMBLE' where t1_id = 1; select * from t1_combined;
select * from t1_indices; \echo ********************************************************************************
\echo * Update indexed column to contain same value
\echo * - even though indexed column is mentioned in update, this makes a heap-only change
\echo * - current version is now (0,6) but indices still indicate (0,4)
\echo update t1 set reference = 'WOMBLE', value = 'womble2' where t1_id = 1; select * from t1_combined;
select * from t1_indices; \echo ********************************************************************************
\echo * Vacuum table
\echo * - ctid (0,1) now reclaimed, index entries pointing to it removed
\echo * - ctid (0,5) reclaimed too, it never had index entries pointing to it
\echo vacuum t1; select * from t1_combined;
select * from t1_indices;

执行结果可以根据脚本自测。在此不再列出。

注:
HOT中,即使是更新加有索引的一列,如果更新的数值不变,也不会产生新的index 记录的。

参考:https://stackoverflow.com/questions/7806058/redundant-data-in-update-statements/7806610#7806610

Redundant data in update statements的更多相关文章

  1. Map Columns From Different Tables and Create Insert and Update Statements in Oracle Forms

    This is one of my most needed tool to create Insert and Update statements using select or alias from ...

  2. spring data jpa update

    一:在controller  加上: @Controller @RequestMapping("/user") public class UserController { @Aut ...

  3. [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...

  4. INSERT ... ON DUPLICATE KEY UPDATE Syntax

    一 mybatis中返回自动生成的id 当有时我们插入一条数据时,由于id很可能是自动生成的,如果我们想要返回这条刚插入的id怎么办呢.在mysql数据中我们可以在insert下添加一个selectK ...

  5. Data Types

    原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...

  6. Data Block Compression

    The database can use table compression to eliminate duplicate values in a data block. This section d ...

  7. How To Commit Just One Data Block Changes In Oracle Forms

    You have an Oracle Form in which you have multiple data blocks and requirement is to commit just one ...

  8. Indexing Sensor Data

    In particular embodiments, a method includes, from an indexer in a sensor network, accessing a set o ...

  9. INSERT ... ON DUPLICATE KEY UPDATE Syntax 专题

    ON DUPLICATE KEY UPDATE :不用用于批量,除 insert into t1  select * from t2 on duplicated key update k1=v1,k2 ...

随机推荐

  1. 经典教程|10 分钟速成 Python3

    Python 是由吉多·范罗苏姆(Guido Van Rossum)在 90 年代早期设计. 它是如今最常用的编程语言之一.它的语法简洁且优美,几乎就是可执行的伪代码. 注意:这篇教程是基于 Pyth ...

  2. JY播放器【蜻蜓FM电脑端,附带下载功能】

    今天给大家带来一款神器----JY播放器.可以不用打开网页就在电脑端听蜻蜓FM的节目,而且可以直接下载,对于我这种强迫症患者来说真的是神器.我是真的不喜欢电脑任务栏上面密密麻麻. 目前已经支持平台(蜻 ...

  3. Java如何调用shell脚本的

    有些时候会碰到这样的场景:java的功能里面要嵌入一个功能点,这个功能是通过是shell脚本实现的.这种时候就需要Java对脚本调用的支持了. 测试环境 Ubuntu16.04 i3-6100,12G ...

  4. 【第八章】MySQL数据库备份—逻辑备份

    一.数据库备份 1.命令简介: # mysqldump -h 服务器 -u用户名 -p密码 数据库名 > 备份文件.sql1)关于数据库名: -A, --all-databases       ...

  5. 算法笔记(c++)--01背包问题

    算法笔记(c++)--经典01背包问题 算法解释起来太抽象了.也不是很好理解,最好的办法就是一步步写出来. 背包问题的核心在于m[i][j]=max(m[i-1][j],m[i-1][j-w[i]]+ ...

  6. ubuntu docker 安装

    1.安装环境 Ubuntu16.04 安装 升级docker .docker-compose.docker-machine Docker 有两个版本 docker-ce 社区版和docker-ee企业 ...

  7. ViewPort <meta>标记

    ViewPort <meta>标记用于指定用户是否可以缩放Web页面,如果可以,那么缩放到的最大和最小缩放比例是什么.使用ViewPort <meta>标记还表示文档针对移动设 ...

  8. centos7.2 apache开启.htaccess

    打开httpd.conf(在那里? APACHE目录的CONF目录里面),用文本编纂器打开后,查找 (1) AllowOverride None 改为 AllowOverride All (2)去掉下 ...

  9. [C++] Solve "No source available for main()" error when debugging on Eclipse

    In Mac, the issue image: 1. A existing cmake project on disk 2. import this project into Eclipse. 3 ...

  10. 4个数的和为0 51nod 1267

    给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No". Input 第1行,1个数N,N为数组的长度(4 < ...