优化SQL查询(类似Facebook的应用程序)

时间:2012-07-02 02:27:39

标签: mysql optimization

我的应用程序类似于Facebook,我正在尝试优化获取用户记录的查询。用户记录是他作为src ou dst。 src直接位于usermuralentrydst列表位于usermuralentry_user

因此,条目可以包含一个src和多个dst

我有那些表:

mysql> desc usermuralentry ;
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| id              | int(11)          | NO   | PRI | NULL    | auto_increment |
| user_src_id     | int(11)          | NO   | MUL | NULL    |                |
| private         | tinyint(1)       | NO   |     | NULL    |                |
| content         | longtext         | NO   |     | NULL    |                |
| date            | datetime         | NO   |     | NULL    |                |
| last_update     | datetime         | NO   |     | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+
10 rows in set (0.10 sec)


mysql> desc usermuralentry_user ;
+-------------------+---------+------+-----+---------+----------------+
| Field             | Type    | Null | Key | Default | Extra          |
+-------------------+---------+------+-----+---------+----------------+
| id                | int(11) | NO   | PRI | NULL    | auto_increment |
| usermuralentry_id | int(11) | NO   | MUL | NULL    |                |
| userinfo_id       | int(11) | NO   | MUL | NULL    |                |
+-------------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

以下查询从两个用户检索信息。

mysql> explain

SELECT *
FROM usermuralentry AS a
   , usermuralentry_user AS b
WHERE a.user_src_id IN ( 1, 2 )
   OR
   (
      a.id = b.usermuralentry_id
         AND b.userinfo_id IN ( 1, 2 )
   );
+----+-------------+-------+------+-------------------------------------------------------------------------------------------+------+---------+------+---------+------------------------------------------------+
| id | select_type | table | type | possible_keys                                                                             | key  | key_len | ref  | rows    | Extra                                          |
+----+-------------+-------+------+-------------------------------------------------------------------------------------------+------+---------+------+---------+------------------------------------------------+
|  1 | SIMPLE      | b     | ALL  | usermuralentry_id,usermuralentry_user_bcd7114e,usermuralentry_user_6b192ca7 | NULL | NULL    | NULL |  147188 |                                                |
|  1 | SIMPLE      | a     | ALL  | PRIMARY                                                                                   | NULL | NULL    | NULL | 1371289 | Range checked for each record (index map: 0x1) |
+----+-------------+-------+------+-------------------------------------------------------------------------------------------+------+---------+------+---------+------------------------------------------------+
2 rows in set (0.00 sec)

但这需要很多时间......

优化的一些提示?表模式可以在我的应用程序中更好吗?

2 个答案:

答案 0 :(得分:1)

我认为你的连接没有正确形成,你需要更改查询以使用UNION。 where子句中的OR条件也是杀死性能:

SELECT *
FROM usermuralentry AS a
 JOIN usermuralentry_user AS b ON a.id = b.usermuralentry_id /* use explicit JOIN! */
WHERE a.user_src_id IN (1 , 2)
 UNION
SELECT *
FROM usermuralentry AS a
 JOIN usermuralentry_user AS b ON a.id = b.usermuralentry_id 
WHERE b.usermuralentry_id IN ( 1, 2 )   

您还需要一个索引:ALTER TABLE usermuralentry_user ADD INDEX(usermuralentry_id)

答案 1 :(得分:1)

使用此查询

SELECT *
FROM usermuralentry AS a
left join usermuralentry_user AS b
on b.usermuralentry_id = a.id
WHERE a.user_src_id IN(1, 2)
OR (a.id = b.usermuralentry_id
AND b.userinfo_id IN(1, 2));

这里有一些提示是 您正在使用from子句中的两个表,这是一个产品,会花费大量时间和不良结果。在这种情况下总是使用连接。