sqlercn老師,請(qǐng)問(wèn),一張大表水平拆分過(guò)后,如果需要查詢(xún)這張大表的數(shù)據(jù)某一條,并且不知道被查詢(xún)記錄的id值,也就是不知道具體在哪張小表中,那么,我們應(yīng)該怎么查詢(xún)呢?應(yīng)該join所有小表來(lái)一起查詢(xún)嗎?
比如:大表:
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
以上的兩種在方式適合使用在水平拆分的表中進(jìn)行查詢(xún)嗎?如果不適合,該怎樣查詢(xún)呢?怎樣的查詢(xún)效率相對(duì)更高呢?
2014-08-29
這種情況,如果不知道分區(qū)鍵的話(huà)就只能在各個(gè)分區(qū)內(nèi)掃描了。就給出的兩種方法,個(gè)人認(rèn)為第二種要比第一種效率高些。在水平分表設(shè)計(jì)時(shí)最好不要使用沒(méi)有意義的id值做為分區(qū)鍵,而是選擇在業(yè)務(wù)中有意義的物理主鍵進(jìn)行拆分比較適合。
2015-06-24
如果有100表呢?sql語(yǔ)句是不是很可怕 @sqlercn