0

我的应用程序类似于 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)

但这需要很多时间...

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

4

2 回答 2

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)

于 2012-07-02T05:29:26.237 回答
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 子句中使用了两个表,这是一个 cartision 产品,将花费大量时间以及不希望的结果。在这种情况下始终使用连接。

于 2012-07-02T05:34:42.953 回答