product表:
========================================================================
id(主键)
title(varchar)
cid(产品分类)
userid(用户id)
updatetime(更新时间)
========================================================================现在希望能够按分类查看产品,同时产品按更新时间排序,那么两种索引方式:1、建立cid索引,并建立updatetime索引,用这个语句查询:
(1)select * from product where cid=123 order by updatetime desc2、建立复合索引cid+updatetime,这样查询:
(2)select * from product where cid=123 order by updatetime desc
(3)select * from product where cid=123 order by cid,updatetime desc哪种方式正确?或者都不正确?

解决方案 »

  1.   

    用explain看一下mySQL的执行计划,一比较就知道哪个正确了。 实践是检验的最好方法。
      

  2.   

    不建议使用第一种方案,时间如果经常更新的话,就会对索引结构影响比较大,影响insert,update,除非这么做很值.
      

  3.   

    第二种,时间在第二维影响会小很多,如果cid不怎么变化的话.不过我觉得就建个cid索引即可.当然explain下最好,实践出真理...
      

  4.   

    使用2个单列索引时,select * from product where cid=123 order by updatetime desc 这个只利用cid的优化了where,没有进行order by 的优化
    而一个多列索引时二者优化都有选择2