TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 09-10-2008, 03:02 PM   #1 (permalink)
The Acquainted
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 177
Thanks: 34
ETbyrne is on a distinguished road
Default How should I layout this database?

I've been working on a social networking website that if things go well it could potentially get very large in the future so I've been thinking about how I should have the MySQL database(s) layed out. Here are my ideas.

(1) Have one database that stores all of the information in HUGE tables.

(2) Have two databases. One that stores information like user login details, front page articles, and other stuff like that. The Second one would be made up of separate tables for each user.

For example the user "evan" would have four (or more) tables in the database. Ex:
Code:
evan_comments
evan_photos
evan_albums
evan_groups
I'm looking for the best solution performance wise while keeping it as neat and scalable as possible. Any suggestions?
__________________
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 09-10-2008, 08:49 PM   #2 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 354
Thanks: 3
xenon is on a distinguished road
Default

That's extremely bad for the server that processes the requests. A single MySQL table can work easily with as many as 1 billion rows. And that is alot of info. This way, it only has to deal with one physical file (opening it, reading it, etc.), instead of many table files (equal to the number of tables per user multiplied by the number of users).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
ETbyrne (09-10-2008)
Old 09-11-2008, 06:16 PM   #3 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

Those were my thoughts exactly. Properly designed tables will scale WAY better than tables for each user.

I personally normalize most of my database stuff. In your example having a user table, an article table, a comments table, a photos table, an albums table, and a groups table would make the most sense. These could all contain the user id and/or the article id as indexes like the following:

tblUsers:
sql Code:
CREATE TABLE `tblUsers` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(50) NOT NULL,
  `last_name` VARCHAR(50) NOT NULL,
  PRIMARY KEY (`id`)
)

tblArticles:
sql Code:
CREATE TABLE `tblArticles` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `article_name` VARCHAR(100) NOT NULL,
  `article_text` TEXT NOT NULL,
  `article_timestamp` INTEGER UNSIGNED NOT NULL,
  PRIMARY KEY (`id`)
)

tblComments
sql Code:
CREATE TABLE `tblComments` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` INTEGER UNSIGNED NOT NULL,
  `article_id` INTEGER UNSIGNED NOT NULL,
  `comment_text` TEXT NOT NULL,
  `comment_timestamp` INTEGER UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `UserIndex`(`user_id`),
  INDEX `ArtIndex`(`article_id`)
)

More could be done with foreign keys.

If you wanted to get all the comments for an article with user names you could do something like the following:

sql Code:
SELECT tblComments.comment_text,
   tblComments.comment_timestamp,
   CONCAT(tblUsers.first_name, ' ', tblUsers.last_name) AS username,
FROM
   tblArticles
JOIN
   tblComments ON tblArticles.id=tblComments.article_id
JOIN
   tblUsers ON tblComments.user_id=tblUsers.id
WHERE tblArticles.id=somearticleid;

This is not tested, but I think it illustrates the point pretty well.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
The Following User Says Thank You to buggabill For This Useful Post:
codefreek (10-31-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:56 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0