![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello I have table BLOG id, user_id, creation_date, name, content (around 7000 records) And then I have table SITE_USERS id, name (around 20000 records) I want to select the latest blog entries, but one user should not appear more than once. Doing: select B.*, S.name from blog B, site_users S where B.user_id = S.id group by B.user_id order by creation_date is not so good. It seems that its not fetching the latest blog entry from a user (why should it? I dont think I say it in the query, sigh). So, I tought this query could work: select B.*, S.name from blog B, site_users S where B.user_id = S.id and B.id IN (select MAX(id) from blog group by user_id) order by creation_date It actually works but takes 20 (!!!!) seconds to execute. The problem seems to be when joining the two tables. Causa if I remove from th query the other table (SITE_USERS), then it executes without problems. Both tables have been indexed. And have the auto_increment and primary_key of course. I really cannot understand why it takes so much time when there is so little data. Or, well, I guess the subquery must be very bad itself I guess? |
#3
| |||
| |||
|
|
Hello I have table BLOG id, user_id, creation_date, name, content (around 7000 records) And then I have table SITE_USERS id, name (around 20000 records) I want to select the latest blog entries, but one user should not appear more than once. Doing: select B.*, S.name from blog B, site_users S where B.user_id = S.id group by B.user_id order by creation_date is not so good. It seems that its not fetching the latest blog entry from a user (why should it? I dont think I say it in the query, sigh). So, I tought this query could work: select B.*, S.name from blog B, site_users S where B.user_id = S.id and B.id IN (select MAX(id) from blog group by user_id) order by creation_date It actually works but takes 20 (!!!!) seconds to execute. The problem seems to be when joining the two tables. Causa if I remove from th query the other table (SITE_USERS), then it executes without problems. Both tables have been indexed. And have the auto_increment and primary_key of course. I really cannot understand why it takes so much time when there is so little data. Or, well, I guess the subquery must be very bad itself I guess? |
#4
| |||
| |||
|
|
On 2/16/2011 9:52 AM, pac Man wrote: Hello I have table BLOG id, user_id, creation_date, name, content (around 7000 records) And then I have table SITE_USERS id, name (around 20000 records) I want to select the latest blog entries, but one user should not appear more than once. Doing: select B.*, S.name from blog B, site_users S where B.user_id = S.id group by B.user_id order by creation_date is not so good. It seems that its not fetching the latest blog entry from a user (why should it? I dont think I say it in the query, sigh). So, I tought this query could work: select B.*, S.name from blog B, site_users S where B.user_id = S.id and B.id IN (select MAX(id) from blog group by user_id) order by creation_date It actually works but takes 20 (!!!!) seconds to execute. The problem seems to be when joining the two tables. Causa if I remove from th query the other table (SITE_USERS), then it executes without problems. Both tables have been indexed. And have the auto_increment and primary_key of course. I really cannot understand why it takes so much time when there is so little data. Or, well, I guess the subquery must be very bad itself I guess? So, what's the definition of your merchants table? |
![]() |
| Thread Tools | |
| Display Modes | |
| |