【性能优化】直方图

简介: SQL>SQL> begin  2         for i in 1..10000 loop  3           insert into hist values (i, i);  4         end loop;  5         commit;  6       end;  7       / PL/SQL 过程已成功完成。

SQL>
SQL> begin
  2         for i in 1..10000 loop
  3           insert into hist values (i, i);
  4         end loop;
  5         commit;
  6       end;
  7       /

PL/SQL 过程已成功完成。
SQL> update hist set b=5 where b between 6 and 9995;
已更新9990行。
SQL> commit;
提交完成。
SQL> create index i_hist_b on tab(b);
索引已创建。
然后分析表,强制使列B不产生直方图。
BEGIN
  DBMS_STATS.GATHER_TABLE_STATS(OWNNAME    => USER,
                                TABNAME    => 'HIST',
                                CASCADE    => TRUE,
                                METHOD_OPT => 'FOR  COLUMNS B SIZE 1 ');
END;
查看视图USER_TAB_HISTOGRAMS,列B上只有最大值,最小值两条记录分别对应端点号(endpoint_number)0和1,这种显示说明列B没有直方

图信息。
SQL>SELECT table_name,column_name,endpoint_number,endpoint_value FROM USER_TAB_HISTOGRAMS WHERE TABLE_NAME='TAB' ;

TABLE_NAME                     COLUMN_NAME                              ENDPOINT_NUMBER ENDPOINT_VALUE
------------------------------ ---------------------------------------- --------------- --------------
HIST                            B                                                      0              1
HIST                            B                                                      1          10000

在没有直方图的情况下,在B列上进行等值查询的时候,都是索引范围扫描。
-----------------------------------------------------------------
SQL> select * from hist where b =1;
Elapsed: 00:00:00.01
Execution Plan
----------------------------------------------------------
Plan hash value: 1911084455
----------------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |  1000 |  6000 |     4   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| HIST     |  1000 |  6000 |     4   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | I_HIST_B |  1000 |       |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("B"=1)
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          4  consistent gets
          0  physical reads
          0  redo size
        570  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> select * from hist where b =5;
9991 rows selected.
Elapsed: 00:00:00.07
Execution Plan
----------------------------------------------------------
Plan hash value: 1911084455
----------------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |  1000 |  6000 |     4   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| HIST     |  1000 |  6000 |     4   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | I_HIST_B |  1000 |       |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("B"=5)
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       1369  consistent gets
          0  physical reads
          0  redo size
     212165  bytes sent via SQL*Net to client
       7818  bytes received via SQL*Net from client
        668  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
       9991  rows processed

-----生成直方图 再次收集统计信息时 务必清除之前的统计信息。否则执行计划会利用原来的统计信息
SQL> BEGIN
  2  DBMS_STATS.GATHER_TABLE_STATS(OWNNAME    => user,
  3  TABNAME    => 'HIST',
  4  CASCADE    => TRUE,
  5  METHOD_OPT => 'FOR ALL COLUMNS  SIZE AUTO ');
  6  END;
  7  /

PL/SQL procedure successfully completed.
Elapsed: 00:00:00.28

SQL> select * from hist where b=1;
Elapsed: 00:00:00.01
Execution Plan
----------------------------------------------------------
Plan hash value: 1911084455
----------------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |     1 |     6 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| HIST     |     1 |     6 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | I_HIST_B |     1 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("B"=1)
Statistics
----------------------------------------------------------
        150  recursive calls
          0  db block gets
         24  consistent gets
          0  physical reads
          0  redo size
        570  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> select * from hist where b =5;
9991 rows selected.
Elapsed: 00:00:00.06
Execution Plan
----------------------------------------------------------
Plan hash value: 1745918543
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  9991 | 59946 |     6   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| HIST |  9991 | 59946 |     6   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("B"=5)
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        688  consistent gets
          0  physical reads
          0  redo size
     212165  bytes sent via SQL*Net to client
       7818  bytes received via SQL*Net from client
        668  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
       9991  rows processed
-------------------------------------------
SQL> SELECT TABLE_NAME, COLUMN_NAME, ENDPOINT_NUMBER, ENDPOINT_VALUE FROM USER_TAB_HISTOGRAMS
  2  WHERE TABLE_NAME = 'HIST';

TABLE_NAME  COLUMN_NAME  ENDPOINT_NUMBER ENDPOINT_VALUE
----------- ------------ --------------  -------------- ---
HIST         B                1              1      
HIST         B                2              2
HIST         B                3              3
HIST         B                4              4
HIST         B               9995              5
HIST         B               9996           9996
HIST         B               9997           9997
HIST         B               9998           9998
HIST         B               9999           9999
HIST         B               10000          10000
HIST         A                0              1
HIST         A                1              10000

12 rows selected.

Elapsed: 00:00:00.00

目录
相关文章
|
4月前
|
存储 关系型数据库 MySQL
【性能优化】MySql查询性能优化必知必会
【性能优化】MySql查询性能优化必知必会
83 0
【性能优化】MySql查询性能优化必知必会
|
6月前
|
编解码 缓存 自然语言处理
UITablew性能优化之栅格化
UITablew性能优化之栅格化
26 0
|
监控 算法 测试技术
性能优化之几种常见压测模型及优缺点 | 陈显铭
上一篇讲的是《性能优化的常见模式及趋势》,今天接着讲集中常见的压测模型。通过上一章我们大概知道了性能优化的一些招式,但是怎么发现有性能问题,常见的模式还是需要压测。
5729 0
|
8天前
|
缓存 并行计算 数据可视化
Matplotlib性能优化:提升图表渲染速度
【4月更文挑战第17天】提升 Matplotlib 渲染速度的技巧:1) 减少数据点;2) 使用矢量化操作;3) 减少图表元素;4) 增量渲染;5) 优化图像保存;6) 更换更快的后端;7) 并行处理;8) 避免循环内绘图;9) 利用缓存;10) 使用专业图形工具。注意根据具体需求调整优化策略。
|
4月前
|
机器学习/深度学习 并行计算 Android开发
Int8量化算子在移动端CPU的性能优化
Int8量化算子在移动端CPU的性能优化
|
9月前
|
缓存 负载均衡 固态存储
MySQL优化心得:提升性能的关键步骤
MySQL作为最常用的关系型数据库管理系统之一,优化性能是每个开发者和管理员必须掌握的技能。
118 0
|
10月前
|
分布式计算 关系型数据库 BI
KYLIN 建模设计学习总结(概念、空间优化、查询性能优化)
KYLIN 建模设计学习总结(概念、空间优化、查询性能优化)
93 0
|
10月前
|
SQL 缓存 搜索推荐
第⼋章 查询性能优化
第⼋章 查询性能优化
|
Web App开发 JavaScript 前端开发
当我们进行性能优化,我们在优化什么(LightHouse优化实操)
好的互联网产品不仅仅在功能上要高人一筹,在性能层面也需要出类拔萃,否则金玉其外败絮其中,页面是美轮美奂了,结果首屏半天加载不出来,难免让用户乘兴而来,败兴而归。 幸运的是,前端的性能优化有诸多有迹可循的理论和方法,其中相对权威的,无疑是LightHouse。
当我们进行性能优化,我们在优化什么(LightHouse优化实操)
|
存储 算法 关系型数据库
MySQL查询性能优化前,必须先掌握MySQL索引理论
数据库索引在平时的工作是必备的,怎么建索引,怎么使用索引,可以提高数据的查询效率。而且在面试过程,数据库的索引也是必问的知识点,
150 0
MySQL查询性能优化前,必须先掌握MySQL索引理论