WIP: CS-5432

First draft for optimizing backend search.

Please merge takeit's sql_optimize branch first.
same as comment above