fixed sql query on cleanup & optmize