阿里面试官问:说说你使用MySQL的NoSQL的七大理由?
MySQL 8.0中一个很棒的新功能是文档存储。 现在使用MySQL,您可以将JSON文档存储在集合中,并使用CRUD操作对其进行管理。NoSQL现在是MySQL的一部分! 而不是MongoDB和MySQL的混合,现在你可以消除MongoDB并与MySQL合并!
这是NoSQL和SQL在同一个数据库服务器上的历史性汇集 !
image.png要将MySQL 8.0用作文档存储,您需要安装X插件(默认情况下从8.0.11开始)。 该插件支持[X DevAPI],提供编程接口。 使用X协议与MySQL服务器通信的客户端可以使用X DevAPI开发应用程序。 然后X协议允许这些客户端和服务器之间更灵活的连接。 它支持SQL和NoSQL API,并且可以执行非阻塞异步调用。
让我们来看下使用MySql来完成你NoSQL功能的主要原因
- MySQL关心你的数据安全! MySql的NoSQL完全符合ACID标准
- CRUD操作并不一定需要SQL来完成
- 无模式
- Document的数据完整性提供了保证
- 允许SQL查询Document(对分析非常重要)
- 文档没有16MB的限制
- 提供简单的查询语法
MySQL关心你的数据! NoSQL完全符合ACID标准
与传统的NoSQL数据库不同,MySQL符合完整的[ACID] (原子性,一致性,隔离性和耐久性)。 大多数时候NoSQL数据库都被人诟病,说它们缺乏持久性。 这意味着有时数据可能会在崩溃后丢失。 实际上,也不保证一致性 。
但是 MySQL Document Store依赖于InnoDB存储引擎,因此文档存储受益于InnoDB的强大和强大。默认情况下,开箱即用,InnoDB是完全持久的,一旦数据被确认为已提交,它将不会丢失:
···
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> innodb_flush_log_at_trx_commit = 1 innodb_doublewrite = ON sync_binlog = 1</pre>
···
MySQL Document 存储也支持原子性和隔离,因为还支持事务!
看下下面的示例:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] JS> db.users.find() [ { "_id": "00005ad754c90000000000000001", "name": "lefred" } ] 1 document in set (0.0109 sec) MySQL [localhost+ ssl/docstore] JS> session.startTransaction() Query OK, 0 rows affected (0.0069 sec) MySQL [localhost+ ssl/docstore] JS> db.users.add({name: 'dim0'}) Query OK, 1 item affected (0.0442 sec) MySQL [localhost+ ssl/docstore] JS> db.users.find().fields('name') [ { "name": "lefred" }, { "name": "dim0" } ] 2 documents in set (0.0579 sec) MySQL [localhost+ ssl/docstore] JS> session.rollback() Query OK, 0 rows affected (0.1201 sec) MySQL [localhost+ ssl/docstore] JS> db.users.find().fields('name') [ { "name": "lefred" } ] 1 document in set (0.0004 sec) </pre>
正如您所看到的,是不是在使用上和mongodb 很像了,而且MySQL使用简单的语义支持事务,
CRUD操作(SQL不再是强制性的)
通过新的MySQL文档存储和X协议,我们引入了新的操作来管理集合和/或关系表。 这些操作称为[CRUD] (创建,读取,更新,删除)操作,它们允许您在不编写单行SQL的情况下管理数据。
我们当然将CRUD操作区分为两组,一组用于集合 ,另一组用于表 :
Collection上的CRUD函数表上的CRUD函数add() :CollectionInsertObjinsert() :InsertObjfind() :CollectionFindObjselect() :SelectObjmodify() :CollectionUpdateObjupdate() :UpdateObjremove() :CollectionDeleteObjdelete() :DeleteObj
您已经在上面的示例中看到了如何使用find()和add() ,这个时候不使用sql语句也能操作数据了。
无框架
MySQL Document Store为您带来了新的无模式数据。 实际上,在存储文档时,您不需要事先知道所有属性,如果需要,您可以随时修改文档。 由于您不需要专注于表设计,因此您无需处理规范化,外键和约束甚至数据类型。 这使您可以进行非常快速的初始开发。 由于文档是动态的,因此模式迁移也不再是问题,并且可以忘记大的ALTER语句。
例如,让我们使用以下数据:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] JS> db.users.find() [ { "_id": "00005ad754c90000000000000001", "name": "lefred" }, { "_id": "00005ad754c90000000000000003", "name": "dim0" }, { "_id": "00005ad754c90000000000000004", "name": "Dave" }, { "_id": "00005ad754c90000000000000005", "name": "Luis" }, { "_id": "00005ad754c90000000000000006", "name": "Sunny" } ] 5 documents in set (0.0007 sec) </pre>
现在让我们假设我们想为所有用户添加一个新属性,您可以在不运行ALTER语句的情况下执行此操作:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">MySQL [localhost+ ssl/docstore] JS> db.users.modify('1').set('department', 'development') Query OK, 5 items affected (0.1910 sec) </pre>
让我们验证其中一条记录:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] JS> db.users.find("name = 'Sunny'") [ { "_id": "00005ad754c90000000000000006", "department": "development", "name": "Sunny" } ] 1 document in set (0.0005 sec) </pre>
因此,具有这种可能性,为开发人员提供了更多的自由来维护他们编写文档的方式,并且他们不依赖于需要运行大型alter语句的运营团队
文档可以提供数据完整性
即使无模式非常重要,有时人们也希望强制数据完整性。 使用MySQL文档存储,也可以为文档创建和维护约束和外键。
这是一个我们有两个集合的例子: 用户和部门 ,我们创建了一个GENERATED STORED列作为外键:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] SQL> alter table departments add column dept varchar(20) generated always as (doc->>".department") STORED ; Query OK, 5 rows affected (0.1302 sec) MySQL [localhost+ ssl/docstore] SQL> select * from users; +---------------------------------------------------------------------------------------+------------------------------+-------------+ | doc | _id | dept | +---------------------------------------------------------------------------------------+------------------------------+-------------+ | {"_id": "00005ad754c90000000000000001", "name": "lefred", "department": "community"} | 00005ad754c90000000000000001 | community | | {"_id": "00005ad754c90000000000000003", "name": "dim0", "department": "community"} | 00005ad754c90000000000000003 | community | | {"_id": "00005ad754c90000000000000004", "name": "Dave", "department": "community"} | 00005ad754c90000000000000004 | community | | {"_id": "00005ad754c90000000000000005", "name": "Luis", "department": "development"} | 00005ad754c90000000000000005 | development | | {"_id": "00005ad754c90000000000000006", "name": "Sunny", "department": "development"} | 00005ad754c90000000000000006 | development | +---------------------------------------------------------------------------------------+------------------------------+-------------+ 5 rows in set (0.0010 sec) MySQL [localhost+ ssl/docstore] SQL> select * from departments; +------------------------------------------------------------------------------------+------------------------------+-------------+ | doc | _id | dept | +------------------------------------------------------------------------------------+------------------------------+-------------+ | {"_id": "00005ad754c90000000000000007", "name": "development", "manager": "Tomas"} | 00005ad754c90000000000000007 | development | | {"_id": "00005ad754c90000000000000008", "name": "community", "manager": "Andrew"} | 00005ad754c90000000000000008 | community | +------------------------------------------------------------------------------------+------------------------------+-------------+ 2 rows in set (0.0004 sec) </pre>
让我们在这些新列上添加一些索引:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">MySQL [localhost+ ssl/docstore] SQL> alter table users add index dept_idx(dept); Query OK, 0 rows affected (0.0537 sec) MySQL [localhost+ ssl/docstore] SQL> alter table departments add index dept_idx(dept); Query OK, 0 rows affected (0.1278 sec) </pre>
现在,让我们创建约束,我希望如果我删除部门,那些部门的所有用户都会被删除:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] SQL> alter table users add foreign key (dept) references departments(dept) on delete cascade; Query OK, 5 rows affected (0.2401 sec) MySQL [localhost+ ssl/docstore] SQL> delete from departments where doc->>"$.manager" like 'Andrew'; Query OK, 1 row affected (0.1301 sec) MySQL [localhost+ ssl/docstore] SQL> select * from departments; +------------------------------------------------------------------------------------+------------------------------+-------------+ | doc | _id | dept | +------------------------------------------------------------------------------------+------------------------------+-------------+ | {"_id": "00005ad754c90000000000000007", "name": "development", "manager": "Tomas"} | 00005ad754c90000000000000007 | development | +------------------------------------------------------------------------------------+------------------------------+-------------+ 1 row in set (0.0007 sec) MySQL [localhost+ ssl/docstore] SQL> select * from users; +---------------------------------------------------------------------------------------+------------------------------+-------------+ | doc | _id | dept | +---------------------------------------------------------------------------------------+------------------------------+-------------+ | {"_id": "00005ad754c90000000000000005", "name": "Luis", "department": "development"} | 00005ad754c90000000000000005 | development | | {"_id": "00005ad754c90000000000000006", "name": "Sunny", "department": "development"} | 00005ad754c90000000000000006 | development | +---------------------------------------------------------------------------------------+------------------------------+-------------+ 2 rows in set (0.0006 sec) </pre>
正如您所看到的,实现外键约束以增强数据完整性是可能且非常容易的。
允许SQL(对分析非常重要)
正如您在前面的观点中看到的那样,可以将SQL和NoSQL混合使用,事实上,我认为前端的开发人员只会使用NoSQL来创建和使用看作对象的数据,而在后台工作更多的人将继续使用SQL来创建报表和分析。
为了说明使用Documents的MySQL的强大功能,我将使用另一种流行的NoSQL解决方案中受欢迎的餐厅示例集合。
这是存储在集合中的文档示例:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] JS> db.restaurants.find().limit(1) [ { "_id": "5ad5b645f88c5bb8fe3fd337", "address": { "building": "1007", "coord": [ -73.856077, 40.848447 ], "street": "Morris Park Ave", "zipcode": "10462" }, "borough": "Bronx", "cuisine": "Bakery", "grades": [ { "date": "2014-03-03T00:00:00Z", "grade": "A", "score": 2 }, { "date": "2013-09-11T00:00:00Z", "grade": "A", "score": 6 }, { "date": "2013-01-24T00:00:00Z", "grade": "A", "score": 10 }, { "date": "2011-11-23T00:00:00Z", "grade": "A", "score": 9 }, { "date": "2011-03-10T00:00:00Z", "grade": "B", "score": 14 } ], "name": "Morris Park Bake Shop", "restaurant_id": "30075445" } ] </pre>
可以看到餐厅可以评级,当然还有一种美食风格。
我们可以尝试找到结果的一个简单查询是:每家餐厅的平均成绩是多少? (并将结果限制为10)
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] SQL> SELECT name, cuisine, avg(rating) FROM restaurants, -> JSON_TABLE(doc, ".name", -> cuisine varchar(100) path ".grades[*]" -> columns (rating int path "$.score"))) -> AS jt GROUP BY name, cuisine LIMIT 10; +--------------------------------+---------------------------------+-------------+ | name | cuisine | avg(rating) | +--------------------------------+---------------------------------+-------------+ | Morris Park Bake Shop | Bakery | 8.2000 | | Wendy'S | Hamburgers | 9.4404 | | Dj Reynolds Pub And Restaurant | Irish | 9.2500 | | Riviera Caterer | American | 9.0000 | | Tov Kosher Kitchen | Jewish/Kosher | 17.7500 | | Brunos On The Boulevard | American | 17.0000 | | Kosher Island | Jewish/Kosher | 10.5000 | | Wilken'S Fine Food | Delicatessen | 10.0000 | | Regina Caterers | American | 9.6000 | | Taste The Tropics Ice Cream | Ice Cream, Gelato, Yogurt, Ices | 8.2500 | +--------------------------------+---------------------------------+-------------+ 10 rows in set, 13 warnings (1.5114 sec) </pre>
是不是特别的棒,用sql也完成了Document的查询
再来看下一个另外的问题
给我10家最好的餐厅,但他们都必须来自不同的美食! (这意味着,如果两个最好的餐厅是2个意大利餐厅,我只想看到两个中最好的餐厅,列表中的下一个餐厅应该是第三个是不供应意大利食品)。
在MySQL 8.0文档存储中,我们可以使用公用表表达式(CTE)通过一个查询实现此目的:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> MySQL [localhost+ ssl/docstore] SQL> SELECT name, cuisine, avg(rating) FROM restaurants, -> JSON_TABLE(doc, ".name", -> cuisine varchar(100) path ".grades[*]" -> columns (rating int path "$.score"))) -> AS jt GROUP BY name, cuisine LIMIT 10; +--------------------------------+---------------------------------+-------------+ | name | cuisine | avg(rating) | +--------------------------------+---------------------------------+-------------+ | Morris Park Bake Shop | Bakery | 8.2000 | | Wendy'S | Hamburgers | 9.4404 | | Dj Reynolds Pub And Restaurant | Irish | 9.2500 | | Riviera Caterer | American | 9.0000 | | Tov Kosher Kitchen | Jewish/Kosher | 17.7500 | | Brunos On The Boulevard | American | 17.0000 | | Kosher Island | Jewish/Kosher | 10.5000 | | Wilken'S Fine Food | Delicatessen | 10.0000 | | Regina Caterers | American | 9.6000 | | Taste The Tropics Ice Cream | Ice Cream, Gelato, Yogurt, Ices | 8.2500 | +--------------------------------+---------------------------------+-------------+ 10 rows in set, 13 warnings (1.5114 sec) </pre>
酷不是吗? 所以SQL不再是强制性的,但它仍然可以提供帮助。 您还可以执行更多操作,例如使用关系表等连接文档......当然,不要忘记我们的工程师实施的大型JSON函数面板。
文件的16MB限制
与其他一些NoSQL解决方案不同,MySQL不会将文档大小限制为16MB(mongdb就有着限制).但现在他们有一个解决方案:MySQL!
MySQL文档存储中的单个文档大小可以为1GB ! 它受[maxallowedpacket]大小的限制。
因此,如果您有非常大的文档,MySQL是最适合您的解决方案。 在MySQL 8.0中,我们还改进了MySQL处理大型文档的方式。 InnoDB实现了[JSON部分更新],复制团队[为二进制日志]实现了[部分JSON]。
最简单的查询语法
MySQL文档存储的目标不是成为MongoDB的替代品,即使[将数据迁移MySQL也非常容易 。 新CRUD API的目标是为开发人员提供编写应用程序的最简单方法,而无需过多地处理数据库后端,并能够以最简单的方式查询数据。 因此,MySQL使用非常简单的语法来查询存储在MySQL 8.0中的文档。
让我们比较MongoDB和MySQL中的相同查询:
MongoDB中的第一个:
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">db.restaurants.find({"cuisine": "French", "borough": { $not: /^Manhattan/} }, {"_id":0, "name": 1,"cuisine": 1, "borough": 1}).limit(2) { "borough" : "Queens", "cuisine" : "French", "name" : "La Baraka Restaurant" } { "borough" : "Queens", "cuisine" : "French", "name" : "Air France Lounge" } </pre>
那是什么意思呢? 上面的语法是否特别怪癖?
那接下来使用MySql的语法来看下上面的语句
<pre style="box-sizing: border-box; font-family: monospace; font-size: 18px; margin: 20px 0px; padding: 15px; border: 0px; background-color: rgb(244, 245, 246); white-space: pre-wrap; word-break: break-all; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">MySQL [localhost+ ssl/docstore] JS> restaurants.find("cuisine='French' AND borough!='Manhattan'").fields(["name","cuisine","borough"]).limit(2) [ { "borough": "Queens", "cuisine": "French", "name": "La Baraka Restaurant" }, { "borough": "Queens", "cuisine": "French", "name": "Air France Lounge" } ] 2 documents in set (0.0853 sec) </pre>
更明显不是吗?是不是也更好理解了, 这就是我们使用该语法的原因!
你的关注是我创作的动力,万水千山总是情,点个关注行不行?