为什么mysql查询通过“IN”从两个表中选择是慢查询

时间:2015-10-29 08:57:06

标签: php mysql

我有两个表包含这样的客户ID:

(SELECT * FROM customers WHERE cellular = '$cellular' OR  id  IN(SELECT customer FROM clients WHERE number=$cellular  ) )

当我搜索客户蜂窝客户端或蜂窝时,这个mysql查询需要30秒。 我怎么能更快?

1 个答案:

答案 0 :(得分:0)

尝试MySQL join

SELECT * FROM customers left join clients 
on customers.id=clients.customer 
where clients.number=$cellular or customers.cellular = '$cellular'

或尝试此查询: -

SELECT * FROM customers inner join clients 
on customers.id=clients.customer 
where clients.number=$cellular or customers.cellular = '$cellular'