在工作中,我看到查询将更新具有相同值的表主键。这是一个例子。我做的时候对数据库性能的影响是什么 -
update t set id=1,content='ccc'
where id=1;
我希望索引不会被修改,因为键值相同。但是我相信它将使用更多资源,而不仅仅是更新内容列本身。我是对的吗?
Table t (id is primary key)
id content
1 xxx
2 yyy
答案 0 :(得分:0)
是的,你是正确的,不会修改索引,因为主键(自然索引)列具有与其索引相同的值。
而且,这次更新
update t set id=1,content='ccc'
where id=1;
比
更具资源消耗update t set content='ccc'
where id=1;
因为更新索引列比更新非索引列慢。
示例如下......
SQL> create table a ( b numeric, c varchar2(50) );
SQL> set timing on;
SQL> insert into a(b) select rownum from dual connect by level <= 1000000;
1000000 rows created.
Elapsed: 00:00:02.74
SQL> update a set c=ascii(b);
1000000 rows updated.
Elapsed: 00:00:46.72
SQL> commit;
Commit complete.
Elapsed: 00:00:00.03
SQL> truncate table a;
Table truncated.
Elapsed: 00:00:01.33
SQL> insert into a(b) select rownum from dual connect by level <= 1000000;
1000000 rows created.
Elapsed: 00:00:00.80
SQL> commit;
Commit complete.
Elapsed: 00:00:00.03
SQL> create index idx_c on a;
Index created.
Elapsed: 00:00:00.46
SQL> update a set c=ascii(b);
1000000 rows updated.
Elapsed: 00:01:53.12
SQL> commit;
Commit complete.
Elapsed: 00:00:00.04