我的查询需要很长时间才能执行

时间:2014-08-22 15:05:10

标签: php mysql

我在Dev_phones中有以下查询...我在Dev_users 50 000中有90 000项目

select `u`.`id` from `Dev_users` as `u`
left join `Dev_user_usergroup_map` as `m` on `u`.`id` = `m`.`user_id` 
left join `Dev_phones` as `p` on `m`.`id` = `p`.`acc_id` and `primary`=1 
where  `u`.`email`='sample@gmail.com' or `u`.`private_number`='sample@gmail.com'  
or  `m`.`id`='sample@gmail.com' or `p`.`number`='sample@gmail.com' limit 1

查询进入循环而不是结束... 什么是问题,我怎样才能更好地编写这个查询

1 个答案:

答案 0 :(得分:0)

仅供参考,这个查询可以按如下方式重写 - 尽管我发现在几个表中使用OUTER JOIN与OR(或IN)这样非常混乱...

SELECT u.id 
  FROM Dev_users u
  LEFT
  JOIN Dev_user_usergroup_map m 
    ON u.id = m.user_id 
  LEFT 
  JOIN Dev_phones p 
    ON m.id = p.acc_id 
   AND `primary` = 1 
 WHERE 'sample@gmail.com' IN(u.email,u.private_number,m.id,p.number)
 LIMIT 1;