Distributed Database Query Optimisation with Lego