search
HomeDatabaseMysql Tutorial文档型数据库设计模式

文档型数据库设计模式

Jun 07, 2016 pm 03:35 PM
storagedatadatabasestructureDesign Patterns

在数据库中存储树形结构的数据,这是一个非常普遍的需求,典型的比如论坛系统的版块关系。在传统的关系型数据库中,就已经产生了各种解决方案。 此文以存储树形结构数据为需求,分别描述了利用关系型数据库和文档型数据库作为存储的几种设计模式。 A.关系型

在数据库中存储树形结构的数据,这是一个非常普遍的需求,典型的比如论坛系统的版块关系。在传统的关系型数据库中,就已经产生了各种解决方案。

此文以存储树形结构数据为需求,分别描述了利用关系型数据库和文档型数据库作为存储的几种设计模式。

A.关系型数据库设计模式1

id name parent_id
1 A NULL
2 B 1
3 C 1
4 D 2

上图表示了传统的设计方法之一,就是将树形结构的每一个结点作为关系型数据库中的一行进行存储,每一个结点保存一个其父结点的指针。

优点:结构简单易懂,插入修改操作都很简单

缺点:如果要获取某个结点的所有子结点,将是一件很恶心的事

B.关系型数据库设计模式2

id name parent_id left right
1 A NULL 1 8
2 B 1 2 5
3 C 1 6 7
4 D 2 3 4

上图在模式1的基础上多了两列,left和right,相当于btree中的左右分支,分别存储了左右分支结点的最大值和最小值。

优点:要查找一个结点的子结点很容易,只需要做一个范围查询就行了(比如B节点的子结点,只需要查询 id >=2 && id

缺点:由于树结构存在在这里面了,所以添加或修改已存在结点将可能产生连锁反应,操作过于复杂

C.文档型数据库设计模式1

<span>{</span>
  <span>"name"</span><span>:</span> <span>"A"</span><span>,</span>
  <span>"children"</span><span>:</span> <span>[</span>
    <span>{</span><span>"name"</span><span>:</span> <span>"B"</span><span>,</span> <span>"children"</span><span>:</span> <span>[</span><span>{</span><span>"name"</span><span>:</span> <span>"D"</span><span>}</span><span>]</span><span>}</span><span>,</span>
    <span>{</span><span>"name"</span><span>:</span> <span>"C"</span><span>}</span>
  <span>]</span>

将整个树结构存成一个文档,文档结构既树型结构,简明易懂。

优点:简明易懂

缺点:文档会越来越大,对所有结点的修改都集中到这一个文档中,并发操作受限

D.文档型数据库设计模式2

<span>{</span><span>"_id"</span><span>:</span> <span>"A"</span><span>,</span> <span>"children"</span><span>:</span> <span>[</span><span>"B"</span><span>,</span> <span>"C"</span><span>]</span><span>}</span>
<span>{</span><span>"_id"</span><span>:</span> <span>"B"</span><span>,</span> <span>"children"</span><span>:</span> <span>[</span><span>"D"</span><span>]</span><span>}</span>
<span>{</span><span>"_id"</span><span>:</span> <span>"C"</span><span>}</span>
<span>{</span><span>"_id"</span><span>:</span> <span>"D"</span><span>}</span>
将每个结点的所有子结点存起来

优点:结构简单,查找子结点方便

缺点:查找父结点会比较麻烦

E.文档型数据库设计模式3

<span>{</span>
  <span>"leaf"</span><span>:</span> <span>"A"</span><span>,</span>
  <span>"children"</span><span>:</span> <span>[</span>
    <span>{</span><span>"leaf"</span><span>:</span> <span>"B"</span><span>,</span> <span>"children"</span><span>:</span> <span>[</span><span>{</span><span>"leaf"</span><span>:</span> <span>"D"</span><span>}</span><span>]</span> <span>}</span><span>,</span>
    <span>{</span><span>"leaf"</span><span>:</span> <span>"C"</span><span>}</span>
  <span>]</span>
<span>}</span>
<span>{</span><span>"_id"</span><span>:</span> <span>"A"</span><span>,</span> ...<span>}</span>
<span>{</span><span>"_id"</span><span>:</span> <span>"B"</span><span>,</span> ...<span>}</span>
<span>{</span><span>"_id"</span><span>:</span> <span>"C"</span><span>,</span> ...<span>}</span>
<span>{</span><span>"_id"</span><span>:</span> <span>"D"</span><span>,</span> ...<span>}</span>
充分利用文档型存储schema-less的优点,先利用上面C方案存存储一个大的树形文档,再将每一个结点的其他信息单独存储。

优点:操作方便,结构上的操作可以直接操作大的树形文档,数据上的操作也只需要操作单条数据

缺点:对所有结点的修改都集中到这一个文档中,并发操作受限

Modeling a Tree in a Document Databas

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software