It seems quite easy to fix this through a subquery:
SELECT
transaction.*,
(SELECT rate
FROM rate
WHERE rate.timestamp<transaction.timestamp and rate.currency=transaction.currency
ORDER by created_at DESC
LIMIT 1) as rate
FROM transaction;
The query above is lightning fast if I add "WHERE transaction.id=5" to the query, but it grinds to a halt if I add "WHERE transaction.id<=5", even though it should also be fast.
It seems trivial to fix this by making a loop in an external script, but is it at all possible to do this in a scalable way through a clean SQL query in MySQL?