Mysql `索引(注意编号,重设计数)。。。换句话说,您需要仔细检查每个交付表所需的索引刚刚添加了索引,解释不再使用filesort(解释数据:)对大量行执行任何操作。遗憾的是,查询仍然需要同样长的时间。我已经为问题添加了最新的解释。。是的,在不知道表结构和索引的情

Mysql `索引(注意编号,重设计数)。。。换句话说,您需要仔细检查每个交付表所需的索引刚刚添加了索引,解释不再使用filesort(解释数据:)对大量行执行任何操作。遗憾的是,查询仍然需要同样长的时间。我已经为问题添加了最新的解释。。是的,在不知道表结构和索引的情,mysql,database-performance,query-performance,Mysql,Database Performance,Query Performance,`索引(注意编号,重设计数)。。。换句话说,您需要仔细检查每个交付表所需的索引刚刚添加了索引,解释不再使用filesort(解释数据:)对大量行执行任何操作。遗憾的是,查询仍然需要同样长的时间。我已经为问题添加了最新的解释。。是的,在不知道表结构和索引的情况下很难进一步改进..更新:通过按数量级删除组来加快查询速度MySQL的哪个版本?较新的版本在加入(选择…)方面做得更好;你需要这样的优化! SELECT Concat(Concat(cust.first_name, ' '), cust.la


`索引(注意编号,重设计数)。。。换句话说,您需要仔细检查每个交付表所需的索引刚刚添加了索引,解释不再使用filesort(解释数据:)对大量行执行任何操作。遗憾的是,查询仍然需要同样长的时间。我已经为问题添加了最新的解释。。是的,在不知道表结构和索引的情况下很难进一步改进..更新:通过按数量级删除组来加快查询速度MySQL的哪个版本?较新的版本在加入(选择…)
方面做得更好;你需要这样的优化!
SELECT Concat(Concat(cust.first_name, ' '), cust.last_name)    AS customerName, 
       TYPE.code                                               AS transType, 
       ty1.nsfamount, 
       np.sumrebateamount, 
       trans.note_id                                           AS note_id, 
       trans.createdate                                        AS createdatestr, 
       n.totalamount, 
       n.currentfloat, 
       ( ( n.costofborrowing * 100 ) / n.amounttolent )        AS fees, 
       n.amounttolent, 
       ( 0 - ( trans.cashamount + trans.chequeamount 
               + trans.debitamount 
               + trans.preauthorizedamount ) )                 AS paidamount, 
       sumpenaltyamount 
FROM   (SELECT * 
        FROM   loan_transaction trans1 
        WHERE  trans1.cashamount < 0 
                OR trans1.chequeamount < 0 
                OR trans1.debitamount < 0 
                OR trans1.preauthorizedamount < 0) trans 
       inner join customer cust 
               ON trans.customer_id = cust.customer_id 
       inner join (SELECT * 
                   FROM   lookuptransactiontypes ty 
                   WHERE  ty.code <> 'REB' 
                          AND ty.code <> 'PN') TYPE 
               ON trans.transactiontype = TYPE.transactiontypesid 
       inner join note n 
               ON trans.note_id = n.note_id 
       inner join (SELECT note_id, 
                          SUM(rebateamount) AS sumrebateamount 
                   FROM   note_payment np1 
                   GROUP  BY np1.note_id) np 
               ON trans.note_id = np.note_id 
       left join (SELECT note_id, 
                         transactiontype, 
                         ( SUM(chequeamount) + SUM(cashamount) 
                           + SUM(debitamount) + SUM(preauthorizedamount) )AS 
                         NSFamount 
                  FROM   (SELECT * 
                          FROM   loan_transaction trans4 
                          WHERE  trans4.cashamount > 0 
                                  OR trans4.chequeamount > 0 
                                  OR trans4.debitamount > 0 
                                  OR trans4.preauthorizedamount > 0)trans5 
                         inner join (SELECT transactiontypesid 
                                     FROM   lookuptransactiontypes ty2 
                                     WHERE  ty2.code = 'NSF')type2 
                                 ON 
                         trans5.transactiontype = type2.transactiontypesid 
                  GROUP  BY trans5.note_id) ty1 
              ON ty1.note_id = trans.refnum 
       left join (SELECT note_id                           AS noteid, 
                         ( SUM(tp.cashamount) + SUM(tp.chequeamount) 
                           + SUM(tp.debitamount) 
                           + SUM(tp.preauthorizedamount) ) AS sumpenaltyamount 
                  FROM   loan_transaction tp 
                         inner join (SELECT transactiontypesid 
                                     FROM   lookuptransactiontypes lp 
                                     WHERE  lp.code = 'PN') lp 
                                 ON lp.transactiontypesid = tp.transactiontype 
                  GROUP  BY tp.note_id) p 
              ON p.noteid = trans.refnum 
