I am working on the web application developed in ASP.Net using C#. Allong with other feature, there is a feature which allow users to add various items to their personal list. The database tables relevant to above features are:
-------------------------
| Item |
-------------------------
| ItemId (Primary Key) |
| . . . & other attribs |
-------------------------
---------------------------
|List |
---------------------------
|ListId (Primary Key) |
|. . . and other attribs |
---------------------------
Since there is many to many relationship among above two table (ie One Item is added in many lists and one list contains many items) So i create third table for that
---------------------------------
|ItemList |
---------------------------------
|ItemId (Foreign Key) |
|ListId (Foreign Key) |
|. . . and other Attributes |
---------------------------------
Now, I in need to maintain track that how many times a certain item is added in the list. In order to acheive this i have two possible options
Please suggest which approach is better and why? I am more declined towards the second approach, and regarding efficency i guess both have same
Second approach is better from a data purity point of view. Let the data tell its own natural story - if you can work out how many lists an item is in by doing a quick Select count(ListId), ItemID as CountOfLists from ItemList Group By ItemID
then it's a far more attractive option than having to find all the spots in the code that modify ItemList
and make sure they update Item.Count
You'd consider the first option only if you were looking to denormalise/pre-aggregate the data for performance reasons. You've got to do extra work to make sure that the Count column in your Item table is always accurate (or, eventually accurate)
Option #1 will perform better for read operations because the count has already been aggregated. The downside to this approach is that you must:
Option #2 will perform better if you're application has more insert/deletes compared to reads. The reason for this is that SQL Server only needs to calculate the count when it is needed.
I would suggest going with option #2 unless you have identified performance issues that would make you want to take a different approach.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.