倒序索引 sql优化

willowleaf 2008-11-12 04:26:36
执行下面这个sql语句
select id, title, create_on, has_pic from tbl_info_article where is_recommend = 0 and category_id between 5001 and 5008 and has_check=0 or is_recommend= 0 and category_id between 5001 and 5008 and has_check=1 and can_pass=1 order by id desc
建立索引的语句是create index ind_article_main_1 on tbl_info_article(is_recommend,category_id,has_check,id desc);
但是查询结果不是严格意义上按id倒序排列的,请问这个问题怎么解决呢?
另一个问题是
还有一个查询语句是
select id, title, create_on, has_pic from tbl_info_article where category_id =1003 and has_check=0 or category_id=1003 and has_check=1 and can_pass=1
我如何修改这个语句使它在执行时使用上面那个索引呢?
...全文
240 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlueskyWide 2008-11-13
  • 打赏
  • 举报
回复
willowleaf 2008-11-13
  • 打赏
  • 举报
回复
5楼的伙计,给点儿实质性的意见好不好?
willowleaf 2008-11-13
  • 打赏
  • 举报
回复
四楼的伙计,我原来的语句功能相当于:
select id, title, create_on, has_pic
from tbl_info_article
where
(is_recommend = 0
and category_id between 5001 and 5008
and has_check=0 )
or
(is_recommend= 0
and category_id between 5001 and 5008
and has_check=1
and can_pass=1

order by id desc
这两个括号加不加都不影响效率的
Andy__Huang 2008-11-12
  • 打赏
  • 举报
回复
select id, title, create_on, has_pic from tbl_info_article where category_id =1003 and has_check=0 or category_id=1003 and has_check=1 and can_pass=1
应该改为:
select id, title, create_on, has_pic
from tbl_info_article
where category_id =1003 and (has_check=0 or has_check=1) and can_pass=1

你的语法结构看上去很乱,应该是select,from ,where 分别写在三行
这样结构看上去很清析
Andy__Huang 2008-11-12
  • 打赏
  • 举报
回复
select id, title, create_on, has_pic from tbl_info_article where is_recommend = 0 and category_id between 5001 and 5008 and has_check=0 or is_recommend= 0 and category_id between 5001 and 5008 and has_check=1 and can_pass=1 order by id desc
应该改为:
select id, title, create_on, has_pic
from tbl_info_article
where is_recommend = 0 and category_id between 5001 and 5008
and (has_check=0 or is_recommend= 0) and category_id between 5001 and 5008 and has_check=1 and can_pass=1
order by id desc
因为你了有or而不加括号是不起作用的

willowleaf 2008-11-12
  • 打赏
  • 举报
回复
结果是一样的
willowleaf 2008-11-12
  • 打赏
  • 举报
回复
感觉应该是一样的
BlueskyWide 2008-11-12
  • 打赏
  • 举报
回复

select id, title, create_on, has_pic
from tbl_info_article
where is_recommend = 0
and category_id between 5001 and 5008
and has_check = 0
or is_recommend = 0
and category_id between 5001 and 5008
and has_check = 1
and can_pass = 1
order by is_recommend, category_id, has_check, id desc; --使用和索引一样的排序,是吗?




[Quote=引用楼主 willowleaf 的帖子:]
执行下面这个sql语句
select id, title, create_on, has_pic from tbl_info_article where is_recommend = 0 and category_id between 5001 and 5008 and has_check=0 or is_recommend= 0 and category_id between 5001 and 5008 and has_check=1 and can_pass=1 order by id desc
建立索引的语句是create index ind_article_main_1 on tbl_info_article(is_recommend,category_id,has_check,id desc);
但是查询结果不是…
[/Quote]

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