m6米乐安卓版下载-米乐app官网下载
1

使用增量统计信息高效维护分区表的统计信息-m6米乐安卓版下载

赵勇 2022-08-04
191

介绍

本文涵盖有关当你使用分区交换加载(partition exchange load pel)时,如何高效地管理优化器统计信息的内容。该技巧用于有大量数据需要加载,而性能又是至关重要时。其多用于决策支持系统和有大量操作数据的存储。

确保你已经看过第一部分, 或者至少你熟悉增量统计信息的概念,以便你了解在分区表的语境下,何为synopsis.

分区交换加载

大多数人会比解熟悉分区交换加载,但我会简要汇总,介绍一下我将在本文中使用的术语。
下图代表了处理过程。首先,load表填充有新数据,它会和“实时”应用表(sales)中的一个分区做交换。sales表中有第一季度和第二季度的分区(q1和q2),而load与空的q2分区做交换。交换的效果是通过将load的“标识”与q2交换,将load中的所有数据合并到sales中.交换是一个逻辑操作:改变发生在数据库字典,没有数据被移动。load中的数据“在交换的瞬间”被发布到sales中。

典型的,交换的步骤看起来是这样的:

alter table sales exchange partition q2 with table load
including indexes without validation;

操作上,这个方法比直接插入数据到sales中更复杂一些,但是他有一些优势。比如,在该表上创建任何索引前,新的数据可以被插入到load中。如果数据量很大,在加载过程结束后创建索引是非常高效的,从而避免了加载过程中需要面临的较高的索引维护成本。如果以非常高的并行速率加载数据,则性能收益将尤其明显。

你需要为分区交换加载所做的具体操作步骤,取决于你所使用的分区类型,而不管表上是本地的还是全局的索引,以及正在使用的是什么约束。在这篇博文中,我将重点介绍如何管理统计信息,但你可以从中找到,如何在带有索引和约束时进行处理的详细说明。

当新的数据被加载到表中,优化器统计信息必须被更新,以便将这些新数据考虑在内。在上例中,sales表的全局统计信息必须要更新,以便反映load与q2交换时合并到表中的数据。为了使这个步骤尽可能高效,sales表必须使用增量统计信息维护。我期待你已经从本文的标题中猜到了,我的假设将从现在开始,我还会假设sales表的统计信息在分区交换加载前是最新的。

oracle database 11g

在load与q2交换后的时刻,是没有synopsis在q2中的;它还没有被创建。增量统计信息需要synopses来高效地更新sales的全局统计信息,所以,q2中的synopsis将在收集sales表的统计信息时被自动创建。例如:

exec dbms_stats.gather_table_stats(ownname=>null,tabname=>'sales')

q2上的统计信息将被收集,synopsis也将被创建,以便sales表上的全局统计信息可以被更新。一旦交换已经发生,q2就需要刷新统计信息、synopsis以及还可能会有的扩展统计信息和直方图(如果sales上有的话)。比如,如果sales上有一个列组"(col1,col2)",那么q2也需要这些统计信息。数据库会自动的处理它,因为他们会在sales表的统计信息被收集时为你创建,因此,你不需要在交换前就在load表上创建直方图和扩展统计信息。

然而有一种场景,你可能会希望在交换前就收集load表上的统计信息。比如,在sales表被收集统计信息之前,q2就可能被查询,那么你可能希望交换一完成,就确保q2上是有可用的统计信息。由于任何在load表上收集的统计信息都将与交换后的q2关联起来,所以,这也是容易做到的。然而,请牢记,这意味着最终新数据的统计信息会被收集两次:一次在交换前(在load表上),一次在交换后(sales表上的统计信息被重新收集时对q2分区的采集)。oracle database 12c给了你一个交互选项,我会在下面介绍它。
如果你想进一步了解扩展统计信息和列的使用,请查阅这篇。它介绍了你要如何识别,能从使用扩展统计中受益的种子列的使用情况。注意,为了帮助识别是可以从直方图中受益的列,有些列的使用情况信息是一直被捕获的。即便你不选择种子列,也会发生。

