Top Banner
© 2017 Percona 1 Peter Zaitsev MySQL vs MongoDB When to Use Which Technology CEO Percona University, Budapest May 11 th , 2017
33

MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

Mar 19, 2018

Download

Documents

dinhlien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona1

PeterZaitsev

MySQLvsMongoDBWhentoUseWhichTechnology

CEOPerconaUniversity,BudapestMay11th,2017

Page 2: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona2

InThisPresentation

VerybriefdiscussiononmeritsofMySQLand

MongoDB

Page 3: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona3

WhyMySQLandMongoDB?MostPopularOpenSource SQLandNoSQLEngines

Page 4: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona4

WhyMySQLandMongoDB?

TwoTechnologiesPerconaProvidesSolutionsFor

Page 5: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona5

FullDisclosure

IknowMySQLMuchbetterthanMongoDB…whichwillimpactmy

bias

Page 6: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona6

MySQL

RelationalDatabaseFirstandForemost

FullSQLSupport,Transactions,ACID

DesignedforaSingleServerfirst

Scale-OutasAfterthought

Page 7: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona7

MongoDB

Designedfor“WebScale”

Scalability,Cloud,MultipleMachines

ReplicationandShardingpartofinitialdesign

Onlyfeatureswhichcanscale

Page 8: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona8

Q1:Whatdoyouknowandlove?

BothMySQLandMongoDBarevery

capable.Yourexperienceandpreferencematter

Page 9: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona9

Q2:Whichdatamodelfitsbetter?

Relational

•MySQLObviousChoice

DocumentBased

•MongoDBObviouschoice

•MySQLhasDocumentStorestarting5.7

Page 10: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona10

Q3:HowDataisUsed

Databelongstosingleapplication

• JSONmodelmoreexpressiveforapplicationdatastructures

• Schemadesignedforspecificaccesspaths

Datasharedbymultipleapplications

• Relationalstructureeasiertoshare

• Canbemoreflexibleinhowdataisaccessed

Page 11: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona11

Q4:Transactions

NeedfullTransactions

•MySQLcanbebetterchoice

•OneofthemainbenefitsofMySQLDocumentStore

DonotneedTransactions

•MongoDBcanbegreatchoice

•CandoAtomicDocumentUpdates

Page 12: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona12

Q5:JOINs

AdvancedJOINsandotherSQLfeatures

• MySQLmuchmorepowerful

• $lookupand$graphLookupfeaturesinMongoDBaggregationframework

Mainlysimplelookupswithfilters/sorting

• MongoDBandMySQLbothdotheseverywell

Page 13: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona13

Q6:Scale

SingleServerisGoodEnough

• MySQLworksgreat• WelloptimizedforManycores;largememory;faststorage

NeedMassiveScaleout

• AutomatedshadinginMongoDBismuchbetter

• ReplicationinMongoDBiseasiertouse

• SolutionslikeVitesstrytomakeitlesspainfulforMySQL

Page 14: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona14

Q7:LargeScaleAggregation

MongoDB

• hasbuiltinaggregationframeworkforparallelprocessing

• BIConnectorandToroDBforSQLaccess

• ReplicatetoHadoop

MySQL

• Executeseveryquerysinglethreaded

•MariaDBColumnStore(InfiniDBreborn)

• ClickHouse• ReplicatetoHadoop

Page 15: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona15

MySQLandMongoDBcomparedCourtesyofAlexanderRubin

Page 16: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona16

Fromto

MySQL MongoDB

mysql> select * from zips limit 1\G*************************** 1. row ***************************country_code: USpostal_code: 34050place_name: FPOadmin_name1: admin_code1: AAadmin_name2: Erieadmin_code2: 029admin_name3: admin_code3:

latitude: 41.03750000longitude: -111.67890000accuracy:

1 row in set (0.00 sec)

MongoDB shell version: 3.0.8connecting to: zips> db.zips.find().limit(1).pretty(){

"_id" : "01001","city" : "AGAWAM","loc" : [

-72.622739,42.070206

],"pop" : 15338,"state" : "MA"

}

Page 17: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona17

WhereismySQL?

MySQL MongoDB

CREATE TABLE users (

id MEDIUMINT NOT NULL

AUTO_INCREMENT,

user_id Varchar(30),

age Number,

status char(1),

PRIMARY KEY (id)

)

db.users.insert( {

user_id: "abc123",

age: 55,

status: "A"

} )

(no schema)

SQLtoMongoDBMappingCharthttps://docs.mongodb.org/manual/reference/sql-comparison/

Page 18: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona18

WhereismySQL?

MySQL MongoDB

SELECT *

FROM users

WHERE status = "A"

AND age = 50

db.users.find(

{ status: "A",

age: 50 }

)

SQLtoMongoDBMappingCharthttps://docs.mongodb.org/manual/reference/sql-comparison/

Page 19: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona19

Whereismy/etc/my.cnf?

MySQL MongoDB

/etc/my.cnf /etc/mongod.conf

# Where and how to store data.storage:

dbPath: /datawtjournal:

enabled: trueengine: wiredTiger

...

/usr/bin/mongod -f /etc/mongod.conf

Page 20: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona20

Wherearemydatabases/tables?

MySQL MongoDB

Databasesmysql> show databases;+--------------------+| Database |+--------------------+| information_schema |...

mysql> use zipsDatabase changed

Tablesmysql> show tables;+----------------+| Tables_in_zips |+----------------+| zips |+----------------+

Databases> show dbs;admin 0.000GBlocal 0.000GBosm 13.528GBtest 0.000GBzips 0.002GB

> use zipsswitched to db zips

Collections> show collectionszips> show tables // samezips

Page 21: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona21

WhereismyInnoDB?

MySQL MongoDB

