0

If you have a table called posts, which contains id, title, content .etc, how would you design a database table to hold meta data about each posts, e.g. how many times it was visited today, this week, etc, and so you can work out which post was most popular this week.

Also whilst I'm here, what is the difference between indexes and inner joins. I know this is similar to this Most efficient database design for a blog (posts and comments) question:

I have three tables, post, author_posts and author and I use inner join to find authors of x post, posts contains a field called author_id, however I have seen some sites which do not contain foreign keys, instead they do something like this:

> post          author_posts    author     
>  id               id, title,     id, post_id,   
>  name, content     author_id     username

Which one would be the quickest/ which one should you use?

Community
  • 1
  • 1
john mossel
  • 2,158
  • 5
  • 24
  • 39
  • "what is the difference between indexes and inner joins": if you don't know that, should you really be designing databases? (that is not meant as an insult, but a serious question) – Mitch Wheat Dec 23 '10 at 03:25
  • I'm not really, Im just messing about with sql...lets just say its not my day job – john mossel Dec 23 '10 at 03:27
  • You have to very distinct questions and you should post them separately. – Larry Lustig Dec 23 '10 at 03:28
  • ah OK, a join is a domain relation, usually from an FK to a PK. An index is not a domain object. An index is a mechanism for locating rows faster (think phone book) – Mitch Wheat Dec 23 '10 at 03:29

2 Answers2

0

For this sort of thing you're going to get killed if you're doing calculations in real time. Use cases like this are a very good candidate for denormalization.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
0

Number of visits is a very particular kind of meta data about a post and the solution for that will probably be different from storing other kinds of meta data.

Counting "visits" for a particular resource on your website involves making some decisions about just what a "visit" is. Will you have login information to decide who's doing the visiting, or will you just be using an IP address? What amount of elapsed time triggers a new visit from the same IP?

That said, you have two choices.

You can store one record per visit (with the date and time) and count those records when you want to know how many people visited. The advantage is that you can analyze any time period and you have a place to store extra information about the visit. However, if you get a lot of visitors and do the calculations frequently, this can cause a load on your application.

Alternatively, you can keep a counter of how many visits you had in each discrete time period. You should use the smallest time period you might want to analyze (perhaps, one counter for each date or perhaps for each hour of each day, depending on your eventual use). Each counter is a row in a table with the post ID, date or time descriptor, and an integer count. When you detect a new visit, you simply update the counter for the current time period. You may need to keep a cache of recent visit details to help you decide whether you have a "new" visit, or a continuation of an existing one, but if a visit is simply a request for the page then you don't need to do this, just update the appropriate counter.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160