分区表基础知识培训

4,793 views

Published on

Published in: Technology, Business
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
4,793
On SlideShare
0
From Embeds
0
Number of Embeds
2,606
Actions
Shares
0
Downloads
125
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide
  • Partitioning works by very simply splitting database objects into smaller chunks. Operations on the objects simply leave out the chunks they don’t require. There is absolutely no need to change the application in order to exploit this – and we have a huge variety of ways of partitioning the data to suit any type of data. Let’s go into the advantages in a little more detail.
  • Oracle DW users are universally leveraging partitioning and parallelism. Compression is heavily used, exploited in almost 90% of our survey participants. MVs and VPDs are widespread, touching over two thirds of survey sites.
  • Partitioning is incredibly well established and a very mature Oracle technology. We have more capability than any other database vendor – and have responded to different customer requirements over the years. Most recently, for example, we have introduced a partition advisor to help customers decide how to partition their tables.
  • Splitting database objects up also helps manageability. For example if we know that we have all the data for a certain partition, we can simply mark it as read only and we do not need to back it up ever again.
  • Splitting database objects up also helps manageability. For example if we know that we have all the data for a certain partition, we can simply mark it as read only and we do not need to back it up ever again.
  • Splitting database objects up also helps manageability. For example if we know that we have all the data for a certain partition, we can simply mark it as read only and we do not need to back it up ever again.
  • Partitioning gives us a benefit for availability. If a partition is removed from an object, the query can just run on the remainder of the table. This is great for maintenance and archival.
  • Splitting database objects up also helps manageability. For example if we know that we have all the data for a certain partition, we can simply mark it as read only and we do not need to back it up ever again.
  • Gathering statistics on one partition (e.g. after a bulk load) causes a full scan of all partitions to gather global table statistics Extremely time and resource intensive Use incremental statistics Gather statistics for touched partition(s) ONLY Table (global) statistics are built from partition statistics
  • 分区表基础知识培训

    1. 1. www.oracledatabase12g.com 刘相兵 (Maclean Liu) [email_address] 分区表基础知识培训
    2. 2. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com About Me <ul><li>Email:liu.maclean@gmail.com </li></ul><ul><li>Blog:www.oracledatabase12g.com </li></ul><ul><li>Oracle Certified Database Administrator Master 10g and 11g </li></ul><ul><li>Over 6 years experience with Oracle DBA technology </li></ul><ul><li>Over 7 years experience with Linux technology </li></ul><ul><li>Member Independent Oracle Users Group </li></ul><ul><li>Member All China Users Group </li></ul><ul><li>Presents for advanced Oracle topics: RAC, DataGuard, Performance Tuning and Oracle Internal. </li></ul>
    3. 3. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><li>了解分区表的基础概念 </li></ul><ul><li>了解分区索引的基础概念 </li></ul><ul><li>了解分区表统计信息概念 </li></ul><ul><li>其他注意事项 </li></ul>
    4. 4. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>什么是 partition 分区? </li></ul></ul><ul><li>将数据分成更小的块 (chunk) </li></ul><ul><ul><li>包括 表和索引 </li></ul></ul><ul><li>分区对于应用程序而言是透明的,无需修改 SQL 语句 </li></ul><ul><ul><li>Oracle SQL 优化器自动识别分区 </li></ul></ul><ul><ul><li>避免了大表的全表扫描 </li></ul></ul><ul><li>丰富的分区选项 </li></ul><ul><ul><li>包括 Range,Hash,List,Ref,composite 等 </li></ul></ul>Mar Jan Feb Sales SQL Application
    5. 5. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>大型数据仓库 Oracle 特性使用情况 </li></ul></ul>
    6. 6. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>Oracle Partitioning 分区特性 </li></ul></ul>核心功能 性能增强 管理 Oracle8 Range partitioning Global range indexes “ Static” partition pruning Basic maintenance operations: add, drop, exchange Oracle8 i Hash and composite range-hash partitioning Partition-wise joins “ Dynamic” pruning Merge operation Oracle9 i List partitioning Global index maintenance Oracle9 i R2 Composite range-list partitioning Fast partition split Oracle10 g Global hash indexes Local Index maintenance Oracle10 g R2 1M partitions per table “ Multi-dimensional” pruning Fast drop table
    7. 7. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>Oracle Partitioning 分区特性 </li></ul></ul>核心功能 性能增强 管理 Oracle8 Range partitioning Global range indexes “ Static” partition pruning Basic maintenance operations: add, drop, exchange Oracle8 i Hash and composite range-hash partitioning Partition-wise joins “ Dynamic” pruning Merge operation Oracle9 i List partitioning Global index maintenance Oracle9 i R2 Composite range-list partitioning Fast partition split Oracle10 g Global hash indexes Local Index maintenance Oracle10 g R2 1M partitions per table “ Multi-dimensional” pruning Fast drop table Oracle Database 11 g More composite choices REF Partitioning Virtual Column Partitioning Interval Partitioning Partition Advisor
    8. 8. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>复合分区 </li></ul></ul><ul><li>例如列表 - 范围分区 : </li></ul><ul><ul><li>以国家分区后,再以月份分区 </li></ul></ul>Range List Hash Range 11g 9i 8i List 11g 11g 11g
    9. 9. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>分区对性能的优化 </li></ul></ul>SQL 仅访问相关分区 使用已知值做静态数据修剪 动态修剪使用内部递归 SQL 以找到相关分区 最小化 IO 操作 提供极大的性能收益 S WHERE sales_date BETWEEN ‘01-MAR-2006’ AND ‘31-MAY-2006’; SELECT sum(sales_amount) FROM sales WHERE sales_date BETWEEN ‘01-MAR-2006’ AND ‘31-MAY-2006’; -2006’ AND ‘31-MAY-2006’; ---Sales Table--- 06-Apr 06-Mar 06-May 06-Feb 06-Jan 06-Jun
    10. 10. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>分区精简 </li></ul></ul>Sales Table May 22 nd 2008 May 23 rd 2008 May 24 th 2008 May 18 th 2008 May 19 th 2008 May 20 th 2008 May 21 st 2008 Select sum(sales_amount) From SALES Where sales_date between to_date(‘05/20/2008’,’MM/DD/YYYY’) And to_date(‘05/23/2008’,’MM/DD/YYYY’); 2008 年 5 月 20 日到 5 月 23 日的总销售额是多少?
    11. 11. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>静态精简 </li></ul></ul><ul><li>示例计划 </li></ul>
    12. 12. 动态分区精简 www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com 针对复杂查询存在高级精简的机制 在运行时递归 SQL 语句会自动找出相关分区 关注执行计划中的 PSTAR/PSTOP 字段中的 KEY SELECT sum(amount_sold) FROM sales s, times t WHERE t.time_id = s.time_id AND t.calendar_month_desc IN (‘MAR-04’,‘APR-04’,‘MAY-04’); Sales Table May 2004 June 2004 Jul 2004 Jan 2004 Feb 2004 Mar 2004 Apr 2004 Times Table
    13. 13. 动态分区精简 www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com 嵌套循环 <ul><li>示例计划 </li></ul>
    14. 14. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com Q4 ’ 05 订单表 ( 按季度分区 ) <ul><ul><li>分区对高可用的增强 </li></ul></ul>Q1 ’ 06 Q2 ’ 06 Q3 ’ 06 其他分区不受影响 s not affected
    15. 15. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>分区对管理的增强 </li></ul></ul><ul><li>更小的可管理的数据块 </li></ul><ul><ul><li>包括数据和索引元素 </li></ul></ul><ul><li>分区级别的操作 </li></ul><ul><ul><li>备份和恢复 </li></ul></ul><ul><ul><li>数据加载 </li></ul></ul><ul><li>并行操作 </li></ul><ul><ul><li>减少查询和操作的时间 </li></ul></ul>
    16. 16. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>范围分区 Range Partition </li></ul></ul><ul><li>CREATE TABLE DEPT </li></ul><ul><li>(DEPTNO NUMBER(2), </li></ul><ul><li>DEPT_NAME VARCHAR2(30)) </li></ul><ul><li>PARTITION BY RANGE(DEPTNO) </li></ul><ul><li> (PARTITION D1 VALUES LESS THAN (10) TABLESPACE DEPT1, </li></ul><ul><li>PARTITION D2 VALUES LESS THAN (20) TABLESPACE DEPT2, </li></ul><ul><li>PARTITION D3 VALUES LESS THAN (MAXVALUE) TABLESPACE DEPT3); </li></ul><ul><ul><li>INSERT INTO DEPT VALUES (1, ‘DEPT 1’); </li></ul></ul><ul><ul><li>INSERT INTO DEPT VALUES (7, ‘DEPT 7’); </li></ul></ul><ul><ul><li>INSERT INTO DEPT VALUES (10, ‘DEPT 10’); </li></ul></ul><ul><ul><li>INSERT INTO DEPT VALUES (15, ‘DEPT 15’); </li></ul></ul><ul><ul><li>INSERT INTO DEPT VALUES (22, ‘DEPT 22’); </li></ul></ul>
    17. 17. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>范围分区 Range Partition </li></ul></ul><ul><ul><li>SELECT * FROM DEPT; </li></ul></ul><ul><ul><li>DEPTNO DEPT </li></ul></ul><ul><ul><li>---------- -------------------- </li></ul></ul><ul><ul><li>1 DEPT 1 </li></ul></ul><ul><ul><li>7 DEPT 7 </li></ul></ul><ul><ul><li>10 DEPT 10 </li></ul></ul><ul><ul><li>15 DEPT 15 </li></ul></ul><ul><ul><li>22 DEPT 22 </li></ul></ul><ul><ul><li>SELECT * FROM DEPT PARTITION (D1); </li></ul></ul><ul><ul><li>DEPTNO DEPT </li></ul></ul><ul><ul><li>---------- -------------------- </li></ul></ul><ul><ul><li>1 DEPT 1 </li></ul></ul><ul><ul><li>7 DEPT 7 </li></ul></ul>
    18. 18. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>范围分区 Range Partition </li></ul></ul><ul><ul><li>SELECT * FROM DEPT PARTITION (D2); </li></ul></ul><ul><ul><li>DEPTNO DEPT </li></ul></ul><ul><ul><li>---------- -------------------- </li></ul></ul><ul><ul><li>10 DEPT 10 </li></ul></ul><ul><ul><li>15 DEPT 15 </li></ul></ul><ul><ul><li>SELECT * FROM DEPT PARTITION (D3); </li></ul></ul><ul><ul><li>DEPTNO DEPT </li></ul></ul><ul><ul><li>---------- -------------------- </li></ul></ul><ul><ul><li>22 DEPT 22 </li></ul></ul><ul><ul><li>ALTER TABLE DEPT DROP PARTITION D3; </li></ul></ul><ul><ul><li>Table altered. </li></ul></ul>
    19. 19. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>范围分区 Range Partition </li></ul></ul><ul><ul><li>SELECT * FROM DEPT; </li></ul></ul><ul><ul><li>DEPTNO DEPT </li></ul></ul><ul><ul><li>---------- -------------------- </li></ul></ul><ul><ul><li>1 DEPT 1 </li></ul></ul><ul><ul><li>7 DEPT 7 </li></ul></ul><ul><ul><li>10 DEPT 10 </li></ul></ul><ul><ul><li>15 DEPT 15 </li></ul></ul><ul><li>SELECT TABLE_NAME, PARTITIONED FROM USER_TABLES; </li></ul><ul><li>TABLE_NAME PAR </li></ul><ul><li>------------------------------ --- </li></ul><ul><li>DEPT YES </li></ul><ul><li>TEMP NO </li></ul>
    20. 20. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>范围分区 Range Partition </li></ul></ul>SELECT OWNER, TABLE_NAME, PARTITION_COUNT FROM SYS.DBA_PART_TABLES; OWNER TABLE_NAME PARTITION_COUNT ----------- ----------------- --------------- TEMP DEPT 2 SELECT SEGMENT_NAME, PARTITION_NAME, SEGMENT_TYPE, TABLESPACE_NAME FROM USER_SEGMENTS; SEGMENT_NAME PARTITION_NAME SEGMENT_TYPE TABLESPACE_NAME ------------ -------------- ----------------- --------------- TEMP TABLE USER_DATA DEPT D1 TABLE PARTITION DEPT1 DEPT D2 TABLE PARTITION DEPT2
    21. 21. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>本地前缀分区索引 </li></ul></ul>create index dept_index on dept (deptno) local (partition d1 tablespace dept2, partition d2 tablespace dept3, partition d3 tablespace dept1); Index Created.
    22. 22. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>本地前缀分区索引 </li></ul></ul>create index dept_index on dept (deptno) local (partition d1 tablespace dept2, partition d2 tablespace dept3, partition d3 tablespace dept1); Index Created.
    23. 23. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>选择合适的分区索引类型 </li></ul></ul>
    24. 24. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>统计信息的收集 </li></ul></ul><ul><li>CBO 下必须收集统计信息 </li></ul><ul><ul><li>对于分区表使用动态收集 (dynamic sampling) 是不合适的 </li></ul></ul><ul><li>对于空表执行各类查询,以便收集 column usage 列使用情况信息 </li></ul><ul><ul><li>这帮助 oracle 自动统计机制了解哪些列上需要创建直方图 (histogram) </li></ul></ul><ul><li>在完成大规模的数据统计后记得收集一下统计信息 </li></ul><ul><li>每天晚上运行的 Gather_stats_job ,并非每次都把所有的大表都重新收集一遍 statistics , 而是仅针对从上一次收集以来修改超过 10% 的表做重新收集。 没有专家的建议情况下,不要禁用该自动收集统计信息作业! </li></ul>
    25. 25. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>统计信息的收集 </li></ul></ul><ul><li>默认情况下 DBMS_STATS 为每张表都收集以下信息 , 可以利用 Granularity 参数控制 </li></ul><ul><ul><li>Global stats table level( 表级 ) </li></ul></ul><ul><ul><li>分区级别 partition level </li></ul></ul><ul><ul><li>子分区级别 subpartition level( 默认不收集 ) </li></ul></ul><ul><li>Optimizer 优化器在查询接触到 2 个或以上分区的情况下结合使用 global stats 表级和 paratition level 分区级别的统计信息 </li></ul><ul><li>Optimizer 优化器在查询做分区排除和只用一个分区即能满足查询访问的情况下使用分区级别的信息 </li></ul><ul><li>Optimizer 优化器在查询做分区排除和只有一个子分区即可满足查询的情况下使用子分区级别的统计信息 </li></ul>
    26. 26. Incremental Global Statistics www.oracledatabase12g.com Sales Table May 22 nd 2008 May 23 rd 2008 May 18 th 2008 May 19 th 2008 May 20 th 2008 May 21 st 2008 Sysaux Tablespace S1 S2 S3 S4 S5 S6 1. Partition level stats are gathered & synopsis created Global Statistic 2. Global stats generated by aggregati ng partition synopsis
    27. 27. Incremental Global Statistics Cont’d Sales Table May 24 th 2008 3. A new partition is added to the table & Data is Loaded www.oracledatabase12g.com May 22 nd 2008 May 23 rd 2008 May 18 th 2008 May 19 th 2008 May 20 th 2008 May 21 st 2008 Sysaux Tablespace May 24 th 2008 S7 4. Gather partition statistics for new partition S1 S2 S3 S4 S5 S6 5. Retrieve synopsis for each of the other partitions from Sysaux Global Statistic 6. Global stats generated by aggregati ng the original partition synopsis with the new one
    28. 28. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>收集精确的统计信息的必要步骤 </li></ul></ul><ul><ul><li>为表启用增量统计信息特性 </li></ul></ul><ul><ul><li>EXEC DBMS_STATS.SET_TABLE_PREFS('SH’,'SALES','INCREMENTAL','TRUE'); </li></ul></ul><ul><ul><li>在数据加载后使用 GATHER_TABLE_STATS 为表收集统计信息 </li></ul></ul><ul><ul><li>EXEC DBMS_STATS.GATHER_TABLE_STATS('SH','SALES'); </li></ul></ul><ul><ul><li>以上命令会为分区收集统计信息并利用基于 partition level 的信息更新 global statistics </li></ul></ul>
    29. 29. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>收集精确的统计信息的必要步骤 </li></ul></ul><ul><ul><li>为表启用增量统计信息特性 </li></ul></ul><ul><ul><li>EXEC DBMS_STATS.SET_TABLE_PREFS('SH’,'SALES','INCREMENTAL','TRUE'); </li></ul></ul><ul><ul><li>在数据加载后使用 GATHER_TABLE_STATS 为表收集统计信息 </li></ul></ul><ul><ul><li>EXEC DBMS_STATS.GATHER_TABLE_STATS('SH','SALES'); </li></ul></ul><ul><ul><li>以上命令会为分区收集统计信息并利用基于 partition level 的信息更新 global statistics </li></ul></ul>
    30. 30. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>注意事项 </li></ul></ul><ul><ul><li>新加载数据 </li></ul></ul><ul><ul><li>收集所有数据变化的分区统计信息 </li></ul></ul><ul><ul><li>Exec dbms_stats.gather_table_stats(‘MACLEAN’,’TAB’,GRANULARITY=>’SUBPARTITION’,PARTNAME=>’P_XXX_NewYork’); </li></ul></ul><ul><ul><li>Global statistics 会被更新 </li></ul></ul>
    31. 31. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>注意事项 </li></ul></ul><ul><ul><li>增加或减少分区或子分区 </li></ul></ul><ul><ul><li>Add 、 Split 、 Merge 、 Truncate 、 Exchange 分区或子分区 </li></ul></ul><ul><ul><li>收集有变化的子分区的统计信息 </li></ul></ul><ul><ul><li>Exec dbms_stats.gather_table_stats(‘MACLEAN’,’TAB’,GRANULARITY=>’SUBPARTITION’,PARTNAME=>’P_XXX_NewYork’); </li></ul></ul><ul><ul><li>Global statistics 会被更新 </li></ul></ul>
    32. 32. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>更多信息 </li></ul></ul>www.oracledatabase12g.com Partition or http://www.oracledatabase12g.com/archives/category/oracle/oracle-partitioning
    33. 33. www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com www.oracledatabase12g.com <ul><ul><li>Question & Answer </li></ul></ul>If you have more questions later, feel free to ask

    ×