Topic: Search Results Cache

Hi - I've been developing a site for somebody and they want advanced search capabilities. What this has resulted in is a complicated set of queries, that I believe hit the database relatively hard. My question is if it is possible to cache the results from this complex query (and paginate them in the view). Thanks!

Re: Search Results Cache

bump?

Re: Search Results Cache

Well, a database query should only fetch the records it needs to display on the page, so you can't really cache all the records in the search in Rails without fetching them all to begin with. This will probably lead to even poorer performance.

There may be a solution at the database level, but I'm not too familiar with that.

Railscasts - Free Ruby on Rails Screencasts

Re: Search Results Cache

If the query is complex, but the result set is small (in general) you could choose to fetch just the id's for all the records, and then cache those ordered id's in your session.  Its a small amount of data.  However, if you suspect LARGE result sets, even fetching just the ids can be time consuming.

However, with this method you can, on subsequent page requests use the ordered id array and a page offset to then fetch by id only those objects you need for that search results page or for a individual detail page with prev and next item links.

All that said, I would also recommend optimizing your queries / indexes.

Trevor Rowe

Re: Search Results Cache

Oh yea! For some reason I didn't think of that, great advice. I'll just store the resulting IDs into memcache or the session and load the records based on something like params[:page]

One more thing: can a dynamic query search for only id's? Right now I am doing something like Art.find_all_by_color_id , I wonder if Art.find_all_ids_by_color_id would work (I'm not on my Rails box until tonight) and if that would reduce the load on the server.