热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[oracle性能优化]sql调优基础

不会根据执行计划进行sql调优的dba是不合格的,sqltuning是基本技能0.优化器的基本概念    为sql语句找到最好的,执行成本最低的执行计划    制定执行计划是以sq

不会根据执行计划进行sql调优的dba是不合格的,sql tuning是基本技能


0. 优化器的基本概念

     为sql语句找到最好的,执行成本最低的执行计划
     制定执行计划是以sql语句中涉及到的对象的统计信息为基础的。


1. 统计信息的介绍

    --表的统计信息(user_tables, user_tab_statistics)
   
    select num_rows, --表中的记录数
    blocks, --表中数据所占的数据块数
    empty_blocks, --表中的空块数
    avg_space, --数据块中平均的使用空间
    chain_cnt, --表中行连接和行迁移的数量
    avg_row_len, --每条记录的平均长度
    last_analyzed -- 最近一次搜集统计信息的时间
    from user_tables where table_name='new_sales';
   
    --搜集表的统计信息
    exec dbms_stats.gather_table_stats('sh','new_sales');
   
    --再来执行一次
    select num_rows, --表中的记录数
    blocks, --表中数据所占的数据块数
    empty_blocks, --表中的空块数
    avg_space, --数据块中平均的使用空间
    chain_cnt, --表中行连接和行迁移的数量
    avg_row_len, --每条记录的平均长度
    last_analyzed -- 最近一次搜集统计信息的时间
    from user_tables where table_name='new_sales';
   
   
     --列的统计信息 (user_tab_columns, user_tab_col_statistics,user_tab_histograms)
     select column_name,
     num_distinct, --唯一值的个数
     low_value, --列上的最小值
     high_value, --列上的最大值
     density, --选择率因子(密度) = 1/(ndv),如果不存在柱状图的话
     num_nulls, --空值的个数
     num_buckets, --直方图的bucket个数
     histogram --直方图的类型
     from user_tab_columns
     where table_name='new_sales'
   
     --搜集柱状图
     exec dbms_stats.gather_table_stats('sh', 'new_sales', method_opt => 'for all columns size 1 for columns size 254 cust_id');
    
    
     --再来看看统计信息
     select column_name,
     num_distinct, --唯一值的个数
     low_value, --列上的最小值
     high_value, --列上的最大值
     density, --选择率因子(密度) = 1/(ndv),如果不存在柱状图的话
     num_nulls, --空值的个数
     num_buckets, --直方图的bucket个数
     histogram --直方图的类型
     from user_tab_columns
     where table_name='new_sales'
    
     select
     column_name,
     endpoint_number,
     endpoint_value,
     from user_tab_histograms
     where table_name='new_sales' and column_name='cust_id'
    
   
    扩展统计信息 (user_stat_extensions)
     select e.extension col_group, t.num_distinct, t.histogram
     from user_stat_extensions e, user_tab_col_statistics t
     where e.extension_name=t.column_name
     and t.table_name='new_sales';
    
     --搜集扩展统计信息
     declare
        cg_name varchar2(30);
     begin
        cg_name := dbms_stats.create_extended_stats('sh','new_sales','(prod_id,cust_id)');
     end;

    select sys.dbms_stats.show_extended_stats_name('sh','new_sales', '(prod_id,cust_id)') col_group_name
     from dual;

    exec dbms_stats.gather_table_stats('sh','new_sales', method_opt => -
     'for columns (prod_id,cust_id) size skewonly');
    


2.统计信息不准确容易导致的问题


表统计信息不准确
    导致了表的访问方式出现了问题(全表扫描和使用索引)
    导致了表和表的链接方式出现问题(应该使用hash join,却是用了nest loop)
   

列统计信息不准确
    导致了访问表的方式不同(错误的索引)
    导致了表的连接方式不同(应该使用hash join , 但是使用了nest loop)

索引的统计信息不准确
    导致了访问表的方式不同(应该使用索引,但是使用了全表扫描)

+++++++++++++++++++++++++++++++++++++++++++

--当天线上表

create table sales_online

(
  prod_id      number  not null  ,      
  cust_id       number  not null,      

time_id      date  not null,        

channel_id     number  not null,      

promo_id       number  not null,      

quantity_sold  number(10,2) not null,

amount_sold    number(10,2) not null)

--历史归档表

create table sales_part (
  prod_id      number  not null  ,      
  cust_id       number  not null,      

time_id      date  not null,        

channel_id     number  not null,      

promo_id       number  not null,      

quantity_sold  number(10,2) not null,

amount_sold    number(10,2) not null)

partition by range (time_id)

(

partition part_20171218 values less than (to_date('19-12-2017','dd-mm-yyyy')),

partition part_20171219 values less than (to_date('20-12-2017','dd-mm-yyyy'))

);

insert into sales_part

