views:

85

answers:

2

Hi guys,

We have a mySQL database table for products. We are utilizing a cache layer to reduce database load, but we think that it's a good idea to minimize the actual data needed to be stored in the cache layer to speed up the application further.

All the products in the database, that is visible to visitors have a price attached to them:

The prices are stored in a different table, called prices . There are multiple price categories depending on which discount level each visitor (customer) applies to. From time to time, there are campaigns which means that a special price for each product is available. The special prices are stored in a table called specials.

  • Is it a bad to make a temp table that binds the tables together?

It would only have the neccessary information and would ofcourse be cached.

-------------|-------------|------------ 
| productId  |  hasPrice   | hasSpecial
-------------|-------------|------------ 
  1          |  1          | 0
  2          |  1          | 1

By doing such, it would be super easy to know if the specific product really has a price, without having to iterate through the complete prices or specials table each time a product should be listed or presented.

  • Are temp tables a common thing for web applications or is it just bad design?
+2  A: 

If you're going to cache this data anyways, does it really need to be in a temp table? You would only incur the overhead of the query when you needed to rebuild the cache, so the temp table might not even be necessary.

Eric Petroelje
The problem of writing and keeping a cache table updated is always there, but then again, there would be immediate knowledge in the first query upon joining products with the temp table to know if there's interesting at all to go through the prices/specials table for each product?
Industrial
@Industrial - I think if you had a temp table, the problem with stale data could be worse, since not only could your cache be stale, but the contents of the temp table could be stale as well - especially if the schedule for refreshing them is not timed appropriately.
Eric Petroelje
Well, yeah. Cache invalidation is always a major issue with any kind of cache. It always come down to being a trade-off between cache misses and performance. Do you think that it's still bad database design, to utilize this kind of "temporary" tables?
Industrial
@Industrial - I wouldn't say it's bad design at all, just probably unnecessary in this case.
Eric Petroelje
A: 

You should approach it like any other performance problem: Decide how much performance is necessary, then iterate doing testing on production-grade hardware in your lab. Do not do needless optimisations.

You shoud profile your app and discover if it's doing too many queries or the queries themselves are slow; most cases of web-app slowness are caused by doing too many queries (in my experience) even though the queries are very easy.

Normally the best engineering solution is to restructure the database, in some cases denormalising, to make the common read use-cases require fewer queries. Caching may be helpful as well, but refactoring so you need fewer queries is often the best.

Essentially you can increase the amount of work on the write-path to reduce the amount on the read-path, if you are planning to do a lot more reading than writing.

MarkR