SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    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?

  2. #2
    An average geek earl-grey's Avatar
    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.

  3. #3
    SitePoint Enthusiast
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •