5

I have an application whose first page after login presents the user with a lot of data. The query against the database takes a long time - let's say 10 seconds. The database is in SQL Server 2008, the query run is constructed as a prepared statement in Java. The data that needs to be displayed though will generally only change once per day. So - if I could have this result cached, and display a lot quicker that would be ideal.

However, I'm not really sure what I need to do to bring down this 10 second wait - every time I load the page the same delay happens. Is there something I need to do to make sure SQL Server caches the results to the query? It certainly doesn't seem to be any quicker on a second load at the minute - where should I start exploring?

All help is much appreciated!

5
  • 1
    You should probably explore application caching since you know how long the data is good for. Aug 11, 2011 at 19:59
  • Thanks, sounds like a good idea. But, before looking too far down that road - there's not something obvious I'm missing in terms of db config that would make my 2nd db call loooooads quicker???
    – QuakerOat
    Aug 11, 2011 at 20:04
  • No, the obvious first thing to try is plain old query optimization, not DB cache configuration. Aug 11, 2011 at 20:05
  • 1
    Ok, so let's say I could knock down the query time from 10 seconds to 5 seconds through query optimization - I'd still much prefer that the result to this gets cached somewhere, somehow, and hopefully returns much more quickly than 5 seconds. Or am I barking up the wrong tree?
    – QuakerOat
    Aug 11, 2011 at 20:08
  • @Quaker - see my answer. You don't describe your query but if it's complicated and/or includes aggregates, an indexed view may work for your needs.
    – JNK
    Aug 11, 2011 at 20:08

2 Answers 2

2

You should look into indexed views.

They let you store in an index the results of a query (in the form of the view) that the optimizer can access indirectly or directly (i.e. it "knows" it has the data and will pull it for queries besides those against that view).

There are a number of constraints as outlined in the link.

They are the only really viable way (besides creating and managing results tables) to cache complicated query results like aggregates.

I'm guessing your query is complicated and/or contains aggregated fields and grouping, which will ALWAYS entail processing overhead even if the data pages are already cached in memory.

1
1

If the data is only updated once a day, consider writing the results to a regular table and updating the table periodically (assuming you have the access necessary for that).

2
  • I do have the access necessary for that, and this has actually already crossed my mind. But - what is the benefit of this over application caching for example?
    – QuakerOat
    Aug 11, 2011 at 20:27
  • I could be wrong, but I am not aware of an easy cashing mechanism for SQL Server (other than what happens automatically). Also, from what I have read on indexed views, be careful with them since they can be cause maintenance issues - DBAs usually discourage them. Aug 11, 2011 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.