mysql - 这条sql可以怎么优化,求帮助
阿神
阿神 2017-04-17 15:19:32
0
4
648

select count(*) from trade where shippingtype <> "free" andstatusin ("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
阿神
阿神

闭关修行中......

répondre à tous (4)
阿神

尽管从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...

    阿神

    Strong

      黄舟

      那个 不等于 影响性能

        Peter_Zhu

        从explain上来看没什么问题

          Derniers téléchargements
          Plus>
          effets Web
          Code source du site Web
          Matériel du site Web
          Modèle frontal
          À propos de nous Clause de non-responsabilité Sitemap
          Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!