Upload
majid-azimi
View
646
Download
5
Embed Size (px)
DESCRIPTION
Use cases for different DBMSs
Citation preview
Which DBMS And Why?
سید مجید عظیمی@majidazimi
/پنجمین همایش سراسری نرم افزارهای آزاد متن باز
۱۳۹۳–زنجان شهریور
Theory: ACID
● Atomicity● Consistency● Isolation● Durability
Theory: CAP
Theory: BASE
● Basically Available● Soft State● Eventual Consistency
– Hopeful Consistency?
Read Only Data
!!!!استاد
. بهشون پیامک ارسال بشهنباید شماره تلفن دارم که میلیون۱۰
به مشتری همون لحظهوب سرویس باید در . هست لیست سیاهبگه که شماره ارسالی توی
Read Only Data
● Change rate is low over time● Mainly real time● Size matters alot● Can we use estimation strategies?
Redis● Key/Value● InMemory
– Persistent using snap shot● Lot's of data structures
– Set, Sorted Set, List, Hash● Sharding & Replication is simple
Redis: Set/* add elements */SADD blacklist 9352579085SADD blacklist 9143028953
/* search */SISMEMBER blacklist 9127984909
10 Million Numbers * 8 Byte = 80 MB
Redis: BitSet/* set bit */SETBIT blacklist 9127984909 1SETBIT blacklist 9143028953 1
/* is 9123024909 blacklisted? */GETBIT blacklist 9123024909
300 Million Numbers * 1 Bit = 37.5 MB
MySQL InMemory EngineCREATE TABLE `blacklist` (`phone_number` BIGINT NOT NULL;
) ENGINE = MEMORY;
SELECT * FROM `black_list`WHERE `phone_number` = ?
Use Old School SQL
RAMDISK: Make any DBMS InMemory
● Create partition on RAM● Mount partition ● Tell DBMS to store data theremkdir -p /datamount -t tmpfs -o size=800M tmpfs /data
Estimation: Bloom FilterSpace efficient probabilistic data structure, used to test whether an element is a member of a set or not.
A query returns either:– possibly in set– definitely not in set
Offline Reporting
!!!!استاد
در سطح کشور داریمترددشمار تا دستگاه ۱۰۰۰.که تردد خودرو هارو به دیتابیس مرکزی ارسال می کنند
. رکورد میشه میلیون۵روزی تقریبا
... کنم؟ خیلی زیادهگزارش گیریچه جوری باید
Schemaid BIGSERIAL PRIMARY KEY,station_id INT NOT NULL,passed_time BIGINT NOT NULL,lane INT NOT NULL,speed INT NOT NULL,headway FLOAT NOT NULL,direction INT NOT NULL,is_speed_offender INT NOT NULL,is_headway_offender INT NOT NULL,is_direction_offender INT NOT NULL
SELECT COUNT(*), AVG(speed), AVG(headway), SUM(is_speed_offender),SUM(is_headway_offender),SUM(is_direction_offender)
FROM vbvWHERE passed_time <= 1408652000 AND passed_time >= 1407615200GROUP BY station_id, Lane, DATE_TRUNC('day', passed_time);
Problems
● Record Oriented Storage Engine● Parrallel Query Processor
Record Oriented Engine
id station_id time speed headway direction
1 457812 1407115200 75 2.3 1
2 368525 1402215200 45 1.5 -1
3 458512 1407634200 112 4.7 1
4 369585 1407664200 96 0.9 1
Column Oriented Engineid 1 2 3 4
station_id 457812 368525 458512 369585
time 1407115200 1402215200 1407634200 1407664200
speed 75 45 112 96
headway 2.3 1.5 4.7 0.9
direction 1 -1 1 1
MonetDB
● Column Oriented DBMS● Parallel Query Processor● Just In Time HashIndex
Random CRUD? Fail...
Counting
!!!!استاد
تعداد کلیک هاییه سرور تبلیغاتی داریم که میخاییم . بشماریمکاربران روی لینک های مختلف رو
....بعدش از مشتری پول بگیریم بر همین مبنا
Options
● Good old SQL● Cubrid● MongoDB● Cassandra / Hbase / Riak● Estimate Counting
Good Old SQL
UPDATE `tbl_ads` SET `counter` = `counter` + 1 WHERE `link` = ?
Cubrid● Standrad ACID RDBMS● Built in Counter
SELECT INCR(counter)FROM tbl_adsWHERE link = ?
MongoDB● NoSQL Document Oriented DB● JSON Like Query Language● NO ACID● Built in Counter
db.ads.update({link: ?}, {$inc: {counter: 1}})
Cassandra / HBase / Riak
● Maximum Performance● Distributed Counter
– Each node counts independently
Cassandra / HBase / Riak● Cassandra
– Key/Value– Column Oriented
● HBase– Key/Value– Column Oriented
● Riak– Pure Key/Value
Cassandra
UPDATE tbl_ads SET counter = counter + 1 WHERE link = ?
Estimate CountingHyperLogLog is an approximate
technique for computing the number of distinct entries in a set (cardinality). It does this while using a small amount of memory. For instance, to achieve 99%
accuracy, it needs only 16 KBWikipedia
ComparsionDBMS CAP BASE
Cassandra AP *
MongoDB CP -
HBase CP -
Riak AP *
Hierarchical Structure
!!!!استاد
متادیتای.میخام یه چیزی شبیه دراپ باکس بسازم فایل ها و فولدر ها رو چه جوری باید ذخیره کنم؟
...سیستم شیرینگ فایل و فولدر واقعا پیچیده است
Join Hell
Storing parentchild relation ship with unlimited depth is the main problem
with Relational & Document Oriented DBs. Doing analytics is even harder.
Options● Neo4J● Hybrid Technologies
– OrientDB– ArangoDB
Neo4J● Most robust implementation● Cypher for query language● Server side user space code● Graph algorithms● CA● Blueprint API
Hybrid TechnologiesOne engine to rule them all:– Graph layer– Key/value layer– Document Layer– Configurable CAP
Hybrid Technologies● ArangoDB
– Key/Value– Document Store– Graph Store
● OrientDB
– Document Store– Graph Store
Session Store
!!!!استاد
چند تا میخام برای وب سایت پروژه از باید سشن کاربرها رو یه جای وب سرور . استفاده کنم
.دیگه ذخیره کنم
چه راه حلی پیشنهاد میدین؟
Session: Key/Value Structure
Redis
● Use Hash data structure● Partition data over multiple servers● Enable replication● Take snapshot for persistence
Cassandra / HBase / Riak● More reliable than Redis
– Lower performance● Cross Data center replication
– Cassandra– Riak
● REST Interface– Riak
NTP vs Vector Clock● Cassandra uses NTP
– Clock drift can cause unexpected results– Spend some time on NTP– Not just yum install ntpd
● Riak uses vector clock– Whatever happens to system time you
are rescued!!!
Time Series
!!!!استاد
.تو سطح کشور تعداد زیادی سنسور دماسنج داریم
ثبت و ارسال می کنن ثانیه یک بار۳۰هر اطلعات دمایی رو .همین جا برای ذخیره و بازیابی این همه رکورد
راه حل چی پیشنهاد میدین؟
Time Series
Time series is a sequence of data points, measured typically at successive points in time spaced at uniform time intervals
What we need?● Best Scalibility
Key/Value
● Best Store/Retreive
Column Store
CREATE TABLE temperature (sensor_id INT,capture_day INT, -- 13930101capture_date BIGINT,temperature INT,
PRIMARY KEY ((sensor_id, capture_day),capture_date)
);
● Sensor 1, 13930101 goes to server 1● Sensor 2, 13930101 goes to server 2● Sensor 1, 13930102 goes to server 3● Sensor 2, 13930102 goes to server 4● Sensor 1, 13930103 goes to server 1● Sensor 2, 13930103 goes to server 3● Sensor 1, 13930104 goes to server 4
Options● Cassandra● Hbase
DBMS CAP Base
Cassandra AP *
Hbase CP -
Sparse Columns
!!!!استاد
اکثر ستون هاش نال هستیه سری جدول دارم .ده شده به کوئری هام ..
...به ابرفض دیگه بریدم
Sparse Columnsid user_id(FK) f1 f2 f3
1 234 null 12 null
2 542 null null 987
3 644 null null null
WHERE `f1` = 12 AND `f1` IS NOT NULL
EAV PatternCREATE TABLE tbl (id SERIAL PRIMARY KEY,user_id INT REFERENCES user (id),key TEXT NOT NULL,value TEXT NOT NULL
);
WHERE user_id = ? AND key = 'can_upload'
PostgreSQL: HstoreCREATE TABLE user (id SERIAL PRIMARY KEY,username TEXT NOT NULL,setting HSTORE
);
WHERE user_id = ? AND setting->'can_upload' = true
PostgreSQL: JSONCREATE TABLE user (id SERIAL PRIMARY KEY,username TEXT NOT NULL,setting JSON
);
WHERE user_id = ? AND setting->>'can_upload' = true
Document Store● MongoDB
– JSON Query Language– Schema less– CP
● BaseX– XML– Schema less– XPath and XQuery
DB as a File System
!!!!استاد
همه جا نوشتن نباید فایل ها رو تو دیتابیس ذخیره کرد.من اصلن توجیح نیستم
....اونقدر ها هم کار احمقانه ای به نظر نمیاد
DB as a File System
● ACID Sucks at storing files● Storing small files● Backup handling
MongoDB: GridFS● Designed for storing small files
– Multiple mega bytes at maximum● Crazy simple to use● No permission● You can't mount as a file system
– Only programming API is available
Riak Cloud Storage
● Extremly large files● Amazon S3 Compatible● Comprehensive permission system● Mountable as a file system
?