refactoring
fix query optimization bug