简体   繁体   English

mysql选择两个表并使用“或”变得很慢

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

I have three table in database 我在数据库中有三个表

1. user_phone 1. user_phone
user phone 用户电话
aaa p-12345 AAA P-12345
bbb p-56454 bbb p-56454

2.phone_a 2.phone_a
id phone_number 编号phone_number
p-12345 +887157481 p-12345 +887157481
p-16546 +841461655 p-16546 +841461655

3.phone_b 3.phone_b
id phone_number 编号phone_number
p-54847 +48446165 p-54847 +48446165
p-56461 +64964661 p-56461 +64964661

I used phone_a to store the cell phone number, and phone_b to the phone number at home. 我使用phone_a来存储手机号码,并使用phone_b到家里的电话号码。
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. user_phone和phone_a有60000行,而phone_b只有600行。
I think this number is small to MySql, so I dont know why the query is slow. 我认为这个数字对MySql来说很小,所以我不知道为什么查询很慢。
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? 我也知道使用JOIN可能会固定查询,但是我想知道是否有一种方法不使用JOIN,为什么这个查询这么慢?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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