为什么mysql在可用时不使用索引

时间:2016-09-27 10:51:34

标签: mysql indexing database-performance

我的查询非常慢,我不知道为什么,每个连接字段都有索引。

这是我的疑问:

    SELECT 
    CASE
        WHEN
            arrears_action.executed IS NULL
                OR arrears_action.executed != '0000-00-00 00:00:00'
        THEN
            'No action'
        ELSE DATE_FORMAT(arrears_action.planned, '%d %M %Y')
    END,
    CASE
        WHEN
            arrears_action.executed IS NULL
                OR arrears_action.executed != '0000-00-00 00:00:00'
        THEN
            NULL
        ELSE IF(DATEDIFF(NOW(), arrears_action.planned) >= 0,
            DATEDIFF(NOW(), arrears_action.planned),
            NULL)
    END,
    customer.id AS customer_id,
    customer.customer_number AS cif_number,
    arrears_application.id AS arrears_application_id,
    person.id AS person_id,
    person.fullname,
    account.account_number,
    CONCAT(branch.branch_id,
            product.code,
            SUBSTRING(contract.loan_number, - 9, 9)) AS loan_account,
    payment.principal_amount_outstanding,
    arrears.amount_in_arrears,
    arrears.days_in_arrears,
    balance.account_balance_available,
    product.code AS product_code,
    division.name AS division_name,
    coll_customer_status.name AS customer_status,
    loan_status.name AS loan_status
FROM
    cregora_cregora.base_cregora_loancontract_inarrears_data AS arrears 
        INNER JOIN
    cregora_cregora.base_cregora_loancontract_data AS contract ON contract.id = arrears.loancontract_id 
        INNER JOIN
    cregora_cregora.base_cregora_loancontract_payment_data AS payment ON payment.loancontract_id = contract.id
        INNER JOIN
    cregora_cregora.base_cregora_nmb_product_class_data AS product ON product.id = contract.product_class
        INNER JOIN
    cregora_cregora.base_cregora_account_data AS account ON account.id = contract.account_id
        INNER JOIN
    cregora_cregora.base_cregora_customer_data AS customer ON customer.id = account.customer_id
        INNER JOIN
    cregora_cregora.base_cregora_person_data AS person ON person.id = customer.type_of_customer_id
        INNER JOIN
    cregora_base.system_organization_division AS division ON division.id = contract.loan_branch
        INNER JOIN
    cregora_cregora.base_cregora_nmb_branch_data AS branch ON branch.id = division.entity_instance_id
        INNER JOIN
    cregora_cregora.base_cregora_account_balanceinfo_daily_data AS balance ON balance.account_id = account.id
        LEFT JOIN
    cregora_cregora.base_cregora_arrears_applications_data AS arrears_application ON arrears_application.loan_contract_number = contract.loan_number
        LEFT JOIN
    cregora_cregora.base_cregora_arrears_action_data AS arrears_action ON arrears_action.arrears_application_id = arrears_application.id
        LEFT JOIN
    cregora_cregora.base_cregora_collection_applications_data AS coll_application ON coll_application.arrears_application_id = arrears_application.id
        LEFT JOIN
    cregora_cregora.base_cregora_collection_customer_statuses_data AS coll_customer_status ON coll_customer_status.id = coll_application.collection_customer_status_id
        LEFT JOIN
    cregora_cregora.base_cregora_arrears_loan_contract_statuses_data AS loan_status ON loan_status.id = arrears_application.loan_contract_status_id
        LEFT JOIN
    cregora_cregora.base_cregora_arrears_application_statuses_data AS arrears_status ON arrears_status.id = arrears_application.arrears_application_status_id
WHERE
    contract.loan_branch IN (46)
        AND (coll_customer_status.id IN ('1' , '2',
        '3',
        '5',
        '6',
        '7',
        '8',
        '9',
        '10',
        '11',
        '12',
        '13',
        '15',
        '16',
        '17',
        '18',
        '19',
        '20',
        '21',
        '22',
        '23')
        OR coll_customer_status.id IS NULL)
        AND (loan_status.id IS NULL
        OR loan_status.id IN ('1' , '2', '3', '4', '5'))
        AND arrears.amount_in_arrears > '1'
GROUP BY arrears_application.id
ORDER BY arrears.days_in_arrears DESC , person.fullname

这是EXPLAIN的输出: explain 为什么mysql没有使用合同表的任何索引(解释的第三行,类型 - > ALL,可能的键 - > PRIMARY,account_id,loan_branch,product_class key-> empty?

Thnx提前

1 个答案:

答案 0 :(得分:0)

问题是arrears_application_data表保持连接但也按分组。这导致了显着的性能下降