Improvements to entity tracking of query results.

- Entities not returned by the outer result set are no longer tracked.
- Introduced a query StateEntry buffer to isolate query execution from the StateManager.