Share via


Using fixed values does not result in smaller instances...

XML Schema has an interesting feature where you can define a value as being "fixed".  The meaning here is basically that an attribute or element has to have a particular value, and if it is not there in the instance, then that value is implied.  However, it is important to realize that this doesn't have any effect on the storage size of the instance in SQL Server 2005.  The reason is fairly straightforward when you understand our processing model.  When XML comes in, we process it into the XML Infoset data model and apply validation.  The output of our internal XML Schema validator is what is referred to as the "post schema validation infoset".  This PSVI has the fixed values actually in the document as data, and not simply as metadata.  When we store typed XML, we actually store a serialized representation of this PSVI, so your fixed data is still being written to disk and indexed as if it was in the original document. 

Now, you could argue that perhaps we could have saved a couple of bytes by not storing the fixed values, and instead reading them out of metadata when we need them.  You are right, we probably could have saved a couple of bytes.  However, this would result in much more complicated runtime processing as we would have to account for these fixed values and retrieve them from metadata all over the place.  In the end, we decided that having a more straightforward and robust processing model was more important than saving the couple of bytes, especially considering that many users will not take advantage of fixed values in their XML Schemas.

While I am at it, I might as well say that the same rule applies to default values as well.  As part of validation, if the value is not there and a default value is specified, we simply cook up default value and inject it into the instance.