Refactoring for detailed statistic(SQL optimization)