improve sql script