optimize query concatenation