optimize sql statements