Rev 8506 Bug 819432: Execute queries in two steps to improve performance