Only flush entities that may be affected by pending query