Format and optimzed sqls