를 사용하여이 쿼리 :느린 MySQL 쿼리, EXPLAIN shows Using temporary; filesort
EXPLAIN SELECT ppi_loan.customerID,
loan_number,
CONCAT(forename, ' ', surname) AS agent,
name,
broker,
(SELECT timestamp
FROM ppi_sar_status
WHERE history = 0
AND (status = 10 || status = 13)
AND ppi_sar_status.loanID = ppi_loan.loanID) AS ppi_unsure_date,
fosSent,
letterSent,
(SELECT timestamp
FROM ppi_ques_status
WHERE status = 1
AND ppi_ques_status.loanID = ppi_loan.loanID
ORDER BY timestamp DESC LIMIT 1) AS sent_date,
ppi_ques_status.timestamp
FROM ppi_loan
LEFT JOIN ppi_assignments ON ppi_assignments.customerID = ppi_loan.customerID
LEFT JOIN italk.users ON italk.users.id = agentID
LEFT JOIN ppi_ques_status ON ppi_ques_status.loanID = ppi_loan.loanID
JOIN ppi_lenders ON ppi_lenders.id = ppi_loan.lender
JOIN ppi_status ON ppi_status.customerID = ppi_loan.customerID
JOIN ppi_statuses ON ppi_statuses.status = ppi_status.status
AND ppi_ques_status.status = 1
AND ppi_ques_status.history = 0
AND (cc_type = '' || (cc_type != '' AND cc_accepted = 'no'))
AND ppi_loan.deleted = 'no'
AND ppi_loan.customerID != 10
GROUP BY ppi_loan.customerID, loan_number
여기에, 매우 느립니다 쿼리 EXPLAIN의 모든 결과는 왜 그렇게 많은 행을 스캔하고 이유를 "일시적으로 사용; filesort 사용"
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY ppi_ques_status ref loanID,status,history status 3 const 91086 Using where; Using temporary; Using filesort
1 PRIMARY ppi_loan eq_ref PRIMARY,customerID PRIMARY 8 ppimm.ppi_ques_status.loanID 1 Using where
1 PRIMARY ppi_lenders eq_ref PRIMARY PRIMARY 4 ppimm.ppi_loan.lender 1 Using where
1 PRIMARY ppi_assignments eq_ref customerID customerID 8 ppimm.ppi_loan.customerID 1
1 PRIMARY users eq_ref PRIMARY PRIMARY 8 ppimm.ppi_assignments.agentID 1
1 PRIMARY ppi_status ref status,customerID customerID 8 ppimm.ppi_loan.customerID 6
1 PRIMARY ppi_statuses eq_ref PRIMARY PRIMARY 4 ppimm.ppi_status.status 1 Using where; Using index
3 DEPENDENT SUBQUERY ppi_ques_status ref loanID,status loanID 8 func 1 Using where; Using filesort
2 DEPENDENT SUBQUERY ppi_sar_status ref loanID,status,history loanID 8 func 2 Using where
? 내가 생성 한 결과가 모두 필요하므로 하위 쿼리를 제거 할 수 없습니다.
'ppi_ques_status' 테이블에서만 단일 필드 인덱스가있는 반면, 실제로 쿼리를 돕기 위해서는 다중 필드 인덱스가 필요하다는 것은 명백합니다. – Shadow
많은 행이 데이터베이스에 지정한 조건을 충족하므로 많은 행을 스캔합니다.'where where'는 인덱스를 적용하여 조회해야하는 행의 수를 줄이는 것을 의미하며'filesorts '를 사용하는 것은 거기에있는 레코드를 비트순으로 정렬하여 지정한 순서대로 반환하기 때문에 (filesort가 잘못 명명되었습니다) . 메모리 (빠른) 대신 디스크 (느린)에서 읽으므로 속도가 느립니다. – Mjh
@mjh 설명에 대한 해석은 실제 의미와 다릅니다. – Shadow