id  select_type  table       type    possible_keys    key                               key_len  ref                    rows    Extra                            
1   PRIMARY      <derived3>  ALL                                                                                        2241                                     
1   PRIMARY      <derived4>  ALL                                                                                        191441  Using join buffer                
1   PRIMARY      n           eq_ref  PK_NOTE          PK_NOTE                           8        np.note_id             1                                        
1   PRIMARY      <derived2>  ALL                                                                                        274992  Using where; Using join buffer   
1   PRIMARY      cust        eq_ref  PRIMARY_97       PRIMARY_97                        8        trans.CUSTOMER_ID      1                                        
1   PRIMARY      <derived5>  ALL                                                                                        2803                                     
1   PRIMARY      <derived8>  ALL                                                                                        14755                                    
8   DERIVED      <derived9>  ALL                                                                                        2       Using temporary; Using filesort  
8   DERIVED      tp          ref     TRANSACTIONTYPE  TRANSACTIONTYPE                   9        lp.transactionTypesID  110     Using where                      
9   DERIVED      lp          ALL                                                                                        2206    Using where                      
5   DERIVED      <derived7>  ALL                                                                                        98      Using temporary; Using filesort  
5   DERIVED      <derived6>  ALL                                                                                        314705  Using where; Using join buffer   
7   DERIVED      ty2         ALL                                                                                        2206    Using where                      
6   DERIVED      trans4      ALL                                                                                        664587  Using where                      
4   DERIVED      np1         index                    note_payment_idx_id_rebateamount  16                              193366  Using index                      
3   DERIVED      ty          ALL                                                                                        2206    Using where                      
2   DERIVED      trans1      ALL                                                                                        664587  Using where
SELECT
  CONCAT(cust.first_name, ' ', cust.last_name) AS customerName,
  TYPE.code                                            AS transType,
  ty1.nsfamount,
  SUM(np.rebateamount) as sumrebateamount,
  trans.note_id                                        AS note_id,
  trans.createdate                                     AS createdatestr,
  n.totalamount,
  n.currentfloat,
  ((n.costofborrowing * 100) / n.amounttolent)         AS fees,
  n.amounttolent,
  (0 - (trans.cashamount + trans.chequeamount
        + trans.debitamount
        + trans.preauthorizedamount))                  AS paidamount,
  sumpenaltyamount
FROM loan_transaction trans
INNER JOIN customer cust ON trans.customer_id = cust.customer_id
INNER JOIN lookuptransactiontypes TYPE ON trans.transactiontype = TYPE.transactiontypesid
INNER JOIN note n ON trans.note_id = n.note_id
INNER JOIN note_payment np ON trans.note_id = np.note_id


LEFT JOIN (SELECT
               note_id,
               transactiontype,
               (SUM(chequeamount) + SUM(cashamount)
                + SUM(debitamount) + SUM(preauthorizedamount)) AS
                 NSFamount
                   FROM loan_transaction trans4
                   INNER JOIN lookuptransactiontypes type2 ON trans4.transactiontype = type2.transactiontypesid
                   WHERE (trans4.cashamount > 0
                         OR trans4.chequeamount > 0
                         OR trans4.debitamount > 0
                         OR trans4.preauthorizedamount > 0) AND type2.code = 'NSF'
             GROUP BY trans5.note_id) ty1
    ON ty1.note_id = trans.refnum
  LEFT JOIN (SELECT
               note_id                         AS noteid,
               (SUM(tp.cashamount) + SUM(tp.chequeamount)
                + SUM(tp.debitamount)
                + SUM(tp.preauthorizedamount)) AS sumpenaltyamount
             FROM loan_transaction tp
               INNER JOIN (SELECT transactiontypesid
                           FROM lookuptransactiontypes lp
                           WHERE lp.code = 'PN') lp
                 ON lp.transactiontypesid = tp.transactiontype
             GROUP BY tp.note_id) p
    ON p.noteid = trans.refnum

WHERE
(trans.cashamount < 0
OR trans.chequeamount < 0
OR trans.debitamount < 0
OR trans.preauthorizedamount < 0)
AND TYPE.code <> 'REB'
AND TYPE.code <> 'PN'

GROUP BY trans.note_id;
-- this will force a full scan on customer table and ignores the filter
select whatever 
from transactions inner join 
(
    select * from customer 
) customer on transactions.customer_id = customer.customer_id
where customer.customer_id  = 1;
select whatever 
from transactions inner join customer on transactions.customer_id = customer.customer_id
where customer.customer_id  = 1;
FROM loan_transaction trans
    INNER JOIN customer cust ON trans.customer_id = cust.customer_id
FROM (SELECT * FROM customer LIMIT 50 OFFSET 0) cust 
    INNER JOIN loan_transaction trans ON trans.customer_id = cust.customer_id