在select子查询中选择需要太长时间

时间:2017-02-21 09:42:52

标签: mysql

我有3张桌子:

  1. 60.000记录发票

  2. 100.000记录基于当前日期的汇率

  3. 25记录货币符号

  4. table_invoice

    +-----------+-------------+----------+--------------+
    | invoice_id | currency_id | amount   | invoice_date |
    +------------+-------------+----------+--------------+
    |          1 |      2      |     10   | 4/28/2016    |
    |          2 |      3      |     30   | 4/29/2016    |
    |          3 |      4      |     50   | 4/30/2016    |
    |          4 |      2      |     40   | 6/18/2016    |
    |          5 |      6      |     25   | 6/20/2016    |
    |          6 |      7      |     87   | 6/25/2016    |
    |          7 |      4      |     100  | 6/29/2016    |
    |          8 |      9      |     45   | 7/14/2016    |
    |          9 |      2      |     71   | 9/27/2016    |
    |      60000 |      3      |     430  | 1/18/2017    |
    +------------+-------------+----------+--------------+
    

    table_exchange_rate

    +-----------------+-------------+---------------+--------------------+
    | exchange_rate_id | currency_id | exchange_rate | exchange_rate_date |
    +------------------+-------------+---------------+--------------------+
    |              1   |          2  | 13.352        | 4/25/2016          |
    |              2   |          3  | 10.195        | 4/25/2016          |
    |              3   |          4  | 14.390        | 4/25/2016          |
    |              4   |          5  | 1.720         | 4/25/2016          |
    |              5   |          6  | 118           | 4/25/2016          |
    |              6   |          7  | 9.468         | 4/25/2016          |
    |              7   |          2  | 13.125        | 6/15/2016          |
    |              8   |          3  | 10.520        | 6/25/2016          |
    |              9   |          4  | 14.800        | 6/25/2016          |
    |              10  |          5  | 1.800         | 6/25/2016          |
    |              11  |          6  | 120           | 6/25/2016          |
    |              12  |          7  | 9.320         | 6/25/2016          |
    |           100000 |          7  | 9.500         | 6/25/2016          |
    +------------------+-------------+---------------+--------------------+
    

    reference_currency:

    +-----------------+---------------+-----------------------+
    | currency_id      |currency_symbol| currency_name         |
    +------------------+---------------+-----------------------+
    |              1   |          USD  | US Dollar             |
    |              2   |          AUD  | Australian Dollar     |     
    |              3   |          EUR  | Euro                  |
    |              4   |          HKD  | Hong Kong Dollar      |
    |              5   |          JPY  | Japan Yen             |
    |              6   |          SGD  | Singapore Dollar      |
    |              7   |          MYR  | Malaysian Ringgit     |
    |              8   |          CHF  | Swiss Franc           |
    |              9   |          THB  | Thailand Baht         |
    |              10  |          GBP  | Great Britain Pounds  |
    |              11  |          SEK  | Swedish Krona         |
    |              12  |          CNY  | China Yuan            |
    |              25  |          SAR  | Saudi Arabian Riyal   |
    +------------------+-------------+-------------------------+
    

    当我跑步时:

    SELECT
    a.invoice_id AS 'INVOICE_ID',
    a.currency_id AS 'CURRENCY_ID',
    a.amount AS 'AMOUNT',
    a.invoice_date AS 'INVOICE_DATE',
    (
    SELECT b.exchange_rate FROM table_exchange_rate b
    WHERE b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id
    ORDER BY b.exchange_rate_date DESC LIMIT 1) AS 'EXCHANGE RATE'
    )
    FROM table_invoice a
    

    结果:

    `+------------+-------------+----------+--------------+--------------+
    | INVOICE_ID | CURRENCY_ID | AMOUNT   | INVOICE_DATE | EXCHANGE_RATE |
    +------------+-------------+----------+--------------+---------------+
    |          1 |      2      |     10   | 4/28/2016    | 13.352          
    |          2 |      3      |     30   | 4/29/2016    | 10.195
    |          3 |      4      |     50   | 4/30/2016    | 14.390
    |          4 |      2      |     40   | 6/18/2016    | 13.125
    |          5 |      6      |     25   | 6/20/2016    | 118
    |          6 |      7      |     87   | 6/25/2016    | 9.320
    

    `

    并且结果工作正常,但是非常慢(大约> 60秒),60k记录(table_invoice)循环在100k记录(table_exchange_rate)上查找当前日期的汇率,

    如果invoice_date与exchange_rate_date不匹配或者用户未在应用上输入汇率,它将使用汇率在当前日期(b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id)之前输入的最新记录

    我可以加快此查询或其他选项吗?谢谢..

1 个答案:

答案 0 :(得分:0)

使用JOIN

尝试相同的操作
select a.invoice 'invoice_id', a.currency_id 'curency_id', a.amount 'amount', a.invoice_date 'invoice_date', ter.exchange_rate ' exchange_rate'
from table_invoice ti
left join table_exchange_rate ter on ter.currency_id = ti.currency_id and ter.exchange_rate_date <= ti.invoice_date