select prod_id,cust_id, sysdate-2,channel_id,promo_id,quantity_sold,amount_sold

from new_sales;

insert into sales_part

select prod_id,cust_id, sysdate-1,channel_id,promo_id,quantity_sold,amount_sold

from new_sales;

commit;

create index sales_cust_idx on sales_online(cust_id);

create index sales_part_cust_idx on sales_part(cust_id);

--每天晚上把当天数据归档之后,再删除

declare
    v_sql varchar2(3000);

begin

   v_sql := 'alter table sales_part drop partition part_20171219';
    execute immediate v_sql;
   
    v_sql := 'alter table sales_part add partition part_'||to_char(sysdate+1,'yyyymmdd')||
    ' values less than (to_date('||''''||to_char(sysdate+1,'dd-mm-yyyy')||''''||','||''''||'dd-mm-yyyy'||
    ''''||'))';
    dbms_output.put_line(v_sql);
    execute immediate v_sql;
   
    v_sql := 'alter table sales_part exchange partition part_'||to_char(sysdate+1,'yyyymmdd')||
    ' with table sales_online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
   
    v_sql := 'truncate table sales_online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
   
    v_sql := 'alter index sales_part_cust_idx rebuild online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
   
     v_sql := 'alter index sales_cust_idx rebuild online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
  
    dbms_stats.gather_table_stats('sh', 'sales_part');
    dbms_stats.gather_table_stats('sh', 'sales_online');
   

end;

-- 检查统计信息

    select column_name,
     num_distinct, --唯一值的个数
     low_value, --列上的最小值
     high_value, --列上的最大值
     density, --选择率因子(密度) = 1/(ndv),如果不存在柱状图的话
     num_nulls, --空值的个数
     num_buckets, --直方图的bucket个数
     histogram --直方图的类型
     from user_tab_columns
     where table_name='sales_online'

   select num_rows, --表中的记录数
    blocks, --表中数据所占的数据块数
    empty_blocks, --表中的空块数
    avg_space, --数据块中平均的使用空间
    chain_cnt, --表中行连接和行迁移的数量
    avg_row_len, --每条记录的平均长度
    last_analyzed -- 最近一次搜集统计信息的时间
    from user_tables where table_name='sales_online';

   


====实例1


--进行查询

select c.cust_city, sum(amount_sold) from sales_part s, new_customers c

where s.cust_id = c.cust_id

and s.cust_id > 100

and time_id between to_date('2017-12-18 00:00:00' ,'yyyy-mm-dd hh24:mi:ss') and 

to_date('2017-12-18 01:00:00', 'yyyy-mm-dd hh24:mi:ss')

group by c.cust_city

----------------------------------------------------------------------------------------------------------

|   0 | select statement         |               |     1 |    48 |  1779   (1)| 00:00:22 | |        |

|   1 |  hash group by           |               |     1 |    48 |  1779   (1)| 00:00:22 | |        |

|*  2 |   hash join              |               |     1 |    48 |  1778   (1)| 00:00:22 | |        |

|   3 |    partition range single|               |     1 |    18 |  1373   (1)| 00:00:17 |     2 |     2 |

|*  4 |     table access full    | sales_part    |     1 |    18 |  1373   (1)| 00:00:17 |     2 |     2 |

|*  5 |    table access full     | new_customers | 54144 |  1586k|   405   (1)| 00:00:05 | |        |

-------------------------------------------------------------------------------------------

select c.cust_city, sum(amount_sold) from sales_online s, new_customers c

where s.cust_id = c.cust_id

and s.cust_id > 100

and time_id between to_date('2017-12-20 00:00:00' ,'yyyy-mm-dd hh24:mi:ss') and 

to_date('2017-12-20 01:00:00', 'yyyy-mm-dd hh24:mi:ss')

group by c.cust_city

------------------------------------------------------------------------------------------------

| id  | operation                     | name           | rows  | bytes | cost (%cpu)| time     |

------------------------------------------------------------------------------------------------

|   0 | select statement              |                |     1 |    65 |   406   (1)| 00:00:05 |

|   1 |  hash group by                |                |     1 |    65 |   406   (1)| 00:00:05 |

|*  2 |   hash join                   |                |     1 |    65 |   405   (1)| 00:00:05 |

|*  3 |    table access by index rowid| sales_online   |     1 |    35 |     0   (0)| 00:00:01 |

|*  4 |     index range scan          | sales_cust_idx |     1 |       |     0   (0)| 00:00:01 |

|*  5 |    table access full          | new_customers  | 54144 |  1586k|   405   (1)| 00:00:05 |

------------------------------------------------------------------------------------------------

--向表sales_online 中插入一些数据

insert into sales_online

select prod_id,cust_id, sysdate,channel_id,promo_id,quantity_sold,amount_sold

from new_sales;

commit;

--再次查询数据

