SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: keep counter vs count on fly
-
Jun 17, 2008, 00:33 #1
- Join Date
- Jun 2004
- Location
- california
- Posts
- 49
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
keep counter vs count on fly
I have a database of users who can have any number of friends. Sometimes, I just need the total number of friends (without need for whom they are). From a practical and efficiency standpoint, would it be wise to have a running counter of friends as a column for each user? This is in contrast to running a query each time on a friends' database to count the number of rows returned. Is there any standard for this?
-
Jun 17, 2008, 02:44 #2
- Join Date
- Mar 2005
- Location
- Ukraine
- Posts
- 1,403
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If the query is run very often, it's fine to denormalize. But, for better guaranteed consistency, you can put the counter updates in insert/delete triggers on friends relationship table, rather than in your server-side code.
-
Jun 17, 2008, 11:31 #3
- Join Date
- Jun 2004
- Location
- california
- Posts
- 49
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah, I just thought it wouldn't be efficient to pull out 5 rows each time from a 1 million row database 10 times each page.
Bookmarks