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 件の結果があります。

ここで、もう1つのテーブルに参加すると(ユーザーのオンラインステータスの場合:

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

2 番目の質問に応じて説明する:

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インデックスを使用して並べ替えることができましたが、2番目のクエリでは、filesort2つのテーブルの列を並べ替えるために100万レコードを実行する必要があります(その後、結果セットのある時点でLIMIT句を実行します)。

で並べ替えるo.user_idだけで十分です。これは、にNULL等しいか等しいためa.user_idです。

o.user_idまた、代わりに、句で不等式をオンにしてクエリを制限できたWHERE場合は、少なくともここまでシークする必要はありません。

于 2012-12-06T08:52:56.963 に答える
0

2つの列でorderbyを使用する理由

ORDER BY o.user_id DESC, a.user_id DESC

o.user_idとa.user_idは、1人のユーザーに対して同じ値になるので、1つだけ使用できると思います。

o.user_idテーブルTBL_USER_ONLINEからのnullを順番に含めたい場合a.user_idユーザーIDのみで注文したい場合。

また、EXPLAINを使用して、結合に必要なインデックスを作成します。ここで、条件と列による順序

于 2012-12-06T08:54:08.797 に答える
0

この新しい行を

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

編集:あなたの問題はORDER BYにあります。あなたのクエリは、2 つのテーブルのうちの 2 つを ORDER BY するのに長い時間がかかります。1 つの テーブルから 1 つuser_idだけを使用してみてくださいuser_id

EDIT2:

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 に答える