Jump to content

Help optimizing query ..


lampstax

Recommended Posts

EXPLAIN SELECT `u`.*, count(`c`.`commentID`) AS `ccount` FROM `usr` AS `u` LEFT JOIN `comments` AS `c` ON `c`.`usrID` = `u`.`usrID` GROUP BY `u`.`usrID`

 

Result is

 

id select_type table type possible_keys key key_len ref rows Extra

1 SIMPLE u ALL NULL NULL NULL NULL 47122 Using temporary; Using filesort

1 SIMPLE c ref usrID usrID 4 alternet_staging.u.usrID 19

 

--------------------------------------

 

There's actually 600k+ rows in the comments table and about 50k rows in usr.

 

If I remove the 'count' portion of it, it runs pretty quick, but is a pig otherwise.  What am I doing wrong?

Link to comment
Share on other sites

Hi,

 

Sorry for the lack of information Fenway, I just read the link in your signature.

 

Table Usr

 

[pre]

Keyname  Type  Cardinality  Field

PRIMARY PRIMARY 47124  usrID

email UNIQUE 47124  email

url         INDEX 5890          url

password INDEX 47124          password

username INDEX 47124  username

[/pre]

 

Table Comments

 

[pre]

Keyname  Type  Cardinality  Field

PRIMARY  PRIMARY  630584          commentID

storyID INDEX 18546          storyID

parentID INDEX 315292          parentID

usrID INDEX 33188  usrID

[/pre]

 

Both tables has index for usrID.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.