select c.cust_city, sum(amount_sold) from sales_online s, new_customers c

where s.cust_id = c.cust_id

and s.cust_id > 100

and time_id between to_date('2017-12-20 00:00:00' ,'yyyy-mm-dd hh24:mi:ss') and 

to_date('2017-12-20 01:00:00', 'yyyy-mm-dd hh24:mi:ss')

group by c.cust_city

------------------------------------------------------------------------------------------------

|   0 | select statement              |                |     1 |    65 |   406   (1)| 00:00:05 |

|   1 |  hash group by                |                |     1 |    65 |   406   (1)| 00:00:05 |

|*  2 |   hash join                   |                |     1 |    65 |   405   (1)| 00:00:05 |

|*  3 |    table access by index rowid| sales_online   |     1 |    35 |     0   (0)| 00:00:01 |

|*  4 |     index range scan          | sales_cust_idx |     1 |       |     0   (0)| 00:00:01 |

|*  5 |    table access full          | new_customers  | 54144 |  1586k|   405   (1)| 00:00:05 |

------------------------------------------------------------------------------------------------

       

--手动搜集统计信息或许是一个办法

exec dbms_stats.gather_table_stats('sh', 'sales_online', cascade => true);

-------------------------------------------------------------------------------------

| id  | operation           | name          | rows  | bytes | cost (%cpu)| time     |

-------------------------------------------------------------------------------------

|   0 | select statement    |               |     1 |    48 |  1641   (1)| 00:00:20 |

|   1 |  hash group by      |               |     1 |    48 |  1641   (1)| 00:00:20 |

|*  2 |   hash join         |               |     1 |    48 |  1640   (1)| 00:00:20 |

|*  3 |    table access full| sales_online  |     1 |    18 |  1235   (1)| 00:00:15 |

|*  4 |    table access full| new_customers | 54144 |  1586k|   405   (1)| 00:00:05 |

-------------------------------------------------------------------------------------

执行计划变了过来。但是这不是一个好的办法,因为在生产时间搜集统计信息比较危险。

—-可以这样做

declare
    v_sql varchar2(3000);

begin

   v_sql := 'alter table sales_part drop partition part_20171219';
    execute immediate v_sql;
   
    --导出统计信息
    dbms_stats.export_table_stats(ownname =>'sh',tabname=>'sales_online',stattab=>'sales_online_st',statid => 'a2');
   
    v_sql := 'alter table sales_part add partition part_'||to_char(sysdate+1,'yyyymmdd')||
    ' values less than (to_date('||''''||to_char(sysdate+1,'dd-mm-yyyy')||''''||','||''''||'dd-mm-yyyy'||
    ''''||'))';
    dbms_output.put_line(v_sql);
    execute immediate v_sql;
   
    v_sql := 'alter table sales_part exchange partition part_'||to_char(sysdate+1,'yyyymmdd')||
    ' with table sales_online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
   
    v_sql := 'truncate table sales_online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
   
    v_sql := 'alter index sales_part_cust_idx rebuild online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
   
     v_sql := 'alter index sales_cust_idx rebuild online';
    execute immediate v_sql;
    dbms_output.put_line(v_sql);
  
    dbms_stats.gather_table_stats('sh', 'sales_part');
    --dbms_stats.gather_table_stats('sh', 'sales_online');

   --导入统计信息
    dbms_stats.import_table_stats(ownname => 'sh', tabname => 'sales_online', stattab => 'sales_online_st', statid => 'a2', no_invalidate => true);

end;



推荐阅读
  • MySQL索引详解与优化
    本文深入探讨了MySQL中的索引机制,包括索引的基本概念、优势与劣势、分类及其实现原理,并详细介绍了索引的使用场景和优化技巧。通过具体示例,帮助读者更好地理解和应用索引以提升数据库性能。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 深入解析JVM垃圾收集器
    本文基于《深入理解Java虚拟机:JVM高级特性与最佳实践》第二版,详细探讨了JVM中不同类型的垃圾收集器及其工作原理。通过介绍各种垃圾收集器的特性和应用场景,帮助读者更好地理解和优化JVM内存管理。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 本文详细探讨了Java中的24种设计模式及其应用,并介绍了七大面向对象设计原则。通过创建型、结构型和行为型模式的分类,帮助开发者更好地理解和应用这些模式,提升代码质量和可维护性。 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 尽管某些细分市场如WAN优化表现不佳,但全球运营商路由器和交换机市场持续增长。根据最新研究,该市场预计在2023年达到202亿美元的规模。 ... [详细]
  • 本文深入探讨了二叉搜索树(Binary Search Tree, BST)及其操作,包括查找、插入和删除节点。同时,文章还介绍了平衡二叉树(AVL树)的概念及调整方法,并详细讨论了如何判断两个序列是否构成相同的二叉搜索树。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
author-avatar
我的饭桶女神
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有