Most efficient method for persisting complex types with variable schemas in SQL

前端 未结 5 1001
旧巷少年郎
旧巷少年郎 2021-01-03 08:21

What I\'m doing

I am creating an SQL table that will provide the back-end storage mechanism for complex-typed objects. I am trying to determine how

5条回答
  •  没有蜡笔的小新
    2021-01-03 08:37

    Somehow what you want sounds like a painful thing to do in SQL. Basically, you should treat the inside of a text field as opaque as when querying an SQL database. Text fields were not made for efficient queries.

    If you just want to store serialized objects in a text field, that is fine. But do not try to build queries that look inside the text field to find objects.

    Your idea sounds like you want to perform some joins, XML parsing, and XPath application to get to a value. This doesn't strike me as the most efficient thing to do.

    So, my advise:

    • Either just store serialized objects in the db, and do nothing more than load them and perform all other operations in memory
    • Or, if you need to query complex data structures, you may really want to look into document stores/databases like CouchDB or MongoDB; you can also check Wikipedia on the subject. There are even databases specifically designed for storing XML, even though I personally don't like them very much.

    Addendum, per your explanations above

    Simply put, don't go over the top with this thing:

    • If you just want to persist C#/.NET objects, just use the XML Serialization already built into the framework, a single table and be done with it.
    • If you, for some reason, need to store complex XML, use a dedicated XML store
    • If you have a fixed database schema, but it is too complex for efficient queries, use a Document Store in memory where you keep a denormalized version of your data for faster queries (or just simplify your database schema)
    • If you don't really need a fixed schema, use just a Document Store, and forget about having any "schema definition" at all

    As for your solution, yes, it could work somehow. As could a plain SQL schema if you set it up right. But for applying an XPath, you'll probably parse the whole XML document each time you access a record, which wouldn't be very efficient to begin with.

    If you want to check out Document databases, there are .NET drivers for CouchDB and MongoDB. The eXist XML database offers a number of Web protocols, and you can probably create a client class easily with VisualStudio's point-and-shoot interface. Or just google for someone who already did.

提交回复
热议问题