简体   繁体   English

在数据库中将数据另存为xml与保存多行数据

[英]Save data as xml in DB versus Saving multiple rows of data

I am designing my database and wanted to know the best way to handle this problem. 我正在设计数据库,并想知道处理此问题的最佳方法。 I have tabs on a page that looks like this: 我的页面上的标签如下所示:

Tab1
--SubTab1
----Data1
--SubTab1
--SubTab1
Tab2
--SubTab1
--SubTab1
--SubTab1
Tab3
--SubTab1
--SubTab1
--SubTab1

I can put this information in the database that will be stored in multiple rows like this 我可以将此信息放入数据库中,并将其存储在这样的多行中

TypeID---------------Name
1--------------------Tab
2--------------------Data


ObjectID----------Parent-------------TypeID
1-----------------0------------------1
2-----------------0------------------1
3-----------------0------------------1
4-----------------1------------------1

Or I can just put this in the database like so: 或者我可以像这样将其放入数据库中:

<root>
     <tab name="MyTab">
          <tab name="MySubTab">
               <data>1234567890</data>
          </tab>
     </tab>
</root>

If I pull just the xml from the database then I would not need to select multiple rows i just need to select one row then parse the xml into a class then pass that data to the controller. 如果我仅从数据库中提取xml,那么我就不需要选择多行,我只需要选择一行,然后将xml解析为一个类,然后将该数据传递给控制器​​即可。 I just would like to know if this is a good idea? 我只是想知道这是否是个好主意? Will I be making a mistake if my site to scale in the future? 如果将来要扩展我的网站,我会犯错吗? Will this make more maintenance as the site gets bigger and want more features? 随着站点变大并需要更多功能,这会带来更多维护吗?

XML is hard to query using SQL. 使用SQL很难查询XML。 If you think that you will never need to query the data you could use XML. 如果您认为永远不需要查询数据,则可以使用XML。 I'd probably store the data in multiple rows for future flexiblity. 为了将来的灵活性,我可能会将数据存储在多行中。

If your data is one big lump of xml you can't use the database to easily search that data. 如果您的数据是xml的一大块,则不能使用数据库轻松地搜索该数据。 Do you need to search for information in the xml? 您是否需要在xml中搜索信息? Separating it will make that task easier. 分开它将使该任务更加容易。

Maybe I misunderstood it but it seems like you are deciding whether to save the data as XML or save it in tables then query it using xml to return it. 也许我误解了它,但是似乎您正在决定是将数据另存为XML还是将其保存在表中,然后使用xml查询以返回数据。 If that's the case: 如果是这样的话:

Easier to query and can handle more changes: Saving the data to tables and querying it later is better if you will have many changes and it will grow at a high rate. 更容易查询并可以处理更多更改:将数据保存到表中并在以后查询是更好的选择,前提是您要进行很多更改并且数据将以很高的速度增长。 You get more choices for complex querying. 对于复杂的查询,您有更多选择。

Faster if data doesn't grow or change that much: If the data wont change that often and there is no danger of it growing as much then you can simply use XML to keep it in the right syntax already and it will have less of a performance hit. 如果数据增长或变化不那么快,则速度更快:如果数据不会频繁变化,并且没有增长的危险,那么您只需使用XML就可以使它保持正确的语法,那么它就少了性能受到打击。 This method means that you have less flexibility in querying for the right data. 此方法意味着您在查询正确的数据时灵活性较低。

If you are considering whether to store XML on the database it is time to start asking yourself whether storing this data on a database is even necessary. 如果您正在考虑是否在数据库上存储XML,那么是时候开始问自己,是否甚至有必要在数据库上存储此数据。

What about storing this data on a static XML file? 如何将此数据存储在静态XML文件中呢? Is it a viable option? 这是可行的选择吗? Why and why not? 为什么和为什么不呢?

If this data does not change frequently and you do not want to invest the trip to the database, but you still want to make sure it is possible to edit this data in the future using a CMS, then it is also possible to create a batch process that creates the static XML file using the data in your database and then your application would completely ignore the fact that this data is in the database and just use the static XML file. 如果此数据不经常更改,并且您不想投资于数据库,但仍希望确保将来可以使用CMS编辑此数据,那么也可以创建批处理使用数据库中的数据创建静态XML文件的过程,然后您的应用程序将完全忽略此数据在数据库中的事实,而仅使用静态XML文件。 The batch process would run on a schedule or on-demand. 批处理将按计划运行或按需运行。

This is a scalable, maintainable and efficient way to approach this issue for data that doesn't change often. 这是一种可伸缩,可维护且有效的方法,可以解决不经常更改的数据的问题。

A similar way to achieve the same is having a server-side technology like ASP.net or PHP generate the XML for you from the database tables and then using output caching with a long expiration time to make sure that the generation process does not run often. 一种类似的实现方法是,使用ASP.net或PHP之类的服务器端技术从数据库表中为您生成XML,然后使用具有较长到期时间的输出缓存来确保生成过程不会经常运行。

As a general rule of thumb, it is usually not a good idea to store non-primitive or complex data on a database field. 一般而言,在数据库字段上存储非原始或复杂数据通常不是一个好主意。

If using SQL Server, I would just use hierarchyid . 如果使用SQL Server,我将只使用architectureid While this is possible to do with XML in TSQL, there are several caveats with it. 尽管在TSQL中使用XML可以做到这一点,但有一些警告。 The foremost is getting ordered XML back sucks because TSQL (2008) does not correctly/entirely support position() . 最重要的是由于TSQL(2008)不正确/完全不支持position() 使有序XML回抽。 It requires a somewhat messy join to an 'indexer' column. 它要求到“索引器”列的连接有些混乱。 (This doesn't apply if you read the entire XML blob back, but this just sounds like a head-ache to parse manually client-side). (如果您回读了整个 XML Blob,则不适用,但这听起来像是手动解析客户端的头痛)。

hiearchyid allows these levels to be saved easily, leaves the database in a more normalized format (RDBMS' are designed to work efficiently over many columns, just make sure to have the correct indices), and should be much easier to deal with in general -- esp. hiearchyid允许轻松保存这些级别,以更规范的格式保留数据库(RDBMS被设计为可在许多列上有效地工作,只需确保具有正确的索引),并且通常应该更容易处理- -尤其是 if you are not ready to dive into SPROC land :-) 如果您尚未准备好进入SPROC土地:-)

hiearchyid represents parent/child relationships as well as ordering of siblings (it can really tidy up the model); hiearchyid代表父母/孩子的关系以及兄弟姐妹的顺序 (它可以真正整理模型); it is based on a depth-first model that works well for tasks like this. 它基于深度优先模型,可以很好地完成此类任务。

Also, using XML will likely save nothing (IIRC, [typed] XML is actually exploded into some kind of internal column setup). 同样,使用XML可能不会节省任何费用(IIRC,[键入] XML实际上已分解为某种内部列设置)。 It (XML) is much better just to use as a "in/out" message format (or "document"), if required. 如果需要,它(XML)更好地用作“输入/输出”消息格式(或“文档”)。

Happy SQL'ing. 快乐的SQLing。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM