12
返回列表 发新帖
楼主: Germin

[讨论] 索引列顺序对索引数据量及性能的影响?

[复制链接]
论坛徽章:
113
ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:42:50现任管理团队成员
日期:2011-05-07 01:45:08ITPUB官方微博粉丝徽章
日期:2011-06-28 19:45:36蛋疼蛋
日期:2011-07-24 22:25:332012新春纪念徽章
日期:2012-02-13 15:12:252012新春纪念徽章
日期:2012-02-13 15:12:252012新春纪念徽章
日期:2012-02-13 15:12:252012新春纪念徽章
日期:2012-02-13 15:12:252012新春纪念徽章
日期:2012-02-13 15:12:25
11#
发表于 2009-10-29 09:45 | 只看该作者
原帖由 Germin 于 2009-10-28 22:20 发表
模拟了一些数据,

空间占用相同,clustering factor也一致

针对查询select * from T where a=? and b=? 默认选择使用索引i_ba
能否使用hints强制使用i_ab? 不会写

感觉还是没能了解其内部有啥区别以及使用中的影响。


其实如果是select * from T where a=? and b=?两个index能得到几乎相同的效率。
但是我们需要考虑应用中是否有其他的SQL,比如select * from T where b=?。
如果考虑到这一个因素,就该吧b写在index的前面。

btw:
hint写法:
select /*+index(test test_ba)*/ * from test where a = 10 and b = 1000;
select /*+index(test test_ab)*/ * from test where a = 10 and b = 1000;

使用道具 举报

回复
论坛徽章:
47
蒙奇·D·路飞
日期:2017-03-27 08:04:23马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11一汽
日期:2013-09-01 20:46:27复活蛋
日期:2013-03-13 07:55:232013年新春福章
日期:2013-02-25 14:51:24ITPUB 11周年纪念徽章
日期:2012-10-09 18:03:322012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20
12#
发表于 2009-10-30 01:46 | 只看该作者
原帖由 sqysl 于 2009-10-28 19:35 发表
哦,select * from large where a = 10 and b = 10;  
large表,应该为test吧


Sorry for my mistake. I tested earlier with large and later renamed it to test. But pasted one line of wrong code. Just corrected it.

Yong Huang

使用道具 举报

回复
论坛徽章:
4
生肖徽章2007版:鸡
日期:2008-01-02 17:35:53生肖徽章2007版:鼠
日期:2008-01-02 17:35:53生肖徽章2007版:猴
日期:2008-11-11 08:37:35生肖徽章2007版:牛
日期:2008-11-29 14:45:37
13#
发表于 2009-10-30 16:09 | 只看该作者
原帖由 viadeazhu 于 2009-10-29 09:45 发表


其实如果是select * from T where a=? and b=?两个index能得到几乎相同的效率。
但是我们需要考虑应用中是否有其他的SQL,比如select * from T where b=?。
如果考虑到这一个因素,就该吧b写在index的前面。

btw:
hint写法:
select /*+index(test test_ba)*/ * from test where a = 10 and b = 1000;
select /*+index(test test_ab)*/ * from test where a = 10 and b = 1000;


测试过很多次了,确是这样。

使用道具 举报

回复
招聘 : Java研发
论坛徽章:
1
2010新春纪念徽章
日期:2010-03-01 11:19:06
14#
发表于 2009-11-4 10:47 | 只看该作者
原帖由 Byte_X_ 于 2009-10-30 16:09 发表


测试过很多次了,确是这样。


这个今天刚刚测试过。确实是这样的,占用空间其实一样,效率上也差不多。如果启用压缩的话。
a,b列的值分布不同,
列a的不同值个数较小,约20个
列b的不同值个数较大,约1000万
用a来作引导列当然可以更加节省空间。
最主要看你在应用过程中,是否有使用b作引导列。如果有诸如where b=?,则使用b,a会更好。此外,还应该考虑索引块竞争等因素。
TOM的编程艺术中有索引一节中对这个问题也有讲述,以下是测试的脚本。可以试下。
create table t
as
select * from all_objects;
create index t_idx_1 on t(owner,object_type,object_name);
create index t_idx_2 on t(object_name,object_type,owner);
select count(distinct owner), count(distinct object_type),
count(distinct object_name ), count(*)
from t;
analyze index t_idx_1 validate structure;
select btree_space, pct_used, opt_cmpr_count, opt_cmpr_pctsave
from index_stats;
analyze index t_idx_2 validate structure;
select btree_space, pct_used, opt_cmpr_count, opt_cmpr_pctsave
from index_stats;
alter session set sql_trace=true;
declare
        cnt int;
begin
  for x in ( select /*+FULL(t)*/ owner, object_type, object_name from t )
  loop
       select /*+ INDEX( t t_idx_1 ) */ count(*) into cnt
         from t
        where object_name = x.object_name
          and object_type = x.object_type
          and owner = x.owner;

        select /*+ INDEX( t t_idx_2 ) */ count(*) into cnt
         from t
        where object_name = x.object_name
          and object_type = x.object_type
          and owner = x.owner;
  end loop;
end;
/

使用道具 举报

回复
招聘 : Java研发
论坛徽章:
1
2010新春纪念徽章
日期:2010-03-01 11:19:06
15#
发表于 2009-11-4 10:48 | 只看该作者
Byte_X_ ,哈哈!~~

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表