我有以下查询,它工作正常:
SELECT
a.i_am,a.seeking_a,
a.zodiac,
a.my_marital_status,
a.city,a.country,
a.my_age,
a.intrested_in1,
a.intrested_in2,
a.intrested_in3,
a.intrested_in4,
a.intrested_in5,
a.intrested_in6,
a.user_id,
a.picture_type,
a.photo,
a.block_photo,
r.username,
u.title,
u.about_myself
FROM ".TBL_ABOUT_MYSELF." a
INNER JOIN ".TBLREGISTRATION." r ON a.user_id=r.ID
INNER JOIN ".TBLUSERDETAILS." u ON a.user_id=u.user_id
LEFT JOIN ".TBLSTEPS." s ON a.user_id=s.user_id
WHERE 1 $condition $gender_condition
ORDER BY a.user_id
DESC LIMIT $from,10
获取结果大约需要0.5 秒。总共有 1,000,000 个结果。
现在,如果我再加入一张表(对于用户在线状态:
SELECT
a.i_am,a.seeking_a,
a.zodiac,
a.my_marital_status,
a.city,a.country,
a.my_age,
a.intrested_in1,
a.intrested_in2,
a.intrested_in3,
a.intrested_in4,
a.intrested_in5,
a.intrested_in6,
a.user_id,
a.picture_type,
a.photo,
a.block_photo,
r.username,
u.title,
u.about_myself
FROM ".TBL_ABOUT_MYSELF." a
INNER JOIN ".TBLREGISTRATION." r ON a.user_id=r.ID
INNER JOIN ".TBLUSERDETAILS." u ON a.user_id=u.user_id
LEFT JOIN ".TBLSTEPS." s ON a.user_id=s.user_id
LEFT JOIN ".TBL_USER_ONLINE." ON a.user_id=o.user_id <-- New Line
WHERE 1 $condition $gender_condition
ORDER BY o.user_id DESC, a.user_id DESC LIMIT $from,10 <-- One more order by
现在获取结果需要12 秒。
我该如何优化呢?
mysql查询解释:
SQL result
Host: localhost
Database: mysite_new
Generation Time: Dec 06, 2012 at 09:09 AM
Generated by: phpMyAdmin 3.2.0.1 / MySQL 5.1.36-community-log
SQL query: EXPLAIN SELECT a.i_am,a.seeking_a,a.zodiac,a.my_marital_status,a.city,a.country,a.my_age,a.intrested_in1,a.intrested_in2,a.intrested_in3,a.intrested_in4,a.intrested_in5,a.intrested_in6,a.user_id,a.picture_type,a.photo,a.block_photo,r.username,u.title, u.about_myself ,o.user_id as online_user_id FROM mysite_about_myself_new a INNER JOIN mysite_user_registration_new r ON a.user_id=r.ID INNER JOIN mysite_user_details_new u ON a.user_id=u.user_id LEFT JOIN mysite_steps_new s ON a.user_id=s.user_id LEFT JOIN mysite_usersonline_new o ON a.user_id=o.user_id WHERE 1 and r.block_by_webmaster='0' and a.approved='1' and s.step1='1' AND a.i_am ='2' AND a.seeking_a = '1' ORDER BY o.user_id DESC LIMIT 0,10;
Rows: 5
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a ref user_id,i_am i_am 2 const 30011 Using where; Using temporary; Using filesort
1 SIMPLE o ref user_id user_id 4 mysite_new.a.user_id 2 Using index
1 SIMPLE s eq_ref user_id user_id 4 mysite_new.a.user_id 1 Using where
1 SIMPLE u eq_ref user_id user_id 4 mysite_new.s.user_id 1 Using where
1 SIMPLE r eq_ref PRIMARY PRIMARY 4 mysite_new.u.user_id 1 Using where
第二次按要求解释:
SQL result
Host: localhost
Database: mysite_new
Generation Time: Dec 06, 2012 at 10:02 AM
Generated by: phpMyAdmin 3.2.0.1 / MySQL 5.1.36-community-log
SQL query: EXPLAIN SELECT a.i_am,a.seeking_a,a.zodiac,a.my_marital_status,a.city,a.country,a.my_age,a.intrested_in1,a.intrested_in2,a.intrested_in3,a.intrested_in4,a.intrested_in5,a.intrested_in6,a.user_id,a.picture_type,a.photo,a.block_photo,r.username,u.title, u.about_myself ,o.user_id as online_user_id FROM mysite_about_myself_new a INNER JOIN mysite_user_registration_new r ON a.user_id=r.ID INNER JOIN mysite_user_details_new u ON a.user_id=u.user_id LEFT JOIN mysite_steps_new s ON a.user_id=s.user_id LEFT JOIN mysite_usersonline_new o ON a.user_id=o.user_id WHERE 1 and r.block_by_webmaster='0' and a.approved='1' and s.step1='1' AND a.i_am ='2' AND a.seeking_a = '1' ORDER BY a.user_id DESC LIMIT 0,10;
Rows: 5
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a index user_id,i_am user_id 4 NULL 38 Using where
1 SIMPLE o ref user_id user_id 4 mysite_new.a.user_id 2 Using index
1 SIMPLE s eq_ref user_id user_id 4 mysite_new.a.user_id 1 Using where
1 SIMPLE u eq_ref user_id user_id 4 mysite_new.s.user_id 1 Using where
1 SIMPLE r eq_ref PRIMARY PRIMARY 4 mysite_new.u.user_id 1 Using where