WIP: CS-5432

First draft for optimizing backend search.

Please merge takeit's sql_optimize branch first.
okay, I think u can leave it as it is