MySQL Forums
Forum List  »  Performance

Re: bad Join Performance
Posted by: leo mueller
Date: November 02, 2011 08:21AM

select count(distinct usr_id) from tags
Result: 2
Ive got 2 Users

select count(*) from tags
Result: 128
These 2 Users create 128 unique tags (tag_id)`s


SELECT usr_id,count(*) FROM tags Group by usr_id
Result:
1 10
2 118

User 1 create 10 tag_id´s, User 2 create 118 tag_id´s

select count(*) from base
Result: 8545650

each record contains a hash value (not uniqe) bound to a tag_id and a timestamp.
hash + tag_id more often but hash + tag_id + timestamp should be unique.


Hash is an item you can tag several times with the same tag. It will be stored in the base table and thats why i can't make hash+tag_id to an uniqe key.
The tags table just stores the Tag´s name and the User Ownership.

So if i want count how many Hash Items and how they are tagged i would do:
SELECT count(*),tag_id FROM base group by tag_id
and fast like it should be

if i want to see just tags from a user i have to join the tags table:
SELECT count(*),tag_id FROM base JOIN tags USING(tag_id) WHERE usr_id=1 group by tag_id
still fast

Now, I want to see how many items tagged with tag_id 75
SELECT count(*),tags.tag_id FROM base JOIN tags USING(tag_id) WHERE usr_id=1 AND tag_id=75 group by tags.tag_id

now comes the tricky part. I want to see what other tags these hash items got, based on tag_id 75
SELECT count(*),tags.tag_id
FROM base 
INNER JOIN tags ON base.tag_id = tags.tag_id 
INNER JOIN base REV0 ON REV0.`hash`=base.`hash` 	
WHERE 
usr_id = 1 
AND REV0.tag_id=75 
GROUP BY tags.tag_id
and now hell breaks loose and performance drops rapidly



Edited 4 time(s). Last edit at 11/02/2011 12:48PM by leo mueller.

Options: ReplyQuote


Subject
Views
Written By
Posted
3734
October 31, 2011 12:21AM
1089
October 31, 2011 12:41PM
937
November 01, 2011 12:12AM
1166
November 01, 2011 12:26AM
953
November 01, 2011 03:33AM
1055
November 01, 2011 08:29AM
Re: bad Join Performance
968
November 02, 2011 08:21AM
890
November 03, 2011 12:56AM
901
November 03, 2011 12:24AM
1036
November 04, 2011 06:35AM
989
November 02, 2011 06:23AM
904
November 02, 2011 07:42AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.