使用mySQL进行LEFT OUTER JOIN分析所有行

时间:2012-12-26 19:16:22

标签: mysql

我的表很简单:

mysql> desc muralentry ;
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| id              | int(11)          | NO   | PRI | NULL    | auto_increment |
| user_src_id     | int(11)          | NO   | MUL | NULL    |                |
| content         | longtext         | NO   |     | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+

mysql> desc muralentry_user ;
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| muralentry_id   | int(11)          | NO   | PRI | NULL    | auto_increment |
| userinfo_id     | int(11)          | NO   | MUL | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+

我正在执行以下查询:

SELECT DISTINCT *
FROM muralentry
LEFT OUTER JOIN muralentry_user ON (muralentry.id = muralentry_user.muralentry_id)
WHERE user_src_id = 1

解释:

+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+------+-----------------+
| id | select_type | table                      | type | possible_keys                                         | key                                 | key_len | ref                                      | rows | Extra           |
+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+------+-----------------+
|  1 | SIMPLE      | muralentry                 | ref  | muralentry_99bd10ae                                   | muralentry_99bd10ae                 | 4       | const                                    |  686 | Using temporary |
|  1 | SIMPLE      | muralentry_user            | ref  | muralentry_id,muralentry_user_bcd7114e                | muralentry_user_bcd7114e            | 4       | muralentry.id                            |   15 |                 |
+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+------+-----------------+

好结果(对我来说:D) 但是,当我添加另一个where子句时:

SELECT DISTINCT *
FROM muralentry
LEFT OUTER JOIN muralentry_user ON (muralentry.id = muralentry_user.muralentry_id)
WHERE user_src_id = 1 OR userinfo_id = 1;

解释:

+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+---------+-----------------+
| id | select_type | table                      | type | possible_keys                                         | key                                 | key_len | ref                                      | rows    | Extra           |
+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+---------+-----------------+
|  1 | SIMPLE      | muralentry                 | ALL  | muralentry_99bd10ae                                   | NULL                                | NULL    | NULL                                     | 1140932 | Using temporary |
|  1 | SIMPLE      | muralentry_user            | ref  | muralentry_id,muralentry_user_bcd7114e                | muralentry_user_bcd7114e            | 4       | muralentry.id                            |      15 | Using where     |
+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+---------+-----------------+
哇......结果如果ALOT最差...... 我怎样才能解决这个问题? 我应该创建一些索引来完成这项工作吗?或者重新创建我的查询?

我期待以下结果:'muralentry'行,用户是'user_src_id','muralentry_user'行是'userinfo_id'。

- 编辑 -

我编辑了这个问题,因为当我写一个AND实际上想要一个OR ...对不起!

0 个答案:

没有答案