微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

优化我的查询索引,EXPLAINMysql

如何解决优化我的查询索引,EXPLAINMysql

在 stackoverflow 上的开发人员的帮助下,我已经能够优化我的查询以使其性能比最初更好。执行时间下降到:1.2s

但是,在对查询执行 EXPLAIN 后,似乎没有使用 loan_applications_tbl ( date_updated,loan_status,current_loan,ippis ) 索引,因为表 a 的类型列显示为 ALL。请问这可能是什么原因?为什么该表的索引被忽略,我该如何解决

在下面查看我的查询

 EXPLAIN 
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 tu_user_unique_id,c.ippis tu_ippis,c.top_up_approved,c.loan_type tu_loan_type,c.dse,c.status,c.current_loan tu_current_loan,c.record_category,c.date_created tu_date_created,c.date_updated tu_date_updated 
    FROM 
        -- this creates inline MysqL variables I can use for the WHERE condition
        -- by doing comma after with no explicit join,it is a single row
        -- and thus no Cartesian result,just @variables available Now
        ( select 
                -- first truncating any TIME portion by casting to DATE()
                @myToday := date(curdate()),-- Now subtract day of month -1 to get first of THIS month
                @beginofMonth := date_sub( @myToday,interval dayOfMonth( @myToday ) -1 day ),-- and Now,add 1 month for beginning of next
                @beginNextMonth := date_add( @beginofMonth,interval 1 month ) ) sqlVars,loan_applications_tbl a
    
            LEFT JOIN topup_or_reapplication_tbl c
                ON  a.ippis = c.ippis   
                AND c.current_loan='1'
                AND c.status IN ('pending','corrected','Rejected','Processing','Captured','Reviewed','top up') 

                
                AND @beginofMonth <= c.date_updated
                AND c.date_updated < @beginNextMonth
    WHERE
            -- forces only activity for the single month in question
            -- since the "a" table kNows of any "updates" to the "C",-- its updated basis will keep overall restriction to any accounts
            -- updated within this month in question only
            @beginofMonth <= a.date_updated 
        AND a.date_updated < @beginNextMonth
        
        -- and Now we can easily apply the OR without requiring
        -- to run against the ENTIRE set of BOTH tables.
        AND (
                    c.ippis IS NOT NULL
                OR 
                    ( a.loan_status IN (  'pending','top up')
                    AND (   
                            a.current_loan = '1' 
                        OR  (   a.current_loan = '0' 
                            AND a.loan_status IN ('Approved','Closed')
                            )
                        )
                    )
            )

使用的索引:

loan_applications_tbl ( date_updated,ippis )
topup_or_reapplication_tbl ( ippis,status,date_updated )

查询的解释给出以下屏幕截图:

enter image description here

解决方法

试试https://dev.mysql.com/doc/refman/8.0/en/index-hints.html#:~:text=The%20USE%20INDEX%20(%20index_list%20)%20hint,some%20particular%20index%20or%20indexes

例如

SELECT * FROM table1 USE INDEX (col1_index,col2_index) WHERE col1=1 AND col2=2 AND col3=3;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?