优化查询小问题

rczjp 2011-11-30 03:13:44
 Select * from table1 where tid=2 or tid=3
这个就算tid使用了索引也会失效,那怎么办?在查询的时候肯定会有很多查询是需要使用 or 的啊
...全文
69 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 rczjp 的回复:]

引用 10 楼 dawugui 的回复:
没有索引,也想使用索引,或者想快速,未必要求太高了吧?
恩,因为查询的条件太多,这个是需求没有办法呵呵
所以想来请教下是否有个方案。
[/Quote]

没有索引?
那就加个索引好了.
rczjp 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 dawugui 的回复:]
没有索引,也想使用索引,或者想快速,未必要求太高了吧?
[/Quote]恩,因为查询的条件太多,这个是需求没有办法呵呵
所以想来请教下是否有个方案。
dawugui 2011-11-30
  • 打赏
  • 举报
回复
没有索引,也想使用索引,或者想快速,未必要求太高了吧?
rczjp 2011-11-30
  • 打赏
  • 举报
回复
@OrchidCat
union这个和or效率也是一样 另外 or情况多的时候这样也不好吧?

[Quote=引用 7 楼 gogodiy 的回复:]
如果tid是聚集索引,你这样写法应该可以用到的。
[/Quote]恩,tid=2 or num=34 or name='a' 这样的情况 比如 num 是没有索引

不好意思 这个示例写的不好,仅仅想表达 or 没有使用索引的情况怎么解决的好呢?
--小F-- 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gogodiy 的回复:]
SQL code

--试试这样
Select * from table1 where tid>1 and tid<4
[/Quote]

恩 都忘记这样的写法了。
gogodiy 2011-11-30
  • 打赏
  • 举报
回复
如果tid是聚集索引,你这样写法应该可以用到的。
gogodiy 2011-11-30
  • 打赏
  • 举报
回复

--试试这样
Select * from table1 where tid>1 and tid<4
Mr_Nice 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用楼主 rczjp 的回复:]
 Select * from table1 where tid=2 or tid=3
这个就算tid使用了索引也会失效,那怎么办?在查询的时候肯定会有很多查询是需要使用 or 的啊
[/Quote]

select * from table1 where tid = 2
union all
select * from table1 where tid = 3


dawugui 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用楼主 rczjp 的回复:]
 Select * from table1 where tid=2 or tid=3
这个就算tid使用了索引也会失效,那怎么办?在查询的时候肯定会有很多查询是需要使用 or 的啊
[/Quote]你这样用,TID可以使用到索引.

或者:
Select * from table1 where tid=2
union all
Select * from table1 where tid=3

rczjp 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fredrickhu 的回复:]
Select * from table1 where tid in(2,3)
试试
[/Quote]这两种的效率是一样的
SylarZhou 2011-11-30
  • 打赏
  • 举报
回复
不懂索引的飘过,好吧,我承认我今天只是为了回帖而来的
--小F-- 2011-11-30
  • 打赏
  • 举报
回复
Select * from table1 where tid in(2,3)
试试

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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