On a given instance with 600 000 stock.history, the query changed by this commit takes: - before this change => 32 minutes - after this change => 20 seconds opw-685747