How to do an efficient search in a database