oracle database 12c

oracle database 12c包含一个允许你在交换前,在load表上创建synopsis的增强。这意味着,只要交换发生,是不需要在交换后,在q2上收集统计信息,其synopsis就已就绪了。其结果就是sales表上的全局统计信息,在oracle database 12c上的刷新比在oracle database 11g上更快。以下是在交换前,如何在load表上做准备:

begin
   dbms_stats.set_table_prefs (null,'load','incremental','true');
   dbms_stats.set_table_prefs (null,'load','incremental_level','table');
   dbms_stats.gather_table_stats (null,'load');
end;
/

只要交换完成就,q2就将会有一个全新的统计信息和synopsis。故事还没有结束,除非你在交换前,已经在load表上创建了适当的直方图和扩展统计信息,否则q2上的统计信息会在交换后再次收集(当收集sales上的统计信息时)。如果你想看一下表上都有什么,github上的list_s.sql脚本可以显示特定表上的扩展统计信息和直方图。如果你使用method_opt,来指定在sales表上具体创建什么样的直方图,那么你可以使用同样的method_opt来收集load表上的统计信息。比如:

设置表上的偏好参数

dbms_stats.set_table_prefs(
    ownname=>null,
    tabname=>'sales',
    method_opt=>'for all columns size 1 for columns sales_area size 254');

然后

   dbms_stats.set_table_prefs (null,'load','incremental','true');
   dbms_stats.set_table_prefs (null,'load','incremental_level','table');
   select dbms_stats.create_extended_stats(null,'load','(col1,col2)') from dual;
   dbms_stats.gather_table_stats(
         ownname=>null,
         tabname=>'load',
         method_opt=>'for all columns size 1 for columns sales_area size 254');

或者,如果你使用的缺省值’for all columns size auto’在sales表上收集统计信息,那么通常最好保持自动并且交换时不在load表上创建直方图。这会允许sales表上的统计信息收集识别出交换后的q2分区需要什么样的直方图。如果sales表上的列使用信息,显示出在q2上没有直方图的列,可能会从直方图中受益,则交换后的q2上的统计信息会被收集。而且,如上所述,扩展统计信息也将是自动维护的。

步骤摘要

如果你正在使用oracle database 12c,并且在带有适当的直方图和扩展统计信息的load表上创建了synopsis,那么你就可以最小化sales(交换后)表的统计信息收集时间。对于oracle database 11g,统计信息总是会在q2上完成交换后被收集。以下是操作步骤(请注意,我贴出的统计信息维护步骤,未包含维护索引和约束等):

1、创建load表并插入新数据 (或者使用 create table load as select…)
2、为sales表创建一个新的空分区(q2)
3、对load表填充数据
4、可选 (oracle database 12c) - 如果你想q2在交换后就立即拥有有效的统计信息,按下面的步骤操作:
  4.1 load表设置incremental 为 'true' , incremental_level 为 'table' 
  4.2 参照sales表,在load表上创建扩展统计信息
  4.3 参照sales表上的method_opt参数收集load表上的直方图
5、可选 (oracle database 11g) - 如果你想q2在交换后就立即拥有有效的统计信息,按下面的步骤操作:
  5.1 参照sales表在load表上创建扩展统计信息
  5.2 参照sales表上的method_opt参数收集load表上的直方图
6、交换load表和q2分区(这会交换oracle database 12c中的synopses,基本的列统计信息和直方图)
7、为sales表收集统计信息。如果你实施了上面的步骤4,oracle database 12c会完成得更快。

如果过去你曾经使用过分区交换并使用特定的方式收集统计信息,那么你可能需要比较表级直方图与分区和子分区上的直方图,是符合你的预期的。我在github上包含了一个 来帮助你做这个比较。

复合分区表

