sqlercn老師,請問,一張大表水平拆分過后,如果需要查詢這張大表的數(shù)據(jù)某一條,并且不知道被查詢記錄的id值,也就是不知道具體在哪張小表中,那么,我們應(yīng)該怎么查詢呢?應(yīng)該join所有小表來一起查詢嗎?
比如:大表:
select id,name from big_table where phone=123456
小表:
select id,name
from small_table_1 as a
left join small_table_2 as b
left join small_table_3 as c
left join small_table_4 as d
where a.phone=123456 or b.phone=123456 or c.phone=123456 or d.phone=123456
或者
select id,name
from small_table_1
where phone=123456
union
select id,name
from small_table_2
where phone=123456
union
select id,name
from small_table_3
where phone=123456
union
select id,name
from small_table_4
where phone=123456
以上的兩種在方式適合使用在水平拆分的表中進行查詢嗎?如果不適合,該怎樣查詢呢?怎樣的查詢效率相對更高呢?
2014-08-29
這種情況,如果不知道分區(qū)鍵的話就只能在各個分區(qū)內(nèi)掃描了。就給出的兩種方法,個人認為第二種要比第一種效率高些。在水平分表設(shè)計時最好不要使用沒有意義的id值做為分區(qū)鍵,而是選擇在業(yè)務(wù)中有意義的物理主鍵進行拆分比較適合。
2015-06-24
如果有100表呢?sql語句是不是很可怕 @sqlercn