views:

30

answers:

1

Dear all,

this time I got a more general question. Should I use multiple views rather than stored procedures for weighted aggregation of data, if the original data is updated periodically?

Basically I have a local MySQL database that is updated periodically by importing the same kind of data (tables) from a bigger transaction database.

The local database is used for statistical analysis. Thus I de-normalize (basically aggregate) the data locally for use with statistical software packages. So far I used stored procedures because I felt it was easier to handle (and arranged more clearly) when weighting schemes (basically other tables containing weights that are multiplied with variables) came into play.

Though the disadvantage of stored procedures is that I have the run all of 'em again when the tables are populated with new data. Obviously I am not a DBA... So don´t shy away from stating the obvious :) What´s the best approach to handle this kind of scenario? SP or views ? Or something completely different?

thx for any suggestions in advance!

+1  A: 

It depends (that's the generic answer to any "general" questions, isn't it? :) ). You need to evaluate the tradeoffs to see what the best solution is for your needs.

Views are basically just query re-writing (in MySQL), so using a view will be performing the aggregation/denormalization every time the query is run. That may make your queries slower that you would like. Also, if your procedures are really complicated, maybe it's not practical to try to put that logic into a view.

Stored procedures do the work once, so queries will be faster. But then your updates won't show up automatically. So I think the answer depends on how often the data changes, how often queries are run, and how important the performance of the queries is.

As for alternative suggestions, you could also run your stored procedures using events, if your data updates are regular, and you are just trying to save yourself from the manual task of running the procedures.

Another option is to have denormalization/aggregation tables that are updated with triggers. As you update your data in the source table, the triggers will automatically keep the aggregate tables current.

Here is a link to documentation for stored procedures, views, triggers, and events.

nathan
wow, that really helps. I really hope to get some more it-depends answers / opinion. Thx nathan!
ran2
In fact I got more than one sp. Probably in my case summing 'em up into one sp (i.e. use several sp inside another sp). On the other hand I do not expect performance problems, tables are not tiny but contain a handable ~ 10.000 lines at most.
ran2
If your tables are small, using views shouldn't be a problem, performance-wise.
nathan