更新的列数是否会影响Oracle中更新查询的性能

时间:2015-04-29 10:42:49

标签: sql oracle performance

我有一个Oracle更新查询,如下所示

查询1:UPDATE Table1 set col1=value1 where tablekey=123

查询2:UPDATE Table1 set col1=value1,col2=value2 where tablekey=123

这两者之间会有性能差异吗?

3 个答案:

答案 0 :(得分:2)

除了字节数以外没什么。

您可以查看 EXPLAIN PLAN

SQL> EXPLAIN PLAN FOR UPDATE EMP SET ENAME='A' WHERE EMPNO=7369;

Explained.

SQL>
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1494045816

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | UPDATE STATEMENT   |      |     1 |    10 |     3   (0)| 00:00:01 |
|   1 |  UPDATE            | EMP  |       |       |            |          |
|*  2 |   TABLE ACCESS FULL| EMP  |     1 |    10 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------

   2 - filter("EMPNO"=7369)

14 rows selected.

SQL>
SQL> EXPLAIN PLAN FOR UPDATE EMP SET ENAME='A', DEPTNO=30 WHERE EMPNO=7369;

Explained.

SQL>
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1494045816

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | UPDATE STATEMENT   |      |     1 |    13 |     3   (0)| 00:00:01 |
|   1 |  UPDATE            | EMP  |       |       |            |          |
|*  2 |   TABLE ACCESS FULL| EMP  |     1 |    13 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------

   2 - filter("EMPNO"=7369)

14 rows selected.

有关详细信息,您可以上设置自动跟踪:

SQL> set autotrace on
SQL> UPDATE EMP SET ENAME='A' WHERE EMPNO=7369;

1 row updated.


Execution Plan
----------------------------------------------------------
Plan hash value: 1494045816

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | UPDATE STATEMENT   |      |     1 |    10 |     3   (0)| 00:00:01 |
|   1 |  UPDATE            | EMP  |       |       |            |          |
|*  2 |   TABLE ACCESS FULL| EMP  |     1 |    10 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("EMPNO"=7369)


Statistics
----------------------------------------------------------
          4  recursive calls
          1  db block gets
          3  consistent gets
          0  physical reads
        352  redo size
        857  bytes sent via SQL*Net to client
        842  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> UPDATE EMP SET ENAME='A', DEPTNO=30 WHERE EMPNO=7369;

1 row updated.


Execution Plan
----------------------------------------------------------
Plan hash value: 1494045816

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | UPDATE STATEMENT   |      |     1 |    13 |     3   (0)| 00:00:01 |
|   1 |  UPDATE            | EMP  |       |       |            |          |
|*  2 |   TABLE ACCESS FULL| EMP  |     1 |    13 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("EMPNO"=7369)


Statistics
----------------------------------------------------------
          4  recursive calls
          1  db block gets
          3  consistent gets
          0  physical reads
        316  redo size
        858  bytes sent via SQL*Net to client
        853  bytes received via SQL*Net from client
          3  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL>

因此,您会发现 bytes sent via SQL*Net to client bytes received via SQL*Net from client 之间存在细微差别。对于更多列,差异会稍微增加。

答案 1 :(得分:1)

在这个例子中,表TT1和TT2在第一次更新中都具有相同的数据我只更新了1列,而在其他更新中我更新了3列,(两个表都没有任何触发器)。你可以看到几乎相同的时间。

*该表仅包含模拟数据。

SQL> UPDATE TT1 SET EMPNO=1234 WHERE EMPNO=7839 ;

65536 rows updated.

Elapsed: 00:00:06.65

SQL> UPDATE TT2 SET EMPNO=1234,SAL=1000,DEPTNO=15 WHERE EMPNO=7839 ;

65536 rows updated.

Elapsed: 00:00:05.76
SQL>

答案 2 :(得分:0)

在您的更新查询之上放置以下查询,它将为您提供执行查询和SQL Server解析和编译时所需的时间。

  

SET STATISTICS TIME ON