MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?

前端 未结 10 2179
情书的邮戳
情书的邮戳 2020-11-27 10:06

I\'ve got a couple of duplicates in a database that I want to inspect, so what I did to see which are duplicates, I did this:

SELECT relevant_field
FROM some         


        
10条回答
  •  再見小時候
    2020-11-27 10:32

    This is similar to my case, where I have a table named tabel_buku_besar. What I need are

    1. Looking for record that have account_code='101.100' in tabel_buku_besar which have companyarea='20000' and also have IDR as currency

    2. I need to get all record from tabel_buku_besar which have account_code same as step 1 but have transaction_number in step 1 result

    while using select ... from...where....transaction_number in (select transaction_number from ....), my query running extremely slow and sometimes causing request time out or make my application not responding...

    I try this combination and the result...not bad...

    `select DATE_FORMAT(L.TANGGAL_INPUT,'%d-%m-%y') AS TANGGAL,
          L.TRANSACTION_NUMBER AS VOUCHER,
          L.ACCOUNT_CODE,
          C.DESCRIPTION,
          L.DEBET,
          L.KREDIT 
     from (select * from tabel_buku_besar A
                    where A.COMPANYAREA='$COMPANYAREA'
                          AND A.CURRENCY='$Currency'
                          AND A.ACCOUNT_CODE!='$ACCOUNT'
                          AND (A.TANGGAL_INPUT BETWEEN STR_TO_DATE('$StartDate','%d/%m/%Y') AND STR_TO_DATE('$EndDate','%d/%m/%Y'))) L 
    INNER JOIN (select * from tabel_buku_besar A
                         where A.COMPANYAREA='$COMPANYAREA'
                               AND A.CURRENCY='$Currency'
                               AND A.ACCOUNT_CODE='$ACCOUNT'
                               AND (A.TANGGAL_INPUT BETWEEN STR_TO_DATE('$StartDate','%d/%m/%Y') AND STR_TO_DATE('$EndDate','%d/%m/%Y'))) R ON R.TRANSACTION_NUMBER=L.TRANSACTION_NUMBER AND R.COMPANYAREA=L.COMPANYAREA 
    LEFT OUTER JOIN master_account C ON C.ACCOUNT_CODE=L.ACCOUNT_CODE AND C.COMPANYAREA=L.COMPANYAREA 
    ORDER BY L.TANGGAL_INPUT,L.TRANSACTION_NUMBER`
    

提交回复
热议问题