MYSQL无法在不同的表上加载OR运算符

时间:2018-01-23 16:32:47

标签: php mysql query-performance

我认为我的查询没有任何问题,但是当我在phpmyadmin中加载此代码时,不知道为什么系统变得非常慢。有任何建议请帮忙。

select
  *
from
  `table1`
left join
   `table2`
on
  table1.id = table2.id
where
(
   (table1.email = 'test@test.com' and table1.mobile = '99999999')
 or
   (table2.email = 'test@test.com' and table2.mobile = '99999999')
) 

提前致谢。

2 个答案:

答案 0 :(得分:0)

Add this to each table:

INDEX(email, mobile)  -- in either order.

If that does not speed it up enough, then also turn the OR into a UNION:

SELECT *
     FROM ( ( SELECT id FROM table1 WHERE email = ... AND mobile = ... )
              UNION DISTINCT
            ( SELECT id FROM table2 WHERE email = ... AND mobile = ... )
          ) AS x
    JOIN table1 WHERE table1.id = x.id
    JOIN table2 WHERE table2.id = x.id

答案 1 :(得分:0)

添加以下索引:

ALTER TABLE `table1` ADD INDEX `table1_idx_id` (`id`);
ALTER TABLE `table2` ADD INDEX `table2_idx_id` (`id`);
ALTER TABLE `table1` ADD INDEX `table1_idx_email_mobile_id` (`email`, `mobile`, `id`);
ALTER TABLE `table2` ADD INDEX `table2_idx_email_mobile_id` (`email`, `mobile`, `id`);

转换后的查询(通过使用UNION DISTINCT避免OR条件,这是不可索引的):

(SELECT 
    *
FROM
    `table1`
        LEFT JOIN
    `table2` ON table1.id = table2.id
WHERE
    ((table2.email = 'test@test.com'
        AND table2.mobile = '99999999'))) UNION DISTINCT (SELECT 
    *
FROM
    `table1`
        LEFT JOIN
    `table2` ON table1.id = table2.id
WHERE
    ((table1.email = 'test@test.com'
        AND table1.mobile = '99999999')))

P.S,如果没有重复选项,请使用UNION ALL而不是UNION DISTINCT,因为它应该表现更好(避免重复消除,这可能需要时间)。