Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello everyone!

I have 5 items for which 5 different tables are made in the database. On login, the page showing the no. of items the user has saved will be shown to him.

For this,
(1) should I save the no. (int) in the database adding one more column for each one and updating the no. each time an item is added or deleted

OR

(2) should I run a Stored Procedure containing 5 different SELECT COUNT(*) queries each time that page is called?
Please clear my doubt.

According to me, I should go with the first option to add columns to the database and update them and Avoid running SELECT COUNT(*) queries every now and then.
Please correct me if I am wrong.
Posted
Updated 12-Feb-12 2:34am
v3

1 solution

I would not add a column to tell how many items - use count as it can't get out of step.

But either your database design is very odd, or you have described it very badly. Why would you have five tables for different objects if (as it seems from the description) you can only have one row in each table?
 
Share this answer
 
Comments
Tech Code Freak 12-Feb-12 8:32am    
Thanks for the reply!
I have 5 tables for the items which individually contain 8-10 columns each. Maybe my description was a little vague..
Please reply.
I think that select count(*) will take more time for execution. Also, I will have to use output parameters for the procedure which lengthens the code too. Instead, saving count in the table will take comparatively lesser time to run select itemCount from tab1 where id=@id.
OriginalGriff 12-Feb-12 9:27am    
I still don't exactly see what you are trying to do - do you intend that each table should have one row of 8-10 columns?
Tech Code Freak 12-Feb-12 11:43am    
Not one row. The table will contain a list of different users and the specifications that they provide with that type of their item.
eg: TableItem1
ItemId UserId Spec1 Spec2 Spec3 .....
1 1 sp83 sp54 sp54 .....
2 1 sp76 ......................
3 2 .... .........

and so on..
OriginalGriff 12-Feb-12 14:07pm    
Then I would definitely go with the "count it as you go along" route, rather than include a count in the row itself. It may seem faster to count them and store them, but the difference is going to be pretty trivial, except when you want to update the record - when you would have to read it first, count it and write the count as well. There is also a risk of someone forgetting to update the count when they add or remove a specification.
Tech Code Freak 13-Feb-12 0:06am    
Thanks again for the reply!
While updating, the no. will just be incremented/decremented from the existing no. in that column. The updating is all done by coded Stored Procedures so there's no chance of forgetting to update it.
I have made a mistake in describing it. Instead of adding the 5 columns to the 5 tables, they must be added to the User table so that each time he adds/removes an item, then that column will be incremented/decremented. And also, there will be no chance to count the records every now and then. I'll just have to read one particular cell each time(select NoItem1 from User where Id=@id).
Sorry for that mistake!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900