mindwin
12-10-2011, 01:24 AM
Hi All
Need Help to optimize this query, the result wanted is select the max data in union table tbl_inbox and tbl_outbox group by Remoteaddress....
and left join with tbl_member for getting name if remoteaddress is in member data.
uid is unique number in tbl_inbox and tbl_outbox
the query is succeed, but it takes 1 second, the data of tbl_inbox and tbl_outbox total is about 2600...
optional:
wondering if the result of this query include the total count of the remoteaddress data in union tbl_inbox and tbl_outbox, already do separate query for this counter
SELECT t . * , x.memberid, x.membername, DATE_FORMAT( t.indatetime, '%d-%c-%Y %H:%i:%s' ) AS indatetime
FROM (
SELECT max( uid ) AS uid, m.id AS memberid, CONCAT( m.firstname, CONCAT( ' ', m.middlename ) , CONCAT( ' ', m.lastname ) ) AS membername
FROM tbl_member m
RIGHT JOIN (
SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_inbox
UNION ALL SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_outbox
)u ON u.remoteaddress
IN (
m.mobilephonenumber1, m.mobilephonenumber2, m.mobilephonenumber3, m.mobilephonenumber4, m.fixphonenumber
)
WHERE DATE( u.indatetime )
BETWEEN '2001-12-10'
AND '2011-12-10'
GROUP BY remoteaddress
)x
JOIN (
SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_inbox
UNION ALL SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_outbox
)t ON x.uid = t.uid
ORDER BY t.uid DESC
explain says
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const table...
5 DERIVED tbl_inbox ALL NULL NULL NULL NULL 1183
6 UNION tbl_outbox ALL NULL NULL NULL NULL 1454
NULL UNION RESULT <union5,6> ALL NULL NULL NULL NULL NULL
2 DERIVED <derived3> ALL NULL NULL NULL NULL 2637 Using where; Using temporary; Using filesort
2 DERIVED m ALL NULL NULL NULL NULL 163
3 DERIVED tbl_inbox ALL NULL NULL NULL NULL 1183
4 UNION tbl_outbox ALL NULL NULL NULL NULL 1454
NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
any response really appreciated, and let me know if other detail is needed... thank you.
Need Help to optimize this query, the result wanted is select the max data in union table tbl_inbox and tbl_outbox group by Remoteaddress....
and left join with tbl_member for getting name if remoteaddress is in member data.
uid is unique number in tbl_inbox and tbl_outbox
the query is succeed, but it takes 1 second, the data of tbl_inbox and tbl_outbox total is about 2600...
optional:
wondering if the result of this query include the total count of the remoteaddress data in union tbl_inbox and tbl_outbox, already do separate query for this counter
SELECT t . * , x.memberid, x.membername, DATE_FORMAT( t.indatetime, '%d-%c-%Y %H:%i:%s' ) AS indatetime
FROM (
SELECT max( uid ) AS uid, m.id AS memberid, CONCAT( m.firstname, CONCAT( ' ', m.middlename ) , CONCAT( ' ', m.lastname ) ) AS membername
FROM tbl_member m
RIGHT JOIN (
SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_inbox
UNION ALL SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_outbox
)u ON u.remoteaddress
IN (
m.mobilephonenumber1, m.mobilephonenumber2, m.mobilephonenumber3, m.mobilephonenumber4, m.fixphonenumber
)
WHERE DATE( u.indatetime )
BETWEEN '2001-12-10'
AND '2011-12-10'
GROUP BY remoteaddress
)x
JOIN (
SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_inbox
UNION ALL SELECT uid, indatetime, remoteaddress, text,
STATUS FROM tbl_outbox
)t ON x.uid = t.uid
ORDER BY t.uid DESC
explain says
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const table...
5 DERIVED tbl_inbox ALL NULL NULL NULL NULL 1183
6 UNION tbl_outbox ALL NULL NULL NULL NULL 1454
NULL UNION RESULT <union5,6> ALL NULL NULL NULL NULL NULL
2 DERIVED <derived3> ALL NULL NULL NULL NULL 2637 Using where; Using temporary; Using filesort
2 DERIVED m ALL NULL NULL NULL NULL 163
3 DERIVED tbl_inbox ALL NULL NULL NULL NULL 1183
4 UNION tbl_outbox ALL NULL NULL NULL NULL 1454
NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
any response really appreciated, and let me know if other detail is needed... thank you.