簡體   English   中英

mysql選擇兩個表並使用“或”變得很慢

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

我在數據庫中有三個表

1. user_phone
用戶電話
AAA P-12345
bbb p-56454

2.phone_a
編號phone_number
p-12345 +887157481
p-16546 +841461655

3.phone_b
編號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,為什么這個查詢這么慢?

試試這個,未經測試但很容易調試,只告訴我錯誤

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