Better solution for stackoverflow on large queries