2 가지 쿼리가 각각 자체적으로 매우 빠르게 실행됩니다 (2 초 미만). 그러나 하위 쿼리로 조인하려고하면 천천히 실행됩니다. 마지막으로 실행했을 때 약 68 초가 걸렸습니다. 다음은 전체 검색어입니다.2 개의 쿼리가 빠르며, 하위 쿼리로 조인 할 때 속도가 느립니다.
SELECT t.count,
t.total
FROM (SELECT t.account_number,
COUNT(t.id) count,
SUM(t.amount) total,
ib.id import_bundle_id
FROM import_bundle ib
JOIN generic_import gi ON gi.import_bundle_id = ib.id
JOIN transaction_import ti ON ti.generic_import_id = gi.id
JOIN account_transaction t ON t.transaction_import_id = ti.id
JOIN transaction_code tc ON t.transaction_code_id = tc.id
WHERE tc.code IN (0, 20, 40)
GROUP BY t.account_number) t
JOIN (SELECT a.account_number,
np.code
FROM import_bundle ib
JOIN generic_import gi ON gi.import_bundle_id = ib.id
JOIN account_import ai ON ai.generic_import_id = gi.id
JOIN account a ON a.account_import_id = ai.id
JOIN account_northway_product anp ON anp.account_id = a.id
JOIN northway_product np ON anp.northway_product_id = np.id
WHERE np.code != 'O1') a ON t.account_number = a.account_number
이 쿼리는 느리게 실행되어야합니다. 놀랍지는 않습니다. 이 두 테이블이 서브 쿼리가 아닌 두 개의 개별 테이블이라면 인덱스를 account_number
열에 넣을 것입니다. 그러나 쿼리 결과에 인덱스를 넣을 수는 없으므로 그렇게 할 수 없습니다. 그게 문제의 일부라고 생각합니다. 쿼리가 느린 이유 그 외에
설명 결과와 테이블 구조 – Neo
은 두 번째 쿼리가'account_number'에 중복을 반환 할 수 있습니까? – Quassnoi