I'm running tests to see if my queries are optimized and my database can handle what the website needs
Part of the application will be basically a friends bucket like myspace or facebook.
I have the following two tables in my test
CREATE TABLE `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` int(11) NOT NULL,
`type` int(11) NOT NULL,
PRIMARY KEY (`uid`),
KEY `name` (`name`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE `friends` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`myid` int(11) NOT NULL,
`uropt` int(11) NOT NULL,
`fid` int(11) NOT NULL,
`uiopt` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `myid` (`myid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Us
er table is obviously name, age, account type,
the friends table is basiically a opt in / out firend relationship thing
it's user request | user opt in status (1=pending, 2=approve, 3=denied, 4=banned) | friend id requeted | friend opt status (same thing as user)
So some basic infor for user would be
ID | SHIRLY | 23 | 2
ID | BOB | 32 | 2
ID | KELLY | 18 | 2
ID | RICH | 22 | 2
The Friend table basic info is
ID | 232 | 2(defualt for request) | 9232 | 1 -- pending
ID | 223 | 2 | 9232 | 2 - friends
ID | 223 | 3 | 9232 | 2 - user 1 deleted
ID | 223 | 2 | 9232 | 4 - user 2 banned
I have 1500 rows in the USER Table
and 1,500,000 Rows in the friends table for testing.
This SQL ranges from 1.5 secs to .00226 ses:
SELECT `t1`.`name`, `t1`.`age`, `t3`.`name` AS `friend` FROM `users` AS `t1`
LEFT JOIN `friends` AS `t2` ON (`t1`.`uid` = `t2`.`myid`)
LEFT JOIN `users` AS `t3` ON (`t2`.`fid` = `t3`.`uid`)
WHERE `t1`.`uid` = '150' LIMIT 0, 1000
while this sql take a range from .5 to .0125
SELECT `id` FROM `friends` WHERE `myid` = '232' LIMIT 1000
What I want is to be able to procude ID numbers of a person's friends, and their friend's friends' To allow for who can click on what
So far considering the rows number in the 1.5 million, i think my times are alright on my working computer that also runs as my develpment server.
One question I do have,
What is the best approach to pair id's from the friends table with info from the User table without doing 3 levels of left joins?