Query optimization in distributed database systems