BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / database / #4401同步于 2010/4/26
Database机器人发帖

[合集] [求助]关于索引

coolfantasy
2010/4/26镜像同步0 回复
☆─────────────────────────────────────☆ bupthgl (swordheart) 于 (Wed Dec 9 16:28:08 2009) 提到: 用mysql在一个较大的表中a建了一个单列的普通索引(index)index0,然后用explain看以下select语句使用的索引时,怎么也不用index0 explain select * from a where index0>=20 and index0<23 +----+-------------+--------+------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | a | ALL | index0 | NULL | NULL | NULL | 346933 | Using where | +----+-------------+--------+------+---------------+------+---------+------+--------+-------------+ 查某个确定的索引项(where index0=21)是可以用index0的啊,是不是查询语句不能这么写?那查某个范围内的索引应该怎么写查询语句?谢谢~~ ☆─────────────────────────────────────☆ coolfantasy (Cool) 于 (Wed Dec 9 18:04:02 2009) 提到: 试试 explain select * from a where index0>=20000 and index0<39999 就是把 index0 的范围拉开点试试 ☆─────────────────────────────────────☆ bupthgl (swordheart) 于 (Wed Dec 9 18:57:18 2009) 提到: 可是这一列的范围很小,只是1-128,不用索引的后果就是查起来很慢了。。。有没有强制措施? 【 在 coolfantasy 的大作中提到: 】 : 试试 explain select * from a where index0>=20000 and index0<39999 : 就是把 index0 的范围拉开点试试 : ☆─────────────────────────────────────☆ coolfantasy (Cool) 于 (Wed Dec 9 19:00:58 2009) 提到: USING INDEX ☆─────────────────────────────────────☆ coolfantasy (Cool) 于 (Wed Dec 9 19:03:33 2009) 提到: 你这个数据量不少,但 index0 的值只有 1-128,区分度太小,满足这个区间的数据也很多了 MySQL 可能认为对于这个条件,使不使用索引效果差不多 ☆─────────────────────────────────────☆ bupthgl (swordheart) 于 (Wed Dec 9 23:13:39 2009) 提到: 其实是这样的,我想对一些列,比如说(e,a,b,c,d),建立一个最左前缀索引index,然后进行比如说 select * from r where a=1 and b=1 and c=1 and e>=20 and e<23之类的查询,这样的查询一般只返回一两个结果,但mysql总是不用这个索引,造成效率很低。 不知道我说清楚没有。。 【 在 coolfantasy 的大作中提到: 】 : 你这个数据量不少,但 index0 的值只有 1-128,区分度太小,满足这个区间的数据也很多了 : MySQL 可能认为对于这个条件,使不使用索引效果差不多 ☆─────────────────────────────────────☆ coolfantasy (Cool) 于 (Thu Dec 10 06:59:40 2009) 提到: 执行一下 SHOW CREATE TABLE 把结果和你要执行的 SQL 贴出来 【 在 bupthgl 的大作中提到: 】 : 其实是这样的,我想对一些列,比如说(e,a,b,c,d),建立一个最左前缀索引index,然后进行比如说 : select * from r where a=1 and b=1 and c=1 and e>=20 and e<23之类的查询,这样的查询一般只返回一两个结果,但mysql总是不用这个索引,造成效率很低。 : 不知道我说清楚没有。。 ☆─────────────────────────────────────☆ FlyBeast (ah_jun) 于 (Thu Dec 10 09:21:50 2009) 提到: "index0 的值只有 1-128,区分度太小,满足这个区间的数据也很多了 MySQL 可能认为对于这个条件,使不使用索引效果差不多 " 这个我觉得比较有道理。 select * from r where a=1 and b=1 and c=1 and e>=20 and e<23 如果你的查询能,每次有 a=? and b=? and c=? and e>=20 and e<23 这样的条件,建一个联合索引 create index aaa on a(a,b,c,d,e)应该会用的
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。