mysql left join查询没走索引

SELECT
        t0.ID as id,
        t0.`NAME` as name,
        t0.PHONE as phone,
        t0.`CITY_CODE` as cityCode,
        t0.SHOOTING_TIME as shootingTime,
        t0.REMARK as remark,
        t0.SOURCE_FROM as sourceFrom,-- 平台来源
        t0.REFER as refer,
        t0.UPDATE_TIME as updateTime,
         CONCAT(IFNULL(t0.SHOOTING_NAME,''),t1.SHOOTING_NAME) as shootingName,
        t0.SHOOTING_ADDRESS as shootingAddress,
        t0.CREATE_TIME as createTime,
        CASE t0.OP_RESULT
          WHEN 1 THEN '确认需求'
          WHEN 2 THEN '无效需求'
          WHEN 3 THEN '询价需求'
          WHEN 4 THEN '其他需求'
          ELSE ''
        END as opResultStr,
        t0.OP_EXPLAIN as opExplain,
         t1.ORDER_ID as orderCode,
        t2.name as sysName,-- 处理人
        t0.STATUS as status,
        t0.OP_TIME AS opTime,
        t3.COUPONS_ID AS couponsId,
        t5.DESCRIPTION AS couponsDescription,
        t4.`CODE` AS couponsCode,
        t5.EXPIRY_DATE AS couponsExpiryDate,
        t3.STATE AS couponsState,
         ao.ORDER_ID AS reqOrderCode,
         ao.SHOOTING_NAME AS reqShootingName,
        ci.CITY_NAME as cityName
        FROM
        V_TBL_REQUIREMENT AS t0
     -- 问题出处
        left join V_TBL_USER_ORDER t1 ON t1.REQUIREMENT_ID IS NOT NULL AND t0.ID = t1.REQUIREMENT_ID 
        left join t_user t2 ON t0.OP_CUSTOMMANAGER_ID = t2.id
        LEFT JOIN V_TBL_COUPONS_USER t3 ON t0.ID = t3.REQUIREMENT_ID
        LEFT JOIN V_TBL_COUPONS t4 ON t3.COUPONS_ID = t4.ID
        LEFT JOIN V_TBL_COUPONS_TEMPLATE t5 ON t4.COUPON_TEMPLATE_ID = t5.ID
        LEFT JOIN V_TBL_PHOTO_ALBUM a ON a.wechatMd5 = t0.WECHAT_MD5
        LEFT JOIN V_TBL_USER_ORDER ao ON ao.ID = a.orderId
        LEFT JOIN V_TBL_CITY ci on ci.CITY_CODE =t0.CITY_CODE
查询语句如上,
FROM V_TBL_REQUIREMENT AS t0 left join V_TBL_USER_ORDER t1 ON t1.REQUIREMENT_ID IS NOT NULL AND t0.ID = t1.REQUIREMENT_ID
其中 V_TBL_USER_ORDER 明明存在字段REQUIREMENT_ID存在索引,
但是explain解释执行后却是ALL,
在另一个DDL相同的环境中执行却走了索引 一整乱找原因,
最后发现可能是该环境是此表此字段的索引基数太小,
MYSQL自己估计使用全表扫描要比使用索引快,所以不使用索引了
最后使用强制索引解决问题
left join V_TBL_USER_ORDER t1 FORCE INDEX(INDEX_V_TBL_USER_ORDER_REQUIREMENT_ID) ON t1.REQUIREMENT_ID IS NOT NULL AND t0.ID = t1.REQUIREMENT_ID
posted @ 2018-12-14 15:55  月河  阅读(6428)  评论(0编辑  收藏  举报