trying optimize sql queries