Optiomized query count, added confirg parameters for custom query or constraints