1

my tables are simple:

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    |                |
+-----------------+------------------+------+-----+---------+----------------+

Im doing the following query:

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

The explain:

+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+------+-----------------+
| 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 |                 |
+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+------+-----------------+

Good result (for me :D) But, when i add another where clause:

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;

The explain:

+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+---------+-----------------+
| 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     |
+----+-------------+----------------------------+------+-------------------------------------------------------+-------------------------------------+---------+------------------------------------------+---------+-----------------+

Wow... the result if ALOT worst... How can i "fix" this? Should i create some index to do this job? Or recreate my query?

I'm expecting the following result: 'muralentry' rows where the user is 'user_src_id' AND the 'muralentry_user' rows where he is 'userinfo_id'.

-- edit --

I edited the question because when I wrote an AND actually wanted an OR... sorry for that!

4

0 回答 0