暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 条款之间的调谐

askTom 2018-08-22
231

问题描述

我正在尝试调整包含Oracle 11g中的 “之间” 子句的查询。

我有表员工 (id号,join_dt日期,end_dt日期) 有1000万记录。
它在join_dt,end_dt上有索引

首次运行,dbms_stats.gather_table_stats (所有者 =>'Scoot',tabname => 'employe',estimate_percentage =>dbms_stats.autosample_size);

从员工中选择 *
其中join_dt和end_dt之间的trunc(sysdate)

使用全表扫描进行查询以获取数据


第二次运行,
dbms_stats.gather_table_stats (所有者 =>'Scoot',tabname =>'employee',estimate_percentage =>dbms_stats.autosample_size); -第二次运行


从员工中选择 *
其中join_dt和end_dt之间的trunc(sysdate)

使用索引范围扫描进行查询。

第一次运行和第二次运行之间没有时间间隔。如果it查询在第二次运行中使用索引,理想情况下,它应该在第一次运行时进行索引扫描。

你能帮忙吗?


专家解答

你可能看到了我们自动直方图的结果。例如,这是一个包含一些偏斜数据的表

SQL> create table t ( x int );

Table created.

SQL> insert into t select 10 from dual connect by level <= 10;

10 rows created.

SQL> insert into t select 20 from dual connect by level <= 20;

20 rows created.

SQL> insert into t select 100 from dual connect by level <= 100;

100 rows created.

SQL> insert into t select 1000 from dual connect by level <= 1000;

1000 rows created.

SQL>
SQL> exec dbms_stats.gather_table_stats('','T');

PL/SQL procedure successfully completed.

SQL>
SQL> select count(*)
  2  from user_tab_histograms
  3  where table_name = 'T';

  COUNT(*)
----------
         2

1 row selected.
复制


默认情况下,我们没有收集直方图 (count = 2意味着我们只抓取了X列的低值和高值)。现在让我们在该表上运行一些查询

SQL>
SQL> select count(*) from t where x = 10;

  COUNT(*)
----------
        10

1 row selected.

SQL> select count(*) from t where x > 500;

  COUNT(*)
----------
      1000

1 row selected.
复制


我们跟踪您在某些查询中使用了X列,因此我们注意到,下次收集统计信息时,在该列上设置直方图可能会有所帮助

SQL> exec dbms_stats.gather_table_stats('','T');

PL/SQL procedure successfully completed.

SQL>
SQL> select count(*)
  2  from user_tab_histograms
  3  where table_name = 'T';

  COUNT(*)
----------
         4

1 row selected.
复制


如果您从一开始就想要直方图,只需在初始收集统计信息调用中添加一个 “对于所有列大小自动”。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论