單表查詢的演變

單表查詢是互聯(lián)網(wǎng)業(yè)務(wù)中常用的手段.引入單表查詢后,數(shù)據(jù)庫(kù)負(fù)責(zé)簡(jiǎn)單讀取數(shù)據(jù),應(yīng)用需要解決如何將查詢落到核心表上!

現(xiàn)有三張表:
品牌表t_brand(brand_name,brand_code),
類目表t_category(cat_name,cat_code),
產(chǎn)品表為t_product(product_name,product_code,brand_code,cat_code)
需要實(shí)現(xiàn)接口需求為:支持通過(guò) 品牌名稱,類目名稱和產(chǎn)品編號(hào)進(jìn)行查詢產(chǎn)品。

按照傳統(tǒng)的解決方式:

select p.product_name,product_code from t_product p
join t_brand b on p.brand_code=b.brand_code
join t_catgory c on p.cat_code = c.cat_code
where b. brand_name like '%${品牌名稱}%'
and  c.cat_name like '%${類目名稱}%'
and  p.product_code = '${產(chǎn)品編號(hào)}'

這種方式的優(yōu)點(diǎn);開(kāi)發(fā)效率高,迅速支撐業(yè)務(wù)。但是隨著業(yè)務(wù)的發(fā)展,數(shù)據(jù)庫(kù)的數(shù)據(jù)量增加大,join的代價(jià)越來(lái)越大,數(shù)據(jù)庫(kù)的緩存使用率底下。為了解決這些問(wèn)題,通常引入單表查詢的方式來(lái)解決問(wèn)題.

sql變成了:

select brand_code from  t_brand where brand_name like '%${品牌名稱}%';
select cat_code  from  t_brand where cat_name like '%${類目名稱}%';
select product_name,product_code from t_product
where brand_code in ('${brandCode}')
and cat_code in ('${catCode}')
and product_code = '${產(chǎn)品編號(hào)}';

業(yè)務(wù)是不斷發(fā)展的,為了解決產(chǎn)品表的單維度的查詢,決定引入tag表,幫助運(yùn)營(yíng)進(jìn)行多維度的檢索.tag表t_tag(tag_code,tag_name),t_tag_product(id,tag_code,product_code)

需要實(shí)現(xiàn)接口需求為:支持通過(guò)品牌名稱,類目名稱,產(chǎn)品編號(hào)和標(biāo)簽編號(hào) 進(jìn)行查詢產(chǎn)品信息

sql又變成:

select brand_code from  t_brand where brand_name like '%${品牌名稱}%';
select cat_code  from  t_brand where cat_name like '%${類目名稱}%';
select product_code  from  t_tag_product where tag_code = '${標(biāo)簽編號(hào)}';
select product_name,product_code from t_product where brand_code in ('${brandCode}') and cat_code in ('${catCode}') and product_code = '${產(chǎn)品編號(hào)}'  and product_code in('${通過(guò)tagCode找到productCode}')

但是sql查詢時(shí)時(shí)有問(wèn)題,比如:
 1.如果產(chǎn)品編號(hào)與${通過(guò)tagCode找到productCode} 沒(méi)有交集,就不用浪費(fèi)一次sql查詢,直接在應(yīng)用層攔截返回。
 2.如果再增加一張新表sku(sku_code,product_code),同時(shí)更新接口的需求為:
支持通過(guò)品牌名稱,類目名稱,產(chǎn)品編號(hào),標(biāo)簽編號(hào),sku編號(hào)查詢產(chǎn)品信息。其sql也會(huì)越來(lái)越復(fù)雜。

select product_name,product_code from t_product
where brand_code in ('${brandCode}') 
and cat_code in ('${catCode}') 
and product_code = '${產(chǎn)品編號(hào)}'  
and product_code in('${通過(guò)tagCode找到productCode}') 
and product_code in('${通過(guò)skuCode找到productCode}')

很明顯,需要一個(gè)在應(yīng)用層對(duì)productCode進(jìn)行合并,并過(guò)濾無(wú)效的productCode的工具。
這樣的工具,需要解決幾個(gè)問(wèn)題:
 1.將t_product設(shè)為主表,因?yàn)樽罱K接口返回的產(chǎn)品信息,其他表設(shè)置為輔助表,用于輔助查詢。
 2.判斷用戶傳遞的輔助字段,這些條件能否查詢到productCode,并將合并為supportProductCodes。
 3.處理masterProductCode與supportProductCodes的關(guān)系,判斷什么時(shí)候應(yīng)該攔截sql,什么時(shí)候應(yīng)該合并。

有了這個(gè)工具后,sql再次得到簡(jiǎn)化

select brand_code from  t_brand where brand_name like '%xx%';
select cat_code  from  t_brand where cat_name like '%xx%';
select product_code  from  t_tag_product where tag_code = 'power';
select product_code  from t_sku where sku_code = '{sku編號(hào)}'

select product_name,product_code from t_product where brand_code in ('brandcode') and cat_code in ('cat_code') and and product_code in('{合并后的productCode}')
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容