提问1 :
select * from t1 where salary >= 400;  salary 是 INT 类型
ALTER TABLE t1 add INDEX sysalary(salary);我在 salary 建了个索引 , 可是运行时却没有使用到,是为什么? 跟数字有关吗? 
因为如果我方的是非数字的话就会用到了.提问2 :如果在一个 column 内有一个值重复了超过50% 是不是查询 where xx = "那个值" 的时候就没法使用索引了?
但where xx = "其他值" 就会用到索引 索引

解决方案 »

  1.   

    当MYSQL认为走索引成本并不节约的话,MYSQL会直接使用全表扫描。
      

  2.   

    哦,原来是这样,好智能哦 ! 再问一题 : 
    1.explain select a.id,a.datetime,a.from_username,a.link,b.name from a_msgbox a inner join fb_member b on a.from_username = b.username 
    where to_username='keatkeat87' order by id desc;
    这句2个table 都用到了索引. type 是 ref 2.explain select * from (select a.id from t1 a inner join t2 b on a.username = b.username where username='keatkeat87' order by id desc)c group by from_username;
    当我在外面加了个 select * from (第一句)
    table a 的 type 变成了 all (row 也多了) 这是为什么呢? key 和 key_len 都也没变.在补上一题 (这是我想实现的东西)
    表1
    id name order_record
    1  a    invoice-001
    2  a    invoice-002
    3  b    invoice-003
    4  b    invoice-004
    我想获取 每个name 的 最新一条order_record 该怎么写? 
    select * from (select order_record from table1 order by id desc) group by name .
    我用了这一句,就发现上一题的问题...是否有其它语句可以代替 ? 
      

  3.   

    提问1 :
    select * from t1 where salary >= 400;  salary 是 INT 类型
    ALTER TABLE t1 add INDEX sysalary(salary);我在 salary 建了个索引 , 可是运行时却没有使用到,是为什么? 跟数字有关吗? 
    因为如果我方的是非数字的话就会用到了.
    -----因为不走索引效率高提问2 :如果在一个 column 内有一个值重复了超过50% 是不是查询 where xx = "那个值" 的时候就没法使用索引了?
    但where xx = "其他值" 就会用到索引 
    ---没有具体的比例  mysql自己判断查询代价是否是最优的
      

  4.   

    顺序扫描是很快的,随机io是很耗时的。好像是超过40%,mysql就不走索引了,觉得顺序扫描的速度会更快!
      

  5.   


    1.不太会,不好发表意见。2.我想获取 每个name 的 最新一条order_record 该怎么写? 
    select name ,max(order_record ) from tablename
    group by name 
    这个就可以的。
      

  6.   

    2.我想获取 每个name 的 最新一条order_record 该怎么写? 
    select name ,max(order_record ) from tablename
    group by name 
    这个就可以的。感谢 ! 我是不是要在每个 select column 都加上 max() ? 
    select name,max(order_record) 之后order_record 对了,不过 name 的位置却不一样了
    id name order_record 
    1  a     invoice-002  
    3  b     invoice-004 
    //id 和 name 没随着 order_record 了
    似乎我每个都加 max ()就可以了 .
      

  7.   

    2.我想获取 每个name 的 最新一条order_record 该怎么写? 
    select name ,max(order_record ) from tablename
    group by name 
    这个就可以的。啊,不行啊 ! max是针对单一 column而已 , 其它的column没跟着走
    select id,name,max(order_record ) from tablename
    group by name 
    id name order_record 
    1  a     invoice-002  <--id 和 name 不属于 invoice --002 的
    3  b     invoice-004 
      

  8.   

    1、不走索引的效率比走索引的效率要高。
    2、mysql要看查询代价那个是最优的。