improved sql query for meta searches