0

我有以下查询,它工作正常:

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
4

3 回答 3

0

问题很可能出在排序上。而在第一个查询中,MySQL 可以使用a.user_id索引进行排序,而在第二个查询中,它必须执行filesort一百万条记录中的一条,以便对两个表的列进行排序(然后在结果集中查找某个点以影响该LIMIT子句)。

排序依据o.user_id应该是足够的,因为它是NULL或等于a.user_id

o.user_id此外,如果您能够在子句中使用不等式来限制查询WHERE,那么您至少不需要搜索那么远。

于 2012-12-06T08:52:56.963 回答
0

将此新行更改为

    LEFT JOIN ".TBL_USER_ONLINE." o  on ON a.user_id=o.user_id

编辑:那么您的问题出在 ORDER BY 上。您的查询需要很长时间才能user_id按两张表中的两张进行排序,请尝试仅使用user_id 一张表中的一张

编辑2:

o.user_id也将它添加到这样的SELECT部分

 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                 <-- New Line
于 2012-12-06T08:41:29.807 回答
0

why you use order by on two columns

ORDER BY o.user_id DESC, a.user_id DESC

I think o.user_id and a.user_id will be same values for a single user so you can just use one.

o.user_id if you want to include null from tables TBL_USER_ONLINE in order a.user_id if you want to order only on user ids.

Also use EXPLAIN and create any necessary indexes for joins, where conditions and order by columns

于 2012-12-06T08:54:08.797 回答