简体   繁体   中英

mysql select two table and use “or” getting very slow

I have three table in database

1. user_phone
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

I used phone_a to store the cell phone number, and phone_b to the phone number at home.
when user input a phone number, I want to search this number in both two tables.
Here is my query:

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")

but this query is very slow, I dont know why.
user_phone and phone_a have 60000 rows, while phone_b just 600.
I think this number is small to MySql, so I dont know why the query is slow.
I also know that use JOIN will maybe fasten the query, but I want to know if there is a way do not use JOIN, and WHY this query is so slow?

Try this one, not tested but easily to debug, just tell me the errors

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM