tags:

views:

151

answers:

6
+3  A: 

Using a Sql query you can quickly and inexpensively get the total number of records using the max function.

It is better to generate the total then keep it as a record, the same way as you would keep a persons birth date and determine their age then keep their age.

skyfoot
I think his 'total' refers to a sum of values, not the number of records.
Henrik Opel
Yes, thanks Henrik.
ChrisC
+1  A: 

You "could" calculate the total with SQL (I am assuming you do not want total number of records ... the price total or whatever it is). SQL is quite good at mathematics when it gets told to do so :) No storing of total.

But, as it is all run on the client machine, I think my preference would be to total using C#. Then the business rules for calculating the total are out of the DB/SQL. By that I mean if you had a complex calculation for total that reuired adding say 5% to orders below £50 and the "business" changed it to add 10% to orders below £50 it is done in your "business logic" code rather than in your storage medium (in this case SQL).

Kindness,

Dan

Daniel Elliott
It's a stand-alone program on a user's PC. (I edited my question to show this.) Does this change your mind?
ChrisC
A little, edited to reflect this :)
Daniel Elliott
Thank you. Could you explain what you mean when you said, "Then the business rules for calculating the total are out of the DB/SQL."?
ChrisC
A little explanation added :)
Daniel Elliott
Thank you Agileguy.
ChrisC
+2  A: 

How offten and by what number of users u must get this total value, how offten data on which total depends are updated.

Maybe only thing you need is to make this big query once a day (or once at all) and save it somewhere in db and then update it when data, on which your total consist, are changed

MoreThanChaos
It could be updated several times a day and be requested numerous times a day. How much is the performance price?
ChrisC
+4  A: 

Usually it is totally acceptable and even recommended to recalculate values. If you start storing calculated values, you'll face some overhead ensuring that they are always up to date, usually using triggers.

That said, if your specific calculation query turns out to take a lot of time, you might need to go that route, but only do that if you actually hit a performance problem, not upfront.

Henrik Opel
Thanks, Henrik.
ChrisC
+3  A: 

If you have appropriate indexing in place, it won't be too bad to do on demand calculations. The reason that I mention indexing is that you haven't specified whether the total is on all the values in a column, or on a subset - if it's a subset, then the fields that make up the filter may need to be indexed, so as to avoid table scans.

Pete OHanlon
There would be criteria to decide which rows are used for calculating the total. Is that what you mean?
ChrisC
Yes - that's exactly what I mean. You might want to run a query profiler on your database to see if the query is table scanning or not.
Pete OHanlon
Thanks. Dumb Question: Does your suggestion apply if I'm using Linq?
ChrisC
Yes it does. L2S converts to SQL behind the scenes, so you have exactly the same things to contend with.
Pete OHanlon
Gotcha. So I should get it all set up and running, then run a query profiler to see if it's scanning the entire table or not?
ChrisC
Exactly. It's fairly easy to accomplish.
Pete OHanlon
If I found that it was scanning the entire tables, is that bad enough to make me prefer storing the total somewhere?
ChrisC
No - I'd just look at adding appropriate indexes into the database. Basically, you'd want to look at indexing at least some of the columns that make up the criteria.
Pete OHanlon
Ah. Thanks a lot.
ChrisC
A: 

I think that it should not take long, probably less than a second, to generate a sum from 8000-10000 records. Even on a single PC the query plan for this query should be dominated by a single table scan, which will generate mostly sequential I/O.

Proper indexing should make any joins reasonably efficient unless the schema is deeply flawed and unless you have (say) large blob fields in the table the total data volume for the rows should not be very large at all. If you still have performance issues going through an O/R mapper, consider re-casting the functionality as a report where you can control the SQL.

ConcernedOfTunbridgeWells
Thanks ConcernedOfTunbridgeW. (whew)
ChrisC