我有一个连接3个表的查询(一个内连接,一个左连接)
我需要2个条件。
如果我自己使用任何一个条件,搜索很快。如果我同时使用两者,搜索将无法完成。
我不认为我可以直接将搜索子句添加到连接中,因为我不想排除该连接中不存在搜索词的结果。
SELECT job_entry.Job_Number,
job_entry.subject_ref,
contacts_library.company
FROM job_entry
LEFT JOIN multi_part_wind_instructions
ON job_entry.Job_Number = multi_part_wind_instructions.job_number
INNER JOIN contacts_library
ON job_entry.ContactID = contacts_library.ContactID
WHERE
company LIKE '%example%'
OR
multi_part_wind_instructions.address LIKE '%example%'
LIMIT 10
使用WHERE条件解释结果查询:
+----+-------------+------------------------------+--------+---------------+---------+---------+-----------------------------+-------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------------------+--------+---------------+---------+---------+-----------------------------+-------+----------------------------------------------------+
| 1 | SIMPLE | job_entry | ALL | NULL | NULL | NULL | NULL | 16234 | Using where |
| 1 | SIMPLE | contacts_library | eq_ref | PRIMARY | PRIMARY | 4 | euroims.job_entry.ContactID | 1 | Using index condition; Using where |
| 1 | SIMPLE | multi_part_wind_instructions | ALL | NULL | NULL | NULL | NULL | 39447 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+------------------------------+--------+---------------+---------+---------+-----------------------------+-------+----------------------------------------------------+
解释单个WHERE条件的结果(快速):
+----+-------------+------------------------------+--------+---------------+---------+---------+-----------------------------+-------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------------------+--------+---------------+---------+---------+-----------------------------+-------+----------------------------------------------------+
| 1 | SIMPLE | job_entry | ALL | NULL | NULL | NULL | NULL | 16234 | Using where |
| 1 | SIMPLE | contacts_library | eq_ref | PRIMARY | PRIMARY | 4 | euroims.job_entry.ContactID | 1 | Using index condition; Using where |
| 1 | SIMPLE | multi_part_wind_instructions | ALL | NULL | NULL | NULL | NULL | 39447 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+------------------------------+--------+---------------+---------+---------+-----------------------------+-------+----------------------------------------------------+