views:

62

answers:

2

I'm writing a php-mysql application. The application allows the user to run a search against the database. When the search is run, if the user clicks on the back button to go back to it after examining an item brought up by it, they are asked if they would like to resend the query (it uses post). Which would rerun the search in the database. Using get wouldn't result in the request to repost - but it would still run the search again.

Is there a way to store the search and recall it, with out rerunning it in the database?

This can be saving it in the database, or somehow in the php - maybe with sessions? There can be a lot of data coming out of these searches, I feel like it's probably too much for sessions.

The other way I can imagine is to store it in the database, in a temporary table of some kind. Creating the table with the columns I intend to query, then selecting into an insert. Then selecting * from the table. This also seems rather like a bad idea. Lots of dynamically created temporary tables floating around.

I know this can be done. Saved searches are things you see in many web applications. So how is it generally done?

+2  A: 

memcached

or

APC

However, I notice that you're not giving any reason why you want to cache the results of a search. "Because it's faster, and I read that it makes my app more scalable," is the usual reason for this. This smells like premature optimization.

I recommend you build your application and profile it. How costly are these "searches"? Why are they costly? Is it poor schema design? Is it poor indexing? Is it poor choice of storage engine?

You do realize that MySQL has a query cache, right? In many cases this may be all you need. Try running one of your expensive "search" queries using the mysql client and note the time, then run it again. See?

hobodave
I didn't actually know that MySQL had a query cache. I'm teaching myself php and mysql as I go. Good to know, I'll give it a shot and see how it does.
Daniel Bingham
Sorry for the delay in accepting, took a while for me to get around to testing this.
Daniel Bingham
+2  A: 

to fix the "do you want to re-submit" error, you can store the request in the session or in the db, then immediately redirect to a new page to actually perform the search:

search.php?searchId=22

if (isset($_GET['searchId'])) {
  $search = $_SESSION['search'][$_GET['searchId'];
  // do search
} elseif (isset($_POST['search'])) {
  $_SESSION['search'][] = $_POST;
  header('Location: search.php?searchId=' . (count($_SESSION['search']) - 1);
}

that can then grab the query fields from $_SESSION['search'][22] or the db...

as for saving results, you can rely on the browser's cache, or you can store individual SERPs in the session or db... expiring out old ones at some point if needed...

creating a results table is fine (and would speed up pagination on complex searches), but you could potentially have millions of hits in there, so a pagination with caching would be more general way.

if the user wants to return to the search days or weeks later, you can just re-run the search with the saved query fields.

jspcal