473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Number of SELECT queries appropriate in a page

Hi folks,

What I'd like to do for a website I'm designing with PHP/MySQL is have
a number of registered users who can make friends with each other...
so if person 1 wants to be friends with person 2, 1 can add 2 and then
they will be recognized as friends. I also want, if person 3 is
friends with 2, and goes to person 1's page, person 3 will see that 1
is friends with 2 who is 3's friend. So 3 can see the connections that
lead him to anyone else, up to, say, four friends away.

The design I've come up with so far is that each user will have a
unique userid in a users table, and a Friends table which will contain
two keys: friendID1 and friendID2; basically a many-to-many
relationship. Each time a friend is made a row will be inserted which
will relate one user to another. Then, on the user's profile page, a
SELECT clause will find all the user's friends (WHERE friendID1 =
userid OR friendID2 = userid).

What I'm wondering about is the connections. The best way I can think
of to do this would be, for example, if 3 visited 1, to select all of
3's friends and all of 1's friends, and find the ones in common. Those
are second degree connections. Then select all of 1's friends' friends
(which could be in the hundreds) and compare them to 3's friends and
3's friends' friends, and find the ones that are in common. That would
be third or fourth degree, depending which matches up.

Is that the most efficient way to do this? The site has to be capable
of hundreds of thousands of hits a day, as well as being very speedy.
Could this be an appropriate use of a "heap table"? Any suggestions
anyone can give me as to how to most efficiently execute this task
would be very much appreciated. Thanks.

-Sam
Jul 20 '05 #1
1 1571
Sam G wrote:
Hi folks,

What I'd like to do for a website I'm designing with PHP/MySQL is have
a number of registered users who can make friends with each other...
That's very sweet. I'm sure we would all like this to happen! :-)
What I'm wondering about is the connections.
In Joe Celko's "SQL for Smarties", he discusses relational
representations of recursive data structures like trees and graphs.

Basically, once you have a table for users and a table of direct
friendships, you can create a third table that enumerates all unique
paths through the web of connections.

CREATE TABLE pathenum (
friend1 integer not null,
friend2 integer not null,
pathlength integer not null default 0
);

You need to load this table to record all connections. If Jay and Conan
are friends, then enter a row for Jay-to-Conan, length 1. If Conan is
also friends with Ellen and Oprah, then enter Conan-to-Ellen, length 1
and Conan-to-Oprah, length 1. Then also enter Jay-to-Ellen, length 2
and Jay-to-Oprah, length 2. Keep going, as you find the other friends
of Oprah and Ellen, until you run out or you find a friendship
connection back to Jay. This can be done iteratively or recursively.

Now you have a complete graph. You can fetch all users connected to Jay
with SELECT * FROM pathenum WHERE FRIEND1 = Jay_ID OR FRIEND2 = Jay_ID.

If someone new (Dave) is added to the web as a friend of Jay, you enter
Dave-to-Jay, length 1, and then select all connections of Jay, and
insert a new record for each one, substituting Dave for Jay and adding 1
to the pathlength.

It becomes a little bit complex to update this table if someone leaves
the web; it might be easier just to empty the pathenum table and
re-execute the search that populates it based on the current state of
the Friends table.

For more details and pseudocode samples, look in the book "SQL for
Smarties".
Could this be an appropriate use of a "heap table"?


Maybe, but not necessarily. Heap tables (or Memory tables) are
logically similar to conventional tables. The only difference is that
they are stored in system memory, not on disk. They data are lost when
the MySQL server shuts down. They're almost like TEMP tables, but they
are shared among all clients.

It might be worthwhile to make the pathenum table be a Heap table, and
you'd have to rebuild it from your Friends table each time the server
starts up. The advantage would be speed, but as long as the fields of
pathenum are indexed, I believe that speed won't be a problem even if
the table is a conventional disk-based table.

Regards,
Bill K.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alan | last post by:
Hi there, Are there Excel charting gurus here?? If so then please read on... Sorry for the cross-post but I'm not familiar with the Excel groups. I've posted to asp.general because if I have...
0
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ?...
12
by: TP | last post by:
Here is my problem. I need to display a table about which I have no information except the table name. Using metadata I can somehow show the column names and record values. But my table has 1...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
1
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 ...
6
by: SR | last post by:
As a starter project for learning Python/PostgreSQL, I am building a Books database that stores information on the books on my bookshelf. Say I have three tables. Table "books" contains rows...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
7
by: Richard Hollenbeck | last post by:
How can a numeric field display results in fractions? If my recipe data requires 1/2 cup of something, for example, I'm willing to punch in 0.5 in the quantity field then select cup in the unit...
4
dlite922
by: dlite922 | last post by:
hey guys, I'm doing some brain storming and getting ideas to come up with a solution. I have a list of ....data... that is displayed 10 per page with the LIMIT clause. Simply put: My...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.