如果你正在使用一个复合分区表,使用上面描述的同样方式进行分区交换。如果你想用一个完整的例子来体验,我创建了一个名为的脚本.

原文标题:efficient statistics maintenance for partitioned tables using incremental statistics – part 2
原文链接:
原文作者:nigel bayliss

原文内容:

introduction

this post covers how you can manage optimizer statistics efficiently when you use partition exchange load (pel). this technique is used when large volumes of data must be loaded and maximum performance is paramount. it’s common to see it used in decision support systems and large operational data stores.

make sure you’ve taken a look at part 1, or you are at least familiar with the concept of incremental statistics so that you know what a synopsis is in the context of a partitioned table.

partition exchange load

most of you will be familiar with partition exchange load, but i’ll summarize it briefly to introduce you to the terminology i’ll be using here.

the graphic below represents the process. firstly, the load table is filled with new data and then exchanged with a partition in the “live” application table (sales). sales has partitions for quarter 1 and quarter 2 (q1 and q2) and load is exchanged with the empty q2 partition. the effect of the exchange is to incorporate all of the data in load into sales by swapping the “identity” of load with q2. the exchange is a logical operation: a change is made in the oracle data dictionary and no data is moved. the data in load is published to sales “at the flick of a switch”.

typically, the exchange step looks like this:

alter table sales exchange partition q2 with table load
including indexes without validation;

alter table sales exchange partition q2 with table load
including indexes without validation;

operationally, this approach is more complex than inserting data directly into sales but it offers some advantages. for example, new data can be inserted into load before any indexes have been created on this table. if the volume of data is large, creating indexes at the end of the load is very efficient and avoids the need to bear the higher cost of index maintenance during the load. the performance benefit is especially impessive if data is loaded at very high rates in parallel.

the exact steps you need to execute for a partition exchange load will vary depending on the type of partitioning you use, whether there are local or global indexes on the table and what constraints are being used. for the purposes of this blog post i’m going to stick to how you manage statistics, but you can find details on how to deal with indexes and constraints in the .

when new data is loaded into a table, optimizer statistics must be updated to take this new data into account. in the example above, the global-level statistics for sales must be refreshed to reflect the data incorporated into the table when load is exchanged with q2. to make this step as efficient as possible sales must use incremental statistics maintenance. i expect you’ll have guessed from the title of this post that i’m going to assume that from now on! i’m also going to assume that the statistics on sales are up-to-date prior to the partition exchange load.

oracle database 11g

the moment after load has been exchanged with q2 there will be no synopsis on q2; it won’t have been created yet. incremental statistics requires synopses to update the global-level statistics for sales efficiently so a synopsis for q2 will be created automatically when statistics on sales are gathered. for example:

exec dbms_stats.gather_table_stats(ownname=>null,tabname=>'sales')

exec dbms_stats.gather_table_stats(ownname=>null,tabname=>‘sales’)

statistics will be gathered on q2 and the synopsis will be created so that the global-level statistics for sales will be updated. once the exchange has taken place, q2 will need fresh statistics and a synopsis and it might also need extended statistics and histograms (if sales has them). for example, if sales has a column group, “(col1, col2)” then q2 will need these statistics too. the database takes care of this automatically, so there’s no requirement to create histograms and extended column statistics on load prior to the exchange because they are created for you when statistics are gathered on sales.

there is nevertheless a scenario where you might want to gather statistics on load prior to the exchange. for example, if it’s likely that q2 will be queried before statistics have been gathered on sales then you might want to be sure that statistics are available on q2 as soon as the exchange completes. this is easy to do because any statistics gathered on load will be associated with q2 after the exchange. however, bear in mind that this will ultimately mean that statistics for the new data will be gathered twice: once before the exchange (on the load table) and once again after the exchange (for the q2 partition when sales statistics are re-gathered). oracle database 12c gives you an alternative option, so i’ll cover that below.

if you want to know more about extended statistics and column usage then check out . it covers how you can seed column usage to identify where there’s a benefit in using extended statistics. note that some column usage information is always captured to help identify columns that can benefit from histograms. this happens even if you don’t choose to seed column usage.

oracle database 12c

oracle database 12c includes an enhancement that allows you to create a synopsis on load prior to the exchange. this means that a synopsis will be ready to be used as soon as the exchange has taken place without requiring statistics to be gathered on q2 post-exchange. the result of this is that the global-level statistics for sales can be refreshed faster in oracle database 12c than they can be in oracle database 11g. this is how to prepare the load table before the exchange:

begin
   dbms_stats.set_table_prefs (null,'load','incremental','true');
   dbms_stats.set_table_prefs (null,'load','incremental_level','table');
   dbms_stats.gather_table_stats (null,'load');
end;
/

q2 will have fresh statistics and a synopsis as soon as the exchange completes. this isn’t quite the end of the story though. statistics on q2 will be gathered again after the exchange (when statistics are gathered on sales) unless you have created appropriate histograms and extended statistics on load before the exchange. the list_s.sql script in github displays extended statistics and histograms for a particular table if you want to take a look at what you have. if you are using method_opt to specify exactly what histograms to create on sales then you can use the same method_opt for gathering statisitcs on load. for example:

table preference…

dbms_stats.set_table_prefs(
    ownname=>null,
    tabname=>'sales',
    method_opt=>'for all columns size 1 for columns sales_area size 254');

then…

   dbms_stats.set_table_prefs (null,'load','incremental','true');
   dbms_stats.set_table_prefs (null,'load','incremental_level','table');
   select dbms_stats.create_extended_stats(null,'load','(col1,col2)') from dual;
   dbms_stats.gather_table_stats(
         ownname=>null,
         tabname=>'load',
         method_opt=>'for all columns size 1 for columns sales_area size 254');

alternatively, if you are using the default ‘for all columns size auto’ to gather statistics on sales, then it’s usually best to preserve automation and exchange without creating histograms on load. this allows stats gathering on sales to figure out what histograms are needed for q2 post-exchange. statistics on q2 will be gathered post-exchange if sales has column usage information indicating that there are columns in q2 that don’t have a histogram but might benefit from having one. also, as mentioned above, extended statistics will be maintained automatically too.

summary of steps

if you are using oracle database 12c then you can minimize the statistics gathering time for sales (post-exchange) if you create a synopsis on load along with appropriate histograms and extended statistics. for oracle database 11g, statistics will always be gathered on q2 once the exchange has completed. here are the steps (bearing in mind i’m sticking to statistics maintenance and not including steps to manage indexes and constraints etc):
1、create load table and insert new data (or create table load as select…)
2、create a new (empty) partition for sales (q2)
3、populate load with data
4、optionally (oracle database 12c) - follow these steps if you want q2 to have valid statistics immediately after the exchange:
4.1 set incremental to ‘true’ and incremental_level to ‘table’ for load table
4.2 create extended statistics on load to match sales
4.3 gather statistics on load using method_opt parameter to match histograms with sales
5、optionally (oracle database 11g) - follow these steps if you want q2 to have valid statistics immediately after the exchange:
5.1 create extended statistics on load to match sales
5.2 gather statistics on load using method_opt parameter to match histograms with sales
6、exchange load with q2 (this will exchange synopses in oracle database 12c, basic column statistics and histograms)
7、gather statistics for sales. oracle database 12c will complete this step more quickly if you implemented “4”, above.

if, in the past, you have used partition exchange load and gathered statistics in an ad-hoc manner then you should probably check that the histograms you have match your expectations when comparing table-level histograms with histograms on partitions and sub-partitions. i’ve included to help you do that.

composite partitioned tables

if you are using a composite partitioned table, partition exchange load works in the same way as described above. if you would like to experiment with a complete example, i’ve created a script called example.sql .

「喜欢文章,快来给作者赞赏墨值吧」
【米乐app官网下载的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

网站地图