我有这个问题:
.swift
我尝试做的是通过加入与交易日期{{匹配的第一个时间记录 SELECT
mt.market_coin_id AS coin_id,
mt.quantity AS quantity,
(ha.price_eur * mt.quantity) as test
FROM
market_transactions mt
JOIN historical_data ha ON ha.id = (SELECT id FROM historical_data WHERE historical_data.coin_id = mt.market_coin_id ORDER BY mt.trade_at <-> snapshot_on LIMIT 1)
WHERE
mt.user_id = 1
的历史定价表,即时计算价格(test
) 1}}
然而查询似乎很慢。
假设所有内容都被编入索引,是否有正确的方法使这个查询更快一点?
historical_data.snapshot_on
mt.trade_at
historical_data
ID | COIN_ID | SNAPSHOT_ON | PRICE_USD | PRICE_EUR |
------|---------|-------------|-----------|-----------|
42794 | 422 | 2017-07-31 | 0.0001353 | 0.0001152 |
42795 | 422 | 2017-08-01 | 0.0001144 | 0.0000968 |
答案 0 :(得分:1)
我会选择这样的事情:
SELECT mt.market_coin_id AS coin_id, mt.quantity AS quantity,
(SELECT ha.price_eur * mt.quantity
FROM historical_data ha
WHERE ha.coin_id = mt.market_coin_id AND
ha.snapshot_on <= mt.trade_at
ORDER BY ha.snapshot_on DESC
LIMIT 1
) as test
FROM market_transactions mt
WHERE mt.user_id = 1;
然后您需要market_transactions(user_id, market_coin_id, quantity)
和historical_data(coin_id, snapshot_on)
上的索引。