Slowly sub-query issue while checking SQLs syntax