基础知识之生成直方图的重要性

本文通过展示SQL*Plus命令与数据库性能优化的实操案例,深入探讨了如何利用SQL*Plus进行数据库表操作及性能分析,具体包括创建表、数据更新、表分析与统计信息收集等步骤,并通过对比不同参数设置下的执行计划,揭示了性能优化的关键因素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C:\>sqlplus ywbz/ywbzwater

SQL*Plus: Release 11.2.0.1.0 Production on 星期日 1月 20 20:55:49 2013

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
--创建测试表
SQL> create table t as select * from dba_objects;

表已创建。
--制造列值分布不均匀
SQL> update t set object_id=1 where object_id<2001;

已更新1932行。

SQL> show parameter dynamic

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling           integer     2
SQL> commit;

提交完成。
--表已分析未生成直方图
SQL> exec dbms_stats.gather_table_stats('YWBZ','T',method_opt=>'for all columns
size 1');

PL/SQL 过程已成功完成。

SQL> select num_rows,blocks from user_tables where table_name='T';

  NUM_ROWS     BLOCKS
---------- ----------
     73051       1065

SQL> set autotrace traceonly explain
SQL> select * from t where object_id=1;

执行计划
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    97 |   292   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |    97 |   292   (1)| 00:00:04 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=1)

SQL> set autotrace off;
--object_id值为1的实际数量为1932
SQL> select count(*) from t where object_id=1;

  COUNT(*)
----------
      1932
--收集表T的统计信息并生成直方图
SQL> set autotrace off;
SQL> exec dbms_stats.gather_table_stats('YWBZ','T',method_opt=>'for all columns
size 254');

PL/SQL 过程已成功完成。
--高度平衡直方图(254)
 
SQL> set autotrace traceonly explain

SQL> select * from t where object_id=1;

执行计划
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  1726 |   163K|   292   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| T    |  1726 |   163K|   292   (1)| 00:00:04 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=1)

SQL>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值