Fixed the indefinite wait issue, which was due to incorrect placing of WHERE clause of the preliminary (COUNT(*)) SQL query.