Elasticsearch實戰(zhàn)—ES數(shù)據(jù)建模與Mysql對比實現(xiàn) 一對一模型
我們?nèi)绾伟袽ysql的模型合理的在ES中去實現(xiàn)? 就需要你對要存儲的數(shù)據(jù)足夠的了解,及對應(yīng)用場景足夠的深入分析,才能建立一個合適的模型,便于你后期擴展
實體之間的關(guān)系:
-
一對一 模型
一對一(1:1):一個實體最多只能能另一個實體相關(guān)聯(lián),另一個實體如是。
例:一個只能有一張身份證。 -
一對多 模型
一對多(1:n): 一個實體可以和別的實體任意關(guān)聯(lián)(他們只能和前面的實體關(guān)聯(lián))
例:一個班有多名學(xué)生。 -
多對多 模型
多對多:(n:n):一個實體可以和別的實體任意關(guān)聯(lián)(他們也可以和前面的實體任意關(guān)聯(lián))
例:一個學(xué)生可以選擇多門課程,一門課程對應(yīng)多名學(xué)生。
1.一對一 模型
我們現(xiàn)在有兩個模型, 一個商品Product, 一個屬性 Attribute , 我們對比下一對一模型如何處理 ,一個商品有一個屬性
1.1 Mysql建模
對于一對一的數(shù)據(jù)模型, mysql 可以用2個表 來在Mysql中實現(xiàn) 一對一, 查 produce_id的同時, 再根據(jù) attribute_id屬性id去 關(guān)聯(lián) 查屬性表查出商品和商品屬性的關(guān)系
#關(guān)聯(lián)查詢
select * from product left join addribute on product.product_id = attribute.product_id;
Table :produce 商品表 基本信息
字段 | 類型 |
---|---|
id | 唯一主鍵,自增 |
product_id | 商品id |
attribute_id | 屬性id |
product_name | 商品名稱 |
product_price | 商品價格 |
Table :attribute屬性表
字段 | 類型 |
---|---|
id | 唯一主鍵,自增 |
attribute_id | 屬性id |
product_id | 商品id |
product_number | 商品數(shù)量 |
product_address | 商品產(chǎn)地 |
product_remark | 商品標簽 |
produce 商品id 字段product_id 的關(guān)聯(lián)屬性id 字段attribute_id 輕易的獲取兩個表的對應(yīng)關(guān)系
既可以通過 produce 商品id 字段 獲取屬性表 attribute_id 的屬性信息
也可以通過 屬性表 attribute_id 查出擁有這些屬性的所有 的商品product_id
這樣我們可以 在Mysql中輕易的實現(xiàn) 兩個表結(jié)構(gòu)的關(guān)聯(lián) 實現(xiàn)1:1的商品及屬性模型
1.2 Index ES 模型
對于ES 這種1:1的 我們應(yīng)該如何查詢, 正常來說 不會用兩個 索引庫去存儲兩份數(shù)據(jù), 我們一般都是采用大寬表去實現(xiàn)這種邏輯,我們會把商品及商品的屬性 公用一個商品Index庫, 通過一個id去關(guān)聯(lián)該商品所有的唯一性信息
- 去除掉 attribute_id的 表信息
- 把屬性表 所有的這些屬性 全都和商品 product_id綁定在一起
- 多字段形成大寬表 去實現(xiàn)1:1 ES模型設(shè)計
為什么要這樣設(shè)計?
- 方便查詢
- 沒有任何冗余數(shù)據(jù)
- 操作簡單, 一次性就能搜索出所有的結(jié)果字段
Table :produce 商品表 基本信息
字段 | 類型 |
---|---|
id | 唯一主鍵,自增 |
product_id | 商品id |
product_name | 商品名稱 |
product_price | 商品價格 |
product_number | 商品數(shù)量 |
product_address | 商品產(chǎn)地 |
product_remark | 商品標簽 |
xxxx_xxx | 商品xxx等 |
下面創(chuàng)建Index mapping結(jié)構(gòu), 大寬表
{
"product":{
"mappings":{
"_doc":{
"dynamic":"strict",
"_all":{
"enabled":false
},
"properties":{
"productId":{
"type":"keyword"
},
"productName":{
"type":"text",
"analyzer":"ik_max_word"
},
"productNumber":{
"type":"integer"
},
"productAddress":{
"type":"integer"
},
"productRemark":{
"type":"integer"
},
"xxx":{
"type":"keyword"
}
}
}
}
}
}
本文章 分析了 1:1 模型 一個商品Product, 一個屬性 Attribute ,對比了mysql如何處理及ES如何設(shè)置mappings結(jié)構(gòu)文章來源:http://www.zghlxwxcb.cn/news/detail-498696.html
下一篇 我們著重講解下 Mysql的一對多 1:n 模型及 ES的一對多模型如何建立文章來源地址http://www.zghlxwxcb.cn/news/detail-498696.html
到了這里,關(guān)于Elasticsearch實戰(zhàn)(二十二)---ES數(shù)據(jù)建模與Mysql對比 一對一模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!