2017-02-21 73 views
0

我有3個表:選擇子查詢中選擇的時間太長

  1. 60.000記錄發票

  2. 100.000記錄的匯率基於當前日期

  3. 25記錄貨幣符號

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

我可以加快此查詢或有任何其他選項嗎?謝謝。

+0

使用加入與同樣的事情,通常速度比運行子查詢 –

+0

向我們展示說明你的查詢** EXPLAIN SELECT a.invoice_id AS「INVOICE_ID」, 一個.amount AS '金額', ( 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 '匯率' ) FR OM table_invoice a; ** –

+0

EXPLAIN本身並沒有太大的幫助。我們通常也需要查看每個相關表的SHOW CREATE TABLE語句。也就是說,像這樣的相關子查詢通常比不相關的子查詢表現得更差。 – Strawberry

回答

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 
+0

對我來說看起來不一樣。而'LEFT JOIN ... <='不是一個現實的構造。 – Strawberry