一个面试时的题

gutlgutl 2007-12-06 12:37:47
一个有亿条记录的表,列名是 t1,t2,t3................t9
在表上有 t2,t3,t9的复合索引。
现在要求用t3,t9为条件查询全表。
怎么写效率最高?
...全文
214 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
rouqu 2007-12-08
  • 打赏
  • 举报
回复
我做的测试 1楼写的仍然是全表扫描 ojuju10的强制指定index名称才会进行index scan

w2jc 2007-12-07
  • 打赏
  • 举报
回复
更正一下我在7楼写的:
应该1楼(飞天小虫的)的写法才能使用到索引

gutlgutl 2007-12-07
  • 打赏
  • 举报
回复
我也遇到18楼的情况,如果是组合索引,如果没有用到前导列,在执行计划中,应该是没有先扫描索引的
可是我在2005中发现用没用前导组T2,结果是一样的,疑惑呀!
gutlgutl 2007-12-06
  • 打赏
  • 举报
回复
2楼的一会试下,应该是正确的
gutlgutl 2007-12-06
  • 打赏
  • 举报
回复
如果where 后面只有 t3 t9 ,这个索引是不起作用的 用到复合索引,没有用到前导列t2
lishijie910123 2007-12-06
  • 打赏
  • 举报
回复
对就是这样
select * from 表 where t3= and t9=
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
select * from 表 where t3= and t9=
DDMMYYYY 2007-12-06
  • 打赏
  • 举报
回复
CREATE INDEX index_tabename
ON table(t3,t9)
kk19840210 2007-12-06
  • 打赏
  • 举报
回复
 select * from 表 where t2=t2 and t3=   and t9=  
米多 2007-12-06
  • 打赏
  • 举报
回复
我测试了下,
select * from 表 where t2=t2 and t3= and t9=

select * from 表 where t3= and t9=
都用到了上面说的索引,分析的成本也相差无几,可有的书说 select * from 表 where t3= and t9= 不会用到上面说的索引,真是疑惑?!

wzy_love_sly 2007-12-06
  • 打赏
  • 举报
回复
select * from 表 where t3= and t9= 就行
如果加t2
就按t2= t3= t9=
winjay84 2007-12-06
  • 打赏
  • 举报
回复


-- 用到索引的,是index scan

select * from table where t3= and t9=

-- 要求是没有要求用t2
-- 如果用t2
-- index seek

select * from table where t2= and t3= and t9=

kk19840210 2007-12-06
  • 打赏
  • 举报
回复
mengmou
select * from 表 WITH(INDEX = XXX)where t3= and t9=



select * from 表 where t2=t2 and t3= and t9=

的执行计划是一样的

mengmou 的好一点
ojuju10 2007-12-06
  • 打赏
  • 举报
回复
强制指定索引
ojuju10 2007-12-06
  • 打赏
  • 举报
回复
select * from 表 WITH(INDEX = indexname )where t2=t2 and t3= and t9=
gutlgutl 2007-12-06
  • 打赏
  • 举报
回复
Mengmou的好像不对,因为T2没有当前导列出现
mengmou 2007-12-06
  • 打赏
  • 举报
回复
上面错了,也许是这样
select * from 表 WITH(INDEX = XXX)where t3= and t9=
mengmou 2007-12-06
  • 打赏
  • 举报
回复
select * from 表 where t3= and t9=
gutlgutl 2007-12-06
  • 打赏
  • 举报
回复
哈,看错了,把自己也算成一楼了,应该是飞天的方法对 1楼的
不许再建新的索引,必须用现有的
kk19840210 2007-12-06
  • 打赏
  • 举报
回复
2 楼是又重新建了个索引
要使用这索引 要 T2,T3,T9 一起使用
加载更多回复(1)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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