Thursday, 12 September 2013

Index Not working well in mysql

Index Not working well in mysql

I have a a query like this :
SELECT SQL_NO_CACHE u.userid AS userid ,
u.userrname , pmdate , pmid , pmmessage , imagelink
FROM `privatemessagereceiver` JOIN privatemessage
ON pmrtouserid = '1' AND pmrpmid = pmid
JOIN user u ON u.userid = pmuserid
JOIN image i ON i.imageid = u.userprofileimageid
ORDER BY pmr_date DESC
explain :
SIMPLE privatemessagereceiver ref pmrpmid,pmrtouserid pmrtouserid 4
**const** 3116 Using where; Using filesort
IT workes well on index and it takes only 0.0006 sec to execute
but when I use that query like this :
SELECT SQL_NO_CACHE result.*
FROM (
SELECT u.userid AS userid ,
u.userrname , pmdate , pmid , pmmessage , imagelink
FROM `privatemessagereceiver` JOIN privatemessage
ON pmrtouserid = '1' AND pmrpmid = pmid
JOIN user u ON u.userid = pmuserid
JOIN image i ON i.imageid = u.userprofileimageid
ORDER BY pmr_date DESC
) as result
explain :
PRIMARY <derived2> ALL NULL NULL NULL NULL 3117
2 DERIVED privatemessagereceiver ALL pmrtouserid pmrtouserid 4
7054 Using where; Using filesort
it takes 1.1 sec to execute !
============================================================== It seem
that the 2nd query do not user index and as you can see const is not
defined . Also I used Use Index And Force Index , but not working ...
where is the problem and how can I solve ?

No comments:

Post a Comment