wosis
wosis

Reputation: 1219

MySQL Performance: Many tables or many rows?

I want to log access to pages in my PHP/MySQL app to implement a view count similar to the one on SO. My plan is to count the requests by unique IP addresses on each page. There about 5000 different pages with a view-count. (I know counting IPs is not exact but that is OK for my purposes.)

I see two options to do organize the database tables:

Which one is better generally and performance wise? Or am I completely on the wrong track?

Upvotes: 6

Views: 1350

Answers (2)

Kevin Holditch
Kevin Holditch

Reputation: 5303

Wouldnt a better a approach to be to have a table that stores DateTime of access, page id, ip address etc etc. Then every time a page is access you simply add a row to the table. That will give you the data at a raw level and then you can simply aggregate it to answer the questions that you want.

Storing the data in this way also allows you to answer more granular questions like how many page views were made on a particular day or week? Which you wouldn't be able to do with the table structure you have purposed in your question.

Upvotes: 2

Marc B
Marc B

Reputation: 360812

5000 tables means 5000 different queries + 5000 different sets of index + 5000 different sets of data competing for space in the server's caches. Performance will most likely be abysmal.

Multiple tables storing exactly the same data structure is almost ALWAYS a bad design. If you're worried about performance, you can use MySQL's partitioning support to split the table into multiple pieces automatically, and that's done transparently to the end-user (eg. your queries).

Upvotes: 7

Related Questions