MySQL key/value存储方案(转)
需求
250M entities, entities表共有2.5億條記錄,當(dāng)然是分庫(kù)的。
典型解決方案:RDBMS
問題:由于業(yè)務(wù)需要不定期更改表結(jié)構(gòu),但是在2.5億記錄的表上增刪字段、修改索引需要鎖表,最長(zhǎng)需要1小時(shí)到1天以上。
Key value方案
評(píng)估Document類型數(shù)據(jù)庫(kù),如CouchDB
CouchDB問題:?Performance? 廣泛使用? 穩(wěn)定性? 抗壓性?
MySQL方案
MySQL相比Document store優(yōu)點(diǎn):
- 不用擔(dān)心丟數(shù)據(jù)或數(shù)據(jù)損壞
- Replication
- 非常熟悉它的特性及不足,知道如何解決
結(jié)論
綜合取舍,使用MySQL來存儲(chǔ)key/value(schema-less)數(shù)據(jù),value中可以放:
Python dict
JSON object
實(shí)際friendfeed存放的是zlib壓縮的Python dict數(shù)據(jù),當(dāng)然這種綁定一種語言的做法具有爭(zhēng)議性。
表結(jié)構(gòu)及Index設(shè)計(jì)模式
feed數(shù)據(jù)基本上都存在entities表中,它的結(jié)構(gòu)為
mysql> desc entities; +----------+------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------+------+-----+-------------------+----------------+ | added_id | int(11) | NO | PRI | NULL | auto_increment | | id | binary(16) | NO | UNI | | | | updated | timestamp | YES | MUL | CURRENT_TIMESTAMP | | | body | mediumblob | YES | | NULL | | +----------+------------+------+-----+-------------------+----------------+?
假如里面存的數(shù)據(jù)如下
{ "id": "71f0c4d2291844cca2df6f486e96e37c", "user_id": "f48b0440ca0c4f66991c4d5f6a078eaf", "feed_id": "f48b0440ca0c4f66991c4d5f6a078eaf", "title": "We just launched a new backend system for FriendFeed!", "link": "http://friendfeed.com/e/71f0c4d2-2918-44cc-a2df-6f486e96e37c", "published": 1235697046, "updated": 1235697046, }?
如果要對(duì)link字段進(jìn)行索引,則用另外一個(gè)表來存儲(chǔ)。
mysql> desc index_link; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | link | varchar(255) | NO | PRI | | | | entity_id | binary(16) | NO | PRI | | | +-----------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)?
優(yōu)點(diǎn)是
- 增加索引時(shí)候只需要 1. CREATE TABLE,2.更新程序
- 刪除索引時(shí)候只需要 1. 程序停止寫索引表(實(shí)際就是一個(gè)普通表),2. DROP TABLE 索引表
這種索引方式也是一種值得借鑒的設(shè)計(jì)模式,特別是key value類型的數(shù)據(jù)需要索引其中的內(nèi)容時(shí)。
轉(zhuǎn)載于:https://www.cnblogs.com/ggjucheng/p/3349107.html
總結(jié)
以上是生活随笔為你收集整理的MySQL key/value存储方案(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#图片上绘制半透明矩形
- 下一篇: linux cmake编译源码,linu