specterious
n00b
- Joined
- Jun 6, 2003
- Messages
- 60
This is for a document mangement script I've been working on.
I have 4 tables so far:
users
documents
photos
logins
For each user i'd like to count up the # of documents, photos, and logins. They are related by a userid (primary in the users table).
The first thing I did was query the users table selecting * and looping through it querying each of the other 3 tables counting the rows with that userid.
This took about 0.06 seconds.
Then I made this query:
This took 0.18 seconds. I thought it would be faster using 1 query instead of 3 times the # of rows (about 20).
I have 4 tables so far:
users
documents
photos
logins
For each user i'd like to count up the # of documents, photos, and logins. They are related by a userid (primary in the users table).
The first thing I did was query the users table selecting * and looping through it querying each of the other 3 tables counting the rows with that userid.
This took about 0.06 seconds.
Then I made this query:
Code:
select users.*,
count( distinct documents.documentid ) as documents,
count( distinct photos.photoid ) as photos,
count( distinct logins.loginid ) as logins
from users
left join documents on documents.userid = users.userid
left join photos on photos.userid = users.userid
left join logins on logins.userid = users.userid
group by users.userid
This took 0.18 seconds. I thought it would be faster using 1 query instead of 3 times the # of rows (about 20).