Optimizing pathed and filtered queries