2014-11-05 74 views
0

我在数据库MySQL的选择两个表,并使用 “或” 变得很慢

1. USER_PHONE
用户手机
AAA P-12345
BBB P-56454

2三个表.phone_a
ID PHONE_NUMBER
p-12345 887157481
p-16546 841461655

3.phone_b
ID PHONE_NUMBER
P-54847 48446165
P-56461 64964661

我用phone_a存储手机号码,并phone_b在家里的电话号码。
当用户输入一个电话号码,我想在两个表中搜索这个号码。
这里是我的查询:

select A.user 
from 
    user_phone as A,phone_a as B,phone_b as C 
where 
    (A.phone=B.id and B.phone_number ="+9878848") or 
    (A.phone=C.id and C.phone_number ="+9878848") 

但此查询是很慢的,我不知道为什么。
user_phone和phone_a有60000行,而phone_b只有600.
我觉得这个数字对于MySql很小,所以我不知道为什么查询速度很慢。
我也知道使用JOIN可能会加快查询速度,但我想知道是否有一种方法不使用JOIN,并且为什么这个查询太慢?

+0

你加了'phone_number'指数? – Volatil3 2014-11-05 06:00:50

+0

http://dev.mysql.com/doc/refman/5.0/en/explain.html使用'Explain'来获取更多关于查询的信息。并做了正确的索引列也使用适当的数据类型? – gvgvgvijayan 2014-11-05 06:02:47

回答

1

试试这一个,没有测试但容易调试,只是告诉我的错误

select A.user 
from 
user_phone as A inner join phone_a as B on a.phone=b.id inner join phone_b as C on a.phone= c.id 
where 
"+9878848" in(B.phone_number, C.phone_number)