简体   繁体   English

数据库字段中的XMLSerialized对象。 这是好的设计吗?

[英]XMLSerialized Object in Database Field. Is it good design?

Suppose i have one table that holds Blogs. 假设我有一个保存Blogs的表。 The schema looks like : 模式如下所示:

ID (int)| Title (varchar 50) | Value (longtext) | Images (longtext)| ....

In the field Images i store an XML Serialized List of images that are associated with the blog. 在“图像我”字段中,存储与博客关联的图像的XML序列化列表。

Should i use another table for this purpose? 我是否应该为此使用另一个表?

Yes, you should put the images in another table. 是的,您应该将图像放在另一个表中。 Having several values in the same field indicates denormalized data and makes it hard to work with the database. 在同一字段中具有多个值表示数据已规范化,并使其难以使用数据库。

As with all rules, there are exceptions where it makes sense to put XML with multiple values in one field in the database. 与所有规则一样,在将多个值的XML放在数据库的一个字段中有意义的情况下也有例外。 The first rule is that: 第一条规则是:

The data should always read/written together. 数据应始终一起读取/写入。 No need to read or update just one of the values. 无需读取或更新其中一个值。

If that is fulfilled, there can be a number of reasons to put the data together in one field: 如果实现了这一点,则可能出于多种原因将数据放在一个字段中:

  • Storage efficiency, if space has proved to be a problem. 如果事实证明空间不足,则会提高存储效率。
  • Retrieval efficiency, if performance has proved to be a problem. 如果已证明性能存在问题,则检索效率。
  • Schema flexilibity; 模式灵活性; where one XML field can eliminate tens or hundreds of different tables. 一个XML字段可以消除数十个或数百个不同的表。

I would certainly use another table. 我当然会使用另一个表。 If you use XML, what happens when you need to go through and update the references to all images? 如果使用XML,当您需要遍历并更新对所有图像的引用时会发生什么? (Would you just rather do an Update blog_images Set ... , or parse through the XML for each row, make the update, then re-generate the updated XML for each? (您只是愿意执行Update blog_images Set ... ,还是解析每行的XML,进行更新,然后为每行重新生成更新的XML?

Well, it is a bit "inner platform", but it will work. 好吧,它有点“内部平台”,但是可以工作。 A separate table would allow better image querying, although on some RDBMS platforms this could also be achieved via an XML-type column and SQL/XML. 单独的表将允许更好的图像查询,尽管在某些RDBMS平台上,这也可以通过XML类型的列和SQL / XML来实现。

If this data only has to be opaque storage, then maybe. 如果此数据仅必须是不透明存储,则可能是。 However, keep in mind you'll generally have to bring back the entire XML to the app-tier to do anything interesting with it (or: depending on platform, use SQL/XML, but I advise against this, as the DB isn't the place to do such processing in most cases). 但是,请记住,通常必须将整个XML带回应用程序层才能对其执行任何有趣的操作(或:根据平台,使用SQL / XML,但我建议不要这样做,因为数据库不是“在大多数情况下是进行此类处理的地方)。

My advice in all other cases: separate table. 我在所有其他情况下的建议:单独的表。

That depends on whether you'd need to query on the actual image data itself. 这取决于您是否需要查询实际的图像数据本身。 If you see a possible need to query on certain images, or images with certain attributes, then it would probably be best to store that image data in a different way. 如果您可能需要查询某些图像或具有某些属性的图像,那么最好以其他方式存储该图像数据。

Otherwise, leave it the way it is. 否则,请保持原样。

But remember, only include the fields in your SELECT when you need them. 但是请记住,仅在需要时在SELECT中包括这些字段。

Should i use another table for this purpose? 我是否应该为此使用另一个表?

Not necessarily. 不必要。 You just have to ensure that you are not selecting the images field in your queries when you don't need it. 您只需要确保不需要时不在查询中选择“图像”字段即可。 But if you wanted to denormalize your schema you could use another table and when you need the images perform a join . 但是,如果要对模式进行非规范化,则可以使用另一个表,并在需要图像时执行join

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

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