>  Q&A  >  본문

mysql - 这条sql可以怎么优化,求帮助

select count(*) from trade where shippingtype <> "free" and status in ("TRADE_FINISHED","WAIT_SELLER_SEND_GOODS") and tosellerreachgoods = 0 and consigntime <1470110400000 and ( endtime >=1469980800000 or endtime is null ) and created >=1469980800000 and created <=1470067200000 and user = "xxxxxxxxxxxxxxxx" ;

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE trade range trade__user,trade_user_created,trade_user_buyernick,trade__status_created,trade_user_status trade_user_created 108 NULL 588 Using index condition; Using where
阿神阿神2743일 전703

모든 응답(4)나는 대답할 것이다

  • 阿神

    阿神2017-04-17 15:21:32

    尽管从explain结果看这个查询没什么特别问题,但以下几点可以考虑一下。
    1、shippingtypestatus看起来是个枚举值,可以用tinyint代替(数值查询比字符串查询速度要快);
    2、endtime设为NOT NULL, 把原来的null值用一个特殊值(-1或0)代替,NULL值会影响索引的效率;
    3、可以的话,用user_id代替user

    个人YY的优化:

    SELECT count(*) FROM trade 
    WHERE status IN ("TRADE_FINISHED", "WAIT_SELLER_SEND_GOODS") 
    AND shippingtype <> "free"
    AND tosellerreachgoods = 0 
    AND user = "xxxxxxxxxxxxxxxx"
    AND consigntime < 1470110400000 
    AND created BETWEEN 1469980800000 AND 1470067200000
    AND ( endtime >= 1469980800000 OR endtime IS NULL );

    前面4个条件没什么好说的,主要是后面三个条件,分别是consigntimecreatedendtime,根据筛选范围从小到大来排列(具体要看你的表里这三个字段的范围段)。

    部分建议是基于你能修改表结构的基础上提出的,如果没有权限的话那就忽略吧。

    参考资料:http://tech.meituan.com/mysql...

    회신하다
    0
  • 阿神

    阿神2017-04-17 15:21:32

    Strong

    회신하다
    0
  • 黄舟

    黄舟2017-04-17 15:21:32

    那个 不等于 影响性能

    회신하다
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:21:32

    从explain上来看没什么问题

    회신하다
    0
  • 취소회신하다