Mongo db勉強会20110730

3,969 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,969
On SlideShare
0
From Embeds
0
Number of Embeds
129
Actions
Shares
0
Downloads
28
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Mongo db勉強会20110730

    1. 1. Not only NoSQL! RDB- PostgreSQL MongoDB 20110730 @choplin
    2. 2. Why Mongo?
    3. 3. http://www.mongodb.org/display/DOCS/Introduction
    4. 4. Document-oriented http://www.mongodb.org/display/DOCS/Introduction
    5. 5. Document-orientedHigh performance http://www.mongodb.org/display/DOCS/Introduction
    6. 6. Document-orientedHigh performanceHigh availability http://www.mongodb.org/display/DOCS/Introduction
    7. 7. Document-orientedHigh performanceHigh availabilityEasy scalability http://www.mongodb.org/display/DOCS/Introduction
    8. 8. Document-orientedHigh performanceHigh availabilityEasy scalabilityRich query language http://www.mongodb.org/display/DOCS/Introduction
    9. 9. http://www.mongodb.org/display/DOCS/Introduction
    10. 10. Document-oriented http://www.mongodb.org/display/DOCS/Introduction
    11. 11. Document-oriented Documents (objects) map nicely to programming language data types http://www.mongodb.org/display/DOCS/Introduction
    12. 12. Document-oriented Documents (objects) map nicely to programming language data types Embedded documents and arrays reduce need for joins http://www.mongodb.org/display/DOCS/Introduction
    13. 13. Document-oriented Documents (objects) map nicely to programming language data types Embedded documents and arrays reduce need for joins Dynamically-typed (schemaless) for easy schema evolution http://www.mongodb.org/display/DOCS/Introduction
    14. 14. Document-oriented Documents (objects) map nicely to programming language data types Embedded documents and arrays reduce need for joins Dynamically-typed (schemaless) for easy schema evolution No joins and no multi-document transactions for high performance and easy scalability http://www.mongodb.org/display/DOCS/Introduction
    15. 15. http://www.mongodb.org/display/DOCS/Introduction
    16. 16. High performance http://www.mongodb.org/display/DOCS/Introduction
    17. 17. High performance No joins and embedding makes reads and writes fast http://www.mongodb.org/display/DOCS/Introduction
    18. 18. High performance No joins and embedding makes reads and writes fast Indexes including indexing of keys from embedded documents and arrays http://www.mongodb.org/display/DOCS/Introduction
    19. 19. High performance No joins and embedding makes reads and writes fast Indexes including indexing of keys from embedded documents and arrays Optional asynchronous writes http://www.mongodb.org/display/DOCS/Introduction
    20. 20. PointNo Relation (No Join)No Transaction
    21. 21. Point No Relation (No Join) No TransactionSolved many problems!
    22. 22. Join bother me...
    23. 23. How good?
    24. 24. Exapmple
    25. 25. Exapmple Date
    26. 26. Exapmple Date Title
    27. 27. Exapmple Date Title Tag
    28. 28. Exapmple Date Title Tag body
    29. 29. No Join!> use blogswitched to db blog> db.post.insert({ title: " 4 Sugamo.vim ..." ,postDate: new Date(2011, 6, 19) ,tag: [" ", "vim"] ,body: "Sugamo.vim 7/19 ..."})
    30. 30. No Join!> use blogswitched to db blog> db.post.insert({ title: " 4 Sugamo.vim ..." It’s embedded!! ,postDate: new Date(2011, 6, 19) ,tag: [" ", "vim"] ,body: "Sugamo.vim 7/19 ..."})
    31. 31. Array and Object> db.post.insert({ title: " 4 Sugamo.vim ..." ,postDate: new Date(2011, 6, 19) ,tag: [" ", "vim"] ,body: "Sugamo.vim 7/19 ..." ,comment: [ { name: “vimmer” ,comment: “vim is great!” } ,{}... ]})
    32. 32. Array and Object> db.post.insert({ title: " 4 Sugamo.vim ..." Array ,postDate: new Date(2011, 6, 19) ,tag: [" ", "vim"] ,body: "Sugamo.vim 7/19 ..." ,comment: [ { name: “vimmer” ,comment: “vim is great!” } ,{}... ]})
    33. 33. Array and Object> db.post.insert({ title: " 4 Sugamo.vim ..." Array ,postDate: new Date(2011, 6, 19) ,tag: [" ", "vim"] ,body: "Sugamo.vim 7/19 ..." ,comment: [ Objects in array { name: “vimmer” ,comment: “vim is great!” } ,{}... ]})
    34. 34. Embedded
    35. 35. EmbeddedReduce join
    36. 36. EmbeddedReduce join Better performance
    37. 37. EmbeddedReduce join Better performance Join itself (executer)
    38. 38. EmbeddedReduce join Better performance Join itself (executer) Query plan (optimizer)
    39. 39. EmbeddedReduce join Better performance Join itself (executer) Query plan (optimizer)Reduce impedance mismatch between dband apps
    40. 40. EmbeddedReduce join Better performance Join itself (executer) Query plan (optimizer)Reduce impedance mismatch between dband appsAlso easy to understand :)
    41. 41. EmbeddedReduce join Better performance Join itself (executer) Query plan (optimizer)Reduce impedance mismatch between dband appsAlso easy to understand :) but, still have to consider a lot ...
    42. 42. Things to be considered
    43. 43. Things to be consideredDocument size limitation (16MB)
    44. 44. Things to be consideredDocument size limitation (16MB)Write / Read ratio
    45. 45. Things to be consideredDocument size limitation (16MB)Write / Read ratioType of query
    46. 46. Things to be consideredDocument size limitation (16MB)Write / Read ratioType of queryIn-place update
    47. 47. Things to be consideredDocument size limitation (16MB)Write / Read ratioType of queryIn-place updateetc.
    48. 48. More detailOfficial : Schema design http://www.mongodb.org/display/DOCS/Schema+DesignMongoDB Schema Design - MongoSF 2011 http://speakerdeck.com/u/kbanker/p/mongodb-schema-design- mongosf-2011MongoDB Schema Design : myNoSQL http://nosql.mypopescu.com/post/907003504/mongodb-schema- designMongoDB Relation http://d.hatena.ne.jp/masa_w/20101130/1291084939
    49. 49. More detailSchema Design ?
    50. 50. Anyway,
    51. 51. Great Feature!
    52. 52. Wait!
    53. 53. I can!
    54. 54. Arrayblog=# CREATE TABLE post ( id int ,title text ,postDate date ,tag text[] ,body text);
    55. 55. Arrayblog=# CREATE TABLE post ( id int ,title text Array of text ,postDate date ,tag text[] ,body text);
    56. 56. Arrayblog=# INSERT INTO post VALUES ( 1 ,’ 4 Sugamo.vim ...’ ,’2011/7/19’ ,ARRAY[‘ ’, ‘vim’] ,’Sugamo.vim 7/19 ...’ );
    57. 57. Arrayblog=# INSERT INTO post VALUES ( 1 ,’ 4 Sugamo.vim ...’ Array literal ,’2011/7/19’ ,ARRAY[‘ ’, ‘vim’] ,’Sugamo.vim 7/19 ...’ );
    58. 58. Arrayblog=# SELECT tag FROM post tag -------------- { ,vim} (1 row)
    59. 59. has Array!※SQL Standard (SQL99)
    60. 60. Difference
    61. 61. DifferenceDynamically typed{ tag : [1, ‘two’, new Date()]}
    62. 62. DifferenceDynamically typed{ tag : [1, ‘two’, new Date()]} Statically typedSELECT ARRAY[1, 2, 3]::int[]
    63. 63. Array utilities
    64. 64. Array to string> db.post.findOne({tag:"vim"}).tag.join(,) ,vim
    65. 65. Array to stringblog=# SELECT array_to_string(tag, ,) FROM post; array_to_string ----------------- ,vim (1 row)
    66. 66. String to array> db.post.findOne{tag:"vim"}).body.split( ) [ "Sugamo.vim", "7/19", "..." ]
    67. 67. String to arrayblog=# SELECT string_to_array(body, ) FROM post; string_to_array ---------------------- {Sugamo.vim,7/19,..} (1 row)
    68. 68. array to rowblog=# SELECT unnest(body, ) FROM post; unnest -------- vim (2 rows)
    69. 69. row to arrayblog=# SELECT array_agg(v) FROM ( SELECT unnest(tag, ) v FROM post ) t; array_agg -------------- { ,vim} (1 row)
    70. 70. Indexing an array
    71. 71. Indexing > db.post.ensureIndex({tag:1}); > db.post.find({tag:"vim"}).explain(){ "isMultiKey" : true,! "cursor" : "BtreeCursor ! "indexOnly" : false,tag_1", ! "indexBounds" : {! "nscanned" : 1, ! ! "tag" : [! "nscannedObjects" : 1, ! ! ! [! "n" : 1, ! ! ! ! "vim",! "millis" : 0, ! ! ! ! "vim"! "nYields" : 0, ! ! ! ]! "nChunkSkips" : 0, ! ! ]! ! } } http://www.slideshare.net/matsuou1/20110514-mongo-db
    72. 72. Indexingblog=# CREATE INDEX on post USING gin(tag); ※Gin : Generalized Inverted Index
    73. 73. Indexingblog=# set enable_seqscan = off;blog=# EXPLAIN SELECT * FROM post WHERE tag @> ARRAY[vim] QUERY PLAN--------------------------------------------------------------------------- Bitmap Heap Scan on post (cost=4.26..8.27 rows=1 width=104) Recheck Cond: (tag @> {vim}::text[]) -> Bitmap Index Scan on post_tag_idx (cost=0.00..4.26 rows=1 width=0) Index Cond: (tag @> {vim}::text[])(4 rows)
    74. 74. Both useInverted Index
    75. 75. Inverted Index Data IndexT0 = "it is what it is", "a": {2}T1 = "what is it", "banana": {2}T2 = "it is a banana" "is": {0, 1, 2} "it": {0, 1, 2} "what": {0, 1} http://ja.wikipedia.org/wiki/%E8%BB%A2%E7%BD%AE %E3%82%A4%E3%83%B3%E3%83%87%E3%83%83%E3%82%AF%E3%82%B9
    76. 76. Inverted Index Data IndexT0 = "it is what it is", "a": {2}T1 = "what is it", "banana": {2}T2 = "it is a banana" "is": {0, 1, 2} "it": {0, 1, 2} "what": {0, 1} key http://ja.wikipedia.org/wiki/%E8%BB%A2%E7%BD%AE %E3%82%A4%E3%83%B3%E3%83%87%E3%83%83%E3%82%AF%E3%82%B9
    77. 77. Inverted Index Data IndexT0 = "it is what it is", "a": {2}T1 = "what is it", "banana": {2}T2 = "it is a banana" "is": {0, 1, 2} "it": {0, 1, 2} "what": {0, 1} posting list key /bucket http://ja.wikipedia.org/wiki/%E8%BB%A2%E7%BD%AE %E3%82%A4%E3%83%B3%E3%83%87%E3%83%83%E3%82%AF%E3%82%B9
    78. 78. Inner Structurekey tree
    79. 79. Inner Structure posting treekey tree /bucket tree
    80. 80. Inner Structure posting treekey tree /bucket tree ※ Wanted! More precise info at MongoReading :)
    81. 81. Aggregate
    82. 82. Map/Reducemap = function() { if (!this.tags) { return; } for (index in this.tags) { emit(this.tags[index], 1); }} http://cookbook.mongodb.org/patterns/count_tags/
    83. 83. Map/Reducereduce = function(previous, current) { var count = 0; for (index in current) { count += current[index]; } return count;} http://cookbook.mongodb.org/patterns/count_tags/
    84. 84. Map/Reduce> result = db.runCommand({... "mapreduce" : "posts",... "map" : map,... "reduce" : reduce,... "out" : "tags"}) > db.tags.find() { "_id" : "vim", "value" : 1 } { "_id" : " ", "value" : 1 } http://cookbook.mongodb.org/patterns/count_tags/
    85. 85. Group Byblog=# SELECT tag, count(tag) FROM ( SELECT unnest(tag, ) tag FROM post )t GROUP BY tag; tag | count --------+------- | 1 vim | 1 (2 rows)
    86. 86. Semi-structured
    87. 87. Object> db.post.insert({ title: " 4 Sugamo.vim ..." ,postDate: new Date(2011, 6, 19) ,tag: [" ", "vim"] ,body: "Sugamo.vim 7/19 ..." ,comment: [ { Object name: “vimmer” ,comment: “vim is great!” } ,{}... ]})
    88. 88. Some choice
    89. 89. contrib/hstore# SELECT ‘a => 1, b => 2’::hstore"a"=>"1", "b"=>"2"Type for group of (key => value)Can be indexed with Gin/GiSTCan’t be nested ....
    90. 90. XML Type<book> <title> </title> <author> </author> <author> </author> <publisher> </publisher> </book> http://lets.postgresql.jp/documents/tutorial/xml/1/
    91. 91. XML Typetestdb=# CREATE TABLE t_books(books xml);testdb=# INSERT INTO t_books(books) VALUES(testdb# <book>testdb# <title> </title>testdb# <author> </author><author> </author>testdb# <publisher> </publisher>testdb# </book>testdb# ); http://lets.postgresql.jp/documents/tutorial/xml/1/
    92. 92. XML Typetestdb=# SELECT xpath(/book[author/text()=" "/title, books) FROM t_books; xpath --------------------------------------------- {<title> </title>} (1 rows) http://lets.postgresql.jp/documents/tutorial/xml/1/
    93. 93. XML TypeAccess element with xpath()Can be indexed with xpath/Functional Index http://lets.postgresql.jp/documents/tutorial/xml/1/
    94. 94. JSON TypeBeing implemented ... 9.2?
    95. 95. hardly get along with relational model ...
    96. 96. Why Mongo?
    97. 97. The Scripting Language of Databases $m = new Mongo(); $m->test->formResults->insert($_POST); print_r($m->test->formResults->findOne());http://www.snailinaturtleneck.com/blog/2011/04/06/the-scripting-language-of-databases/
    98. 98. Philosophy http://www.mongodb.org/display/DOCS/Philosophy
    99. 99. My OpinionUse Mongo instead of ORM! (or write SQL directly) ORM 11 http://tech.a-listers.jp/2011/06/16/orm_is_an_antipattern/
    100. 100. Thank you !

    ×