MyISAM

InnoDB

TokuDB

MyRocks(RocksDB)*

MMAPv1 memorymappedstoredengine,

WiredTigertransactional,withcompression,btree

PerconaMemoryEngine

MongoRocks (RocksDB)

Page 22: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona22

WhereismyProcesslist?

mysql> show processlist\G*************************** 1. row ***************************

Id: 137259User: rootHost: localhostdb: geonames

Command: QueryTime: 0State: initInfo: show processlist

Rows_sent: 0Rows_examined: 01 row in set (0.00 sec)

> db.currentOp(){

"inprog" : [{

"desc" : "conn28","threadId" : "0x19b85260","connectionId" : 28,"opid" : 27394208,"active" : true,"secs_running" : 3,"microsecs_running" :

NumberLong(3210539),"op" : "query","ns" : "osm.points3","query" : {

"name" : "Durham"},"planSummary" : "COLLSCAN","client" : "127.0.0.1:58835","numYields" : 24905,"locks" : {

"Global" : "r","Database" : "r","Collection" : "r"

},"waitingForLock" : false,

...}

Page 23: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona23

WherearemyGrants?

mysql> grant all on *.* to user@localhost identified by ‘pass’;

> use productsdb.createUser(

{user: "accountUser",pwd: "password",roles: [ "readWrite",

"dbAdmin" ]}

)

Page 24: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona24

WhereismyIndex?

MySQL MongoDBmysql> show keys from zips\G*************************** 1. row ***************************

Table: zipsNon_unique: 0Key_name: PRIMARY

Seq_in_index: 1Column_name: idCollation: A

Cardinality: 0Sub_part: NULLPacked: NULLNull:

Index_type: BTREEComment:

Index_comment: *************************** 2. row ***************************

Table: zipsNon_unique: 1Key_name: postal_code

Seq_in_index: 1...

> db.zips.getIndexes()[

{"v" : 1,"key" : {

"_id" : 1},"name" : "_id_","ns" : "zips.zips"

}]

Page 25: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona25

Whereismyaddindex?

mysql> alter table zips add key (postal_code);Query OK, 0 rows affected (0.10 sec)Records: 0 Duplicates: 0 Warnings: 0

> db.zips.createIndex({ state : 1 } ){

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 1,"numIndexesAfter" : 2,"ok" : 1

}

// Index can be sorted:

> db.zips.createIndex({ state : -1 } ){

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 2,"numIndexesAfter" : 3,"ok" : 1

}

Page 26: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona26

WhereismySlowQueryLog?

MySQL MongoDB

mysql> set global long_query_time = 0.1;Query OK, 0 rows affected (0.02 sec)

mysql> set global slow_query_log = 1;Query OK, 0 rows affected (0.02 sec)

mysql> show global variables like 'slow_query_log_file';+---------------------+------------------------------+| Variable_name | Value |+---------------------+------------------------------+| slow_query_log_file | /var/lib/mysql/thor-slow.log |+---------------------+------------------------------+1 row in set (0.00 sec)

db.setProfilingLevel(level,slowms)

Level: 0 for no profiling, 1 for only slow operations, or 2 for all

operations.

Slowms = long_query_time but in milliseconds> db.setProfilingLevel(2, 100);{ "was" : 0, "slowms" : 100, "ok" : 1 }

> db.system.profile.find( { millis : { $gt : 100 } } ).pretty(){

"op" : "query","ns" : "zips.zips","query" : {

"city" : "DURHAM"},"ntoreturn" : 0,

..

Page 27: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona27

Export from MySQL 5.7:mysql> SELECT JSON_OBJECT('name', replace(name, '"', ''), 'other_tags', replace(other_tags, '"', ''), 'geometry', st_asgeojson(shape)) as j

FROM `points` INTO OUTFILE '/var/lib/mysql-files/points.json'; Query OK, 13660667 rows affected (4 min 1.35 sec)

Fromto

Page 28: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona28

Load to MongoDB (parallel):mongoimport --db osm --collection points -j 24 --file /var/lib/mysql-files/points.json 2016-04-11T22:38:10.029+0000 connected to: localhost2016-04-11T22:38:13.026+0000 [........................] osm.points 31.8 MB/2.2 GB (1.4%)2016-04-11T22:38:16.026+0000 [........................] osm.points 31.8 MB/2.2 GB (1.4%)2016-04-11T22:38:19.026+0000 [........................] osm.points 31.8 MB/2.2 GB (1.4%)…2016-04-11T23:12:13.447+0000 [########################] osm.points 2.2 GB/2.2 GB (100.0%)2016-04-11T23:12:15.614+0000 imported 13660667 documents

Fromto

Page 29: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona29

ThinkingaboutusingMongoDB?ConsidertryingoutPerconaServerforMongoDB

Page 30: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona30

PerconaServerforMongoDB3.4

100%CompatiblewithMongoDB3.4CommunityEdition

OpenSourcewithAlternativestomanyMongoDBEnterpriseFeatures

MongoRocks(RocksDB)andPerconaMemoryEngine

New:SensitiveDataMasking

New:QuerySampling

New:HotBackupforWiredTiger andMongoRocks

Page 31: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona31

PerconaMemoryEngineforMongoDBBenchmarks

Page 32: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

©2017Percona32

WiredTiger vsMongoRocks – writeintensive

Page 33: MySQL vs MongoDB - Percona – The Database · PDF fileMySQL vs MongoDB When to Use Which Technology CEO Percona University ... (InfiniDB reborn) •ClickHouse •Replicate to Hadoop.

DATABASE PERFORMANCEMATTERS

DatabasePerformanceMattersDatabasePerformanceMattersDatabasePerformanceMattersDatabasePerformanceMattersDatabasePerformanceMatters