mysql 我sql限制和查询所有记录花费相同的时间

00jrzges  于 2022-11-21  发布在  Mysql
关注(0)|答案(1)|浏览(142)

下面是我的查询。limit 0,5000和运行所有记录的无limit子句(287,795)花费同样的时间9.5秒。我不得不重复这个查询57次,限制范围同样增加(0,5000),(5000,10000)。所以时间非常关键。
你能提出一些方法来提高速度吗?在SQL中,性能提高或任何策略上的改变。
我已经删除了内部连接下面的,用于测试前置,然后在不到1秒的时间内添加了**。但是连接是必需的。因为我必须检查

"APDMD.DAYS_TO_DEMAND_ARREARS" is within CC.FROM_DPD & CC.TO_DPD 
below is table CC

SELECT   COALESCE ( APDMD.DMS_SOL_ID, '--' ) AS SOL_ID,  
COALESCE ( APDMD.ACID, '--' ) AS ACCOUNT_NO,  
COALESCE ( APDMD.CUST_ID, '--' ) AS CUSTOMER_ID,  
COALESCE ( APDMD.ACCT_NAME, '--' ) AS CUSTOMER_NAME,  
COALESCE ( APDMD.SCHM_CODE, '--' ) AS SCHEME_CODE,  
COALESCE ( APDMD.SANCT_LIM, 0 ) AS SANCTION_LIMIT,  
COALESCE ( APDMD.CLR_BAL_AMT, 0 ) AS OS_BALANCE,  
COALESCE ( APDMD.CAP_OVER_DUE, 0 ) AS CAPITAL_ARREARS,  
COALESCE ( APDMD.INT_OVER_DUE, 0 ) AS INTEREST_ARREARS,  
COALESCE ( APDMD.ACCT_CRNCY_CODE, '--' ) AS CURRANCY_CODE,  
COALESCE ( APDMD.ACCT_MGR_USER_ID, '--' ) AS ACCOUNT_MANAGER,  
COALESCE ( CC.STAGE, '--' ) AS STAGECLASSIFICATION,  
COALESCE ( CC.SUB_CLASSIFICATION, '--' ) AS classification,  
COALESCE ( APDMD.ARRMONTHS, 0 ) AS MONTH_ARREARS,  
COALESCE ( APDMD.ARRDAYS, 0) AS DAYS_ARREARS,  APDMD.NPA_DATE AS NPADATE,  
COALESCE ( APDMD.LOCATION_CODE, '--' ) AS LOCATION_CODE,  
COALESCE ( APDMD.GL_SUB_HEAD_CODE, '--' ) AS GL_SUB_HEAD_CODE,  
COALESCE ( APDMD.IIS_LKR, 0 ) AS IIS_LKR,  
COALESCE ( APDMD.SP_PROVISION, 0 ) AS SP_PROVISION,  
COALESCE ( APDMD.SP_PROVISION_LKR, 0 ) AS SP_PROVISION_LKR,  
COALESCE ( APDMD.BSC_TEAM_LEADER, 0 ) AS BSC_TEAM_LEADER,  
APDMD.ACCT_OPN_DATE AS ACCT_OPN_DATE,  

( SELECT SUM( AA.CLR_BAL_AMT )    FROM app_dms_daily AA WHERE AA.CUST_ID = CUSTOMER_ID )  AS PORTPOLIO,
  
COALESCE (  ( SELECT  TKTH.resolutiondescription  FROM  tickethistory TKTH  WHERE  TKTH.tickethistoryid = (SELECT MAX( TH.TICKETHISTORYID ) FROM tickethistory TH WHERE TH.TICKETID = T.TICKETID )
   ),  '--'   ) AS REMARKS,
  
COALESCE ( DWH_MOBILE_NO, '--' ) AS MOBILENO,   
COALESCE(APDMD.MORATORIUM_GIVEN, '--')      AS MORATORIUM_GRANTED,  
COALESCE(APDMD.MORATORIUM_PERIOD, '--')      AS MORATORIUM_PERIOD 

FROM  app_dms_daily APDMD
 
#adding below increase 9s ---> 9*50 --> 7.5 min   
INNER JOIN classification_configuration CC ON  CASE     
WHEN CC.FROM_DPD IS NULL         
THEN APDMD.DAYS_TO_DEMAND_ARREARS <= CC.TO_DPD     
WHEN CC.FROM_DPD AND CC.TO_DPD IS NOT NULL         
THEN APDMD.DAYS_TO_DEMAND_ARREARS >= CC.FROM_DPD AND APDMD.DAYS_TO_DEMAND_ARREARS <= CC.TO_DPD     
WHEN CC.TO_DPD IS NULL         
THEN APDMD.DAYS_TO_DEMAND_ARREARS >= CC.FROM_DPD     ELSE '' END
  
LEFT OUTER JOIN ticket T ON T.ACCID = APDMD.ACID  
ORDER BY 
DMS_SOL_ID, 
OS_BALANCE ASC  
LIMIT 0,5000;

策略更改或SQL查询性能改进

cetgtptt

cetgtptt1#

由于ticket的唯一用途(我可以c)是在带有MAX的子查询中,您可以将其移到子查询中吗?
这些索引可能会有所帮助:

APDMD:  INDEX(DAYS_TO_DEMAND_ARREARS)
CC:  INDEX(FROM_DPD, TO_DPD,  STAGE, SUB_CLASSIFICATION, TO_DPD)
ticket_T:  INDEX(ACCID,  TICKETID)
app_dms_daily:  INDEX(CUST_ID,  CLR_BAL_AMT)
tickethistory:  INDEX(TICKETID,  TICKETHISTORYID)
tickethistory:  INDEX(tickethistoryid,  resolutiondescription)

OFFSET对于分页来说效率非常低。通常是recommend that you remember where you left off,但是对于所有的JOINs可能不实用。

相关问题