You need to enable JavaScript to run this app. MySQL查询优化:关联查询索引配置与EXPLAIN结果利用咨询
优惠活动
大模型
产品
解决方案
定价
更多

MySQL查询优化:关联查询索引配置与EXPLAIN结果利用咨询

MySQL查询与索引优化方案

一、先修复查询逻辑的已知问题

你的查询存在两个核心导致索引失效的写法,优先调整:

  • 不要对字段套YEAR()/MONTH()函数计算,会直接导致日期字段上的索引失效,改成区间匹配,比如要匹配当月的日期,写法参考:
    SET @current_month_start = DATE_FORMAT(CURRENT_DATE, '%Y-%m-01');
    SET @next_month_start = DATE_ADD(@current_month_start, INTERVAL 1 MONTH);
    -- 原来的MONTH(CURRENT_DATE) IN (MONTH(c.date_created), MONTH(c.date_updated)) AND YEAR(...) 替换为
    (
      (c.date_created >= @current_month_start AND c.date_created < @next_month_start)
      OR
      (c.date_updated >= @current_month_start AND c.date_updated < @next_month_start)
    )
    
  • 跨两表的OR逻辑会导致优化器无法选择合适的索引,直接拆成两个查询用UNION ALL合并,避免全表扫描:
    原有逻辑是满足c表条件或者满足a表条件,拆解后参考写法:
    -- 第一部分:匹配c表条件的记录
    SELECT a.id, a.user_unique_id, a.loan_location, 
      a.ippis, a.tel_no,
      a.organisation, a.branch, a.loan_agree, 
      a.loan_type, a.appr, a.sold, 
      a.loan_status, a.top_up, a.current_loan, 
      a.date_created, a.date_updated, c.loan_id, c.user_unique_id AS tu_user_unique_id, 
      c.ippis AS tu_ippis, c.top_up_approved, 
      c.loan_type AS tu_loan_type, c.dse, c.status, c.current_loan AS tu_current_loan, 
      c.record_category, c.date_created AS tu_date_created, 
      c.date_updated AS tu_date_updated 
    FROM loan_applications_tbl a
    INNER JOIN topup_or_reapplication_tbl c ON a.ippis=c.ippis   
    WHERE c.status IN ('pending', 'corrected', 'Rejected', 'Processing', 'Captured', 'Reviewed', 'top up') 
      AND (
        (c.date_created >= @current_month_start AND c.date_created < @next_month_start)
        OR (c.date_updated >= @current_month_start AND c.date_updated < @next_month_start)
      )
      AND c.current_loan='1'
    
    UNION ALL
    
    -- 第二部分:匹配a表条件、且未被第一部分命中的记录(避免重复)
    SELECT a.id, a.user_unique_id, a.loan_location, 
      a.ippis, a.tel_no,
      a.organisation, a.branch, a.loan_agree, 
      a.loan_type, a.appr, a.sold, 
      a.loan_status, a.top_up, a.current_loan, 
      a.date_created, a.date_updated, NULL AS loan_id, NULL AS tu_user_unique_id, 
      NULL AS tu_ippis, NULL AS top_up_approved, 
      NULL AS tu_loan_type, NULL AS dse, NULL AS status, NULL AS tu_current_loan, 
      NULL AS record_category, NULL AS tu_date_created, 
      NULL AS tu_date_updated 
    FROM loan_applications_tbl a
    WHERE a.loan_status IN ('pending', 'corrected', 'Rejected', 'Processing', 'Captured', 'Reviewed', 'top up', 'Approved', 'Closed')
      AND (
        (a.date_created >= @current_month_start AND a.date_created < @next_month_start)
        OR (a.date_updated >= @current_month_start AND a.date_updated < @next_month_start)
      )
      AND (
        a.current_loan='1' 
        OR (a.current_loan='0' AND a.loan_status IN('Approved','Closed'))
      )
      AND NOT EXISTS (
        SELECT 1 FROM topup_or_reapplication_tbl c 
        WHERE c.ippis = a.ippis
          AND c.status IN ('pending', 'corrected', 'Rejected', 'Processing', 'Captured', 'Reviewed', 'top up') 
          AND (
            (c.date_created >= @current_month_start AND c.date_created < @next_month_start)
            OR (c.date_updated >= @current_month_start AND c.date_updated < @next_month_start)
          )
          AND c.current_loan='1'
      )
    

二、最优索引配置

按照联合索引「等值条件在前,范围条件在后,覆盖查询字段」的原则创建索引:

  • 针对topup_or_reapplication_tbl(c表)建联合索引:
    CREATE INDEX idx_c_ippis_filter ON topup_or_reapplication_tbl (
      ippis, current_loan, status, date_created, date_updated
    ) INCLUDE (loan_id, user_unique_id, top_up_approved, loan_type, dse, record_category);
    
    如果你使用的MySQL版本不支持INCLUDE语法,就把后面需要查询的字段直接加到索引末尾,做成覆盖索引,避免回表查询。
  • 针对loan_applications_tbl(a表)建联合索引:
    ippis是a表的唯一字段,如果你还没给ippis建唯一索引,优先建唯一索引,再补充过滤用的联合索引:
    -- 优先建唯一索引
    CREATE UNIQUE INDEX idx_a_ippis_unique ON loan_applications_tbl (ippis);
    -- 补充过滤用的联合索引
    CREATE INDEX idx_a_filter ON loan_applications_tbl (
      current_loan, loan_status, date_created, date_updated
    ) INCLUDE (id, user_unique_id, loan_location, tel_no, organisation, branch, loan_agree, loan_type, appr, sold, top_up);
    

三、possible_keys列的使用方法

possible_keys列列出了MySQL优化器判断本次查询可能用到的所有索引,按以下规则选择最优配置:

  1. 优先选择包含WHERE和JOIN条件中出现最多等值字段的索引,等值字段过滤性越高,索引效率越高
  2. 排除对应字段被函数包裹的索引,比如你原来的写法对date_created套了YEAR/MONTH,即使date_created在possible_keys里也不会被用到
  3. 如果possible_keys里有多个候选索引,优先选长度更短、过滤性更高的联合索引,避免使用单个字段的零散索引

内容的提问来源于stack exchange,提问作者Peter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 10:06:04