Upload
dianmingsong
View
1.077
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
NO sql
NO relational
NO joins
NO ORM
NO complex transactions
海量数据 高并发 高可用 复杂度 非结构化数据 需求变化
Data Model
1,2,3范式
tree、master/detail、relation…
Objects != Data
ORM
DAL
Key-value
Memcached、Redis
Graph Databases
Neo4j
Column Families
BigTable,HBase,cassandra
Document
CouchDB,mongoDB
酸 ACID
atomicity, consistency, isolation, durability
碱 BASE
Basically Available, Soft-state, Eventually Consistent
Consistency (一致性)
Availability (可用性)
Partition tolerance (分区容忍性)
一个分布式系统不可能同时满足这三个需求最多只能同时满足两个(AP/CP)
Memory is the new disk,disk is the new tap-Jim Gray
function map(fn, a){
for (i = 0; i < a.length; i++){
a[i] = fn(a[i]);
} }
(Joel on Software)
function reduce(fn, a, init){ var s = init; for (i = 0; i < a.length; i++)
s = fn( s, a[i] ); return s;
}
NoSQL Databases: Why, what and when NoSQL生态系统
http://www.mongodb.org/
Document-oriented Rich, document-based queries Full Index Support Replication & High Availability Sharding GridFS Map/Reduce BSON everywhere Memory Mapped Data Files
http://try.mongodb.org/
mongod
mongodb.conf
mongo mongos mongostat mongoexport\mongoimport mongodump\mongorestore
默认数据存储路径是 /data/db, 默认端口 27017 默认 HTTP 端口 28017 日志
--logpath、--logappend、--verbose、--cpu
一个.ns文件和n个数据文件 数据文件大小2倍倍增 单数据文件最大2G
显示创建 不能删除对象
每个BSON对象大小不能超过4MB 字段名不能以”$”开头;不能包含”.” “_id”是系统保留的字段
> help
4e2647e8 0fe07f 1260 54caf4
Timestamp + Machineid + Processid + Counter
一个对象被插入到数据库中时,如果它没有ID,会自动生成一个”_id”字段,为24位16进制数。
<, <=, >, >= $all $exists $mod $ne $in $nin $nor $or $and $size $type
Regular Expressions Embedded Object Search JavaScript Expressions Special operators Explain()
Count db.characters.find({armor:{$gt:1800}}).count()
Limit Skip () db.characters.find({armor:{$gt:1800}}).skip(10).limit(1)
Sort db.characters.find().sort({armor:-1}).limit(3)
db.characters.find().sort({$natural:-1}).
B Tree,支持geospatial index 影响写速度,占用空间 默认索引:_id 索引顺序影响是否用到索引 索引中缺少的字段为null explain()查看执行计划
索引保存在 system.indexes 索引大小 db. characters.totalIndexSize()
创建索引 db.characters.ensureIndex(key_pattern, options)
选项: unique; dropDups ; background
查看索引 db.characters.coll.getIndexes()
删除索引 db.characters.dropIndexes()
重建索引 db.characters.reIndex()
Udate(criteria, objNew, upsert, multi) Save() 支持原子操作tomic 不支持传统事务模型(rollback??)
findAndModify (<options> } )
find, modify, and return
db.colname’.remove({…})
db.eval(…);
http://localhost:28017/_commands db.runCommand( {
<commandname>: <value> [, options] } );
db.commandHelp("datasize")
Http Console
http://localhost:28017/
mongostat db.serverStatus() db.stats()、db.colname.stats() db.colname.validate() 系统监控
memory、cpu、IO
setProfilingLevel(lvl, <ms>)
0: none
1: time-based(默认100ms)
2: all
db.system.profile.find()
mongod --repair db.repairDatabase(); mongod --journal
version 1.9.2+64-bit默认开启
group commit
db.colname.validate();
Master Master/slave
arbiterOnly
Replica sets
db.printReplicationInfo()
db.printSalveReplicationInfo()
oplog(capped collection,规划大小)
Params
w: number of replicas to write to
wtimeout: time to wait for acknowledgements
fsync: flush to disk
Unique Index 空间成本 维护 failover
Single One – Many & Many - Many
Embedded document(4M的限制)
Normalized
Tree
Javascript执行性能
db.addUser
("username", "password")
db.system.users.remove
({user: username})
system.namespaces system.indexes system.profile system.users local.sources
--rest选项
备份
mongodump、mongorestore
db.copyDatabase、db.cloneDatabase
Lock, Snapshot and Unlock
admin.runCommand({fsync : 1, lock : 1})
db.$cmd.sys.unlock.findOne()
缺少传统数据库丰富的还原机制 队列
执行计划 db.colname.find({..}).expliain()
减小json数据的大小(使用较短的key) 优化索引、索引全部放入内存 优化document schema设计 db.eval() 在服务端执行某些操作 用BinData存储UUID格式的数据 36bytes->16bytes
使用连接池,修改每个连接的stack size 默认10M
预分配空间、减少内存和磁盘碎片 删除数据--不收回碎片
NoSQL注入 --auth 启动参数 --noscripting启动参数
每个链接一个线程 单线程执行Map/Reduce 一个写线程
磁盘空间 内存 维护 事务
mongo-csharp-driver (Github) CSharp Driver Tutorial
MongoDB Deployment Strategies MongoDB开发应用实践 Optimizing MongoDB: Lessons Learned at
Localytics Chrome扩展Mongo Live MongoDB部署与运维 10 Key MongoDB Performance Indicators MongoDB Administration
Thanks文中大部分资料来源于网络