bug fix, optimization sql