我有两个表assetList和VWAPUSD,我试图通过匹配两个表中的quoteAsset和交易品种值来计算最近24小时的价格差。
问题:我正在尝试以下查询确实显示了差异,但是30-40%的查询返回null的时间,而且执行时间过长,直到我设置了限制。 / strong>
VWAPUSD表中的价格每3秒更新一次,并每2秒查询数据库一次以获取最近24小时的价格差。
assetList表保存当前价格,VWAPUSD表价格每3秒更新一次。
assetList表
+---------+--------+--------+--------------+---------------------+
| assetId | pair | symbol | lastPriceUSD | createdAt |
+---------+--------+--------+--------------+---------------------+
| 1 | EURUSD | EUR | 0.8900000000 | 2019-05-12 18:05:10 |
| 2 | AUDEUR | AUD | 0.6500000000 | 2019-05-12 18:05:45 |
+---------+--------+--------+--------------+---------------------+
VWAPUSD表
+----------+--------+------------+--------------+---------------------+
| tickerId | pair | quoteAsset | lastPriceUSD | createdAt |
+----------+--------+------------+--------------+---------------------+
| 1 | USDEUR | EUR | 0.9500000000 | 2019-05-11 18:06:40 |
| 2 | EURAUD | AUD | 0.7500000000 | 2019-05-11 18:17:49 |
+----------+--------+------------+--------------+---------------------+
SELECT
al.pair
, ( SELECT 100 * (al.lastPriceUSD - vw.vwapUSD) / al.lastPriceUSD
FROM VWAPUSD vw
WHERE vw.quoteAsset = al.symbol
AND vw.createdAt >= now() -interval 1 day
AND vw.createdAt < now() -interval 1 day + interval 150 second
) diff24h
FROM assetList al
预期结果
symbol difference
EUR -6.00%
AUD -10.00%