免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1272 | 回复: 3
打印 上一主题 下一主题

求助!为什么总不用相应的索引? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-06-23 17:14 |只看该作者 |倒序浏览
表xxx的两个索引
idx_3col 字段 email_status,birthdate,email
idx_5col 字段 email_status,city,gender,birthdate,interests_value
两个sql
这个总在用idx_3col
select count(*)  from xxx where
email_status='N'
and (city is null or city is not null )
and ( gender is null or gender is not null )
and birthdate between add_months(sysdate,-35*12)
and add_months(sysdate,-25*12)
and ( floor(mod(interest_value/power(2,,2))=1
or floor(mod(interest_value/power(2,12),2))=1 );

这个用的idx_5col
select count(*)  from XXX where
email_status='N'
and city in ('51_hefei','51_beijing','51_chongqing','51_fuzhou')
and ( gender is null or gender is not null )
and birthdate between add_months(sysdate,-35*12)
and add_months(sysdate,-25*12)
and ( floor(mod(interest_value/power(2,,2))=1
or floor(mod(interest_value/power(2,12),2))=1 )

我希望两个都用idx_5col,而且也应该是这样的。为什么会这样呢?

论坛徽章:
0
2 [报告]
发表于 2004-06-23 23:40 |只看该作者

求助!为什么总不用相应的索引?

and (city is null or city is not null )
这句不是等于没写?
而且 city is null 是不会用index的,一定是FTS(full table scan)
这一点,好象在书上看到过,时间长了,不太确定。

论坛徽章:
0
3 [报告]
发表于 2004-06-24 03:03 |只看该作者

求助!为什么总不用相应的索引?

Could you try this SQL as below:

select count(*) from xxx
where email_status='N'
and city is null
and gender is null
and birthdate between add_months(sysdate,-35*12) and add_months(sysdate,-25*12)
and ( floor(mod(interest_value/power(2,,2))=1 or floor(mod(interest_value/power(2,12),2))=1 )
union
select count(*) from xxx
where email_status='N'
and city is not null
and gender is not null
and birthdate between add_months(sysdate,-35*12) and add_months(sysdate,-25*12)
and ( floor(mod(interest_value/power(2,,2))=1 or floor(mod(interest_value/power(2,12),2))=1 );

论坛徽章:
0
4 [报告]
发表于 2004-06-24 10:16 |只看该作者

求助!为什么总不用相应的索引?

原帖由 "szhang" 发表:
Could you try this SQL as below:

select count(*) from xxx
where email_status='N'
and city is null
and gender is null
and birthdate between add_months(sysdate,-35*12) and add_months(sysdate,-..........


结果是两个索引都用了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP