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
相關文章
相關標籤/搜索