账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    mysql建立联合索引,但是时间范围多出一天后,不在走索引
    17
    0

    1、索引建立的字段date_point,corp_code,process_code
    2、date_point 在 '2018-09-01' and '2018-10-17',走索引,在'2018-09-01' and '2018-10-18' 却没有走索引

    针对第一条评论,补充统计的数据量(统计是否正确)

    explain select `corp_code`, `process_code`, count(distinct business_code) as total from `tra_aging_distributions`  
    where `process_code` = 'BAO_DAN_SHOU_LI' and `org_code` in ('JIAO_Y I_ZU_ZHI')  
    and `corp_code` in ('FYGS8888', 'TJBK8888', 'LFBK8888', 'DLBK8888', 'NJBK8888', 'WXBK8888', 'HZBK8888', 'HNFD0001', 'WHABK8888', 'GZBK8888', 'XABK8888')  
    and (`product_type_code` = '10001') 
    and `date_point` between '2018-09-01' and '2018-10-17'  
    and `cost_days` > '2' and `cost_days` <= '3'  
    group by `corp_code`, `process_code`;
    
    id select_type table type possible_keys key key_len ref rows Extra
    1 SIMPLE tra_aging_distributions range idx_date_corp_process idx_date_corp_process 587 NULL 1852870 Using index condition; Using where; Using filesort

    1 row in set (0.01 sec)

    explain select `corp_code`, `process_code`, count(distinct business_code) as total from `tra_aging_distributions`  
    where `process_code` = 'BAO_DAN_SHOU_LI' 
    and `org_code` in ('JIAO_YI_ZU_ZHI')  
    and `corp_code` in ('FYGS8888', 'TJBK8888', 'LFBK8888', 'DLBK8888', 'NJBK8888', 'WXBK8888', 'HZBK8888', 'HNFD0001', 'WHABK8888', 'GZBK8888', 'XABK8888')  
    and (`product_type_code` = '10001') 
    and `date_point` between '2018-09-01' and '2018-10-18'  
    and `cost_days` > '2' and `cost_days` <= '3'  
    group by `corp_code`, `process_code`;
    
    id select_type table type possible_keys key key_len ref rows Extra
    1 SIMPLE tra_aging_distributions ALL idx_date_corp_process NULL NULL NULL 10540764 Using where; Using filesort
    explain select `corp_code`, `process_code`, count(distinct business_code) as total from `tra_aging_distributions`  
    where `process_code` = 'BAO_DAN_SHOU_LI' and `org_code` in ('JIAO_Y I_ZU_ZHI')  
    and `corp_code` in ('FYGS8888', 'TJBK8888', 'LFBK8888', 'DLBK8888', 'NJBK8888', 'WXBK8888', 'HZBK8888', 'HNFD0001', 'WHABK8888', 'GZBK8888', 'XABK8888')  
    and (`product_type_code` = '10001') 
    and `date_point` between '2018-10-18' and '2018-10-18'  
    and `cost_days` > '2' and `cost_days` <= '3'  
    group by `corp_code`, `process_code`;
    
    id select_type table type possible_keys key key_len ref rows Extra
    1 SIMPLE tra_aging_distributions range idx_date_corp_process idx_date_corp_process 587 NULL 605 Using index condition; Using where

    1 row in set (0.00 sec)

    select (1852870+605)/10540764;
    
    (1852870+605)/10540764
    0.1758

    1 row in set (0.00 sec)

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • `兲倀地疚╮ 普通会员 1楼

      在MySQL中,当你创建一个联合索引后,它会自动覆盖旧的索引,但是这个过程可能需要一段时间,取决于索引的大小和你的查询速度。

      如果你的时间范围多出一天后,不在走索引,那么可能是因为你的查询速度比索引扫描的速度快。这意味着你的查询可能使用了索引,但索引可能还没有完全建立,或者索引被用于其他查询,而不是你现在的查询。

      为了避免这种情况,你可以尝试以下几种方法:

      1. 如果你对查询速度有特定的需求,你可以尝试优化你的查询,例如减少你的查询次数,或者使用其他查询方法。

      2. 如果你的时间范围多出一天后,但是仍然不走索引,你可能需要重新考虑你的查询策略。例如,你可能需要考虑使用其他查询方法,或者在索引未建立之前就开始你的查询。

      3. 如果你的时间范围多出一天后,但是仍然不走索引,你可能需要考虑使用其他的存储引擎,例如InnoDB,它支持在未建立索引的情况下运行。

      请注意,每个数据库系统都有其特定的特性,你可能需要根据你的具体情况进行调整。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部