Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
XML In My Database!
1. XML in My Database!
Implementation on a Live e-Store
Using SQL Server 2005
Denise McInerney
Silicon Valley SQL Server User Group
August 19, 2008
2. August 19, 2008 Denise McInerney 2
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Decision to Use XML Data Type
Design Choices
Getting Data In and Out
Impact on Performance & Maintenance
Q & A
3. August 19, 2008 Denise McInerney 3
Introduction
10 years SQL Server DBA
Currently Staff Development DBA
at Intuit
Web-based OLTP applications
Design
Performance Tuning
MCDBA/MCTS
PASS volunteer
Women in Technology SIG Chairperson
4. August 19, 2008 Denise McInerney 4
PASS Community Summit 2008
November 18-21, 2008
Washington State Convention & Trade Center,
Seattle
Over 100 sessions, 4 Tracks
DBA, App Dev, BI, Professional Development
MVPs, Microsoft Development Team
Itzik Ben-Gan, Kalen Delaney, Kimberley Tripp,
Greg Low, Brian Knight, Brad McGehee, Joe Webb,
Kevin Kline
Many Networking Opportunities
Vendor Expo
http://summit2008.sqlpass.org/
5. August 19, 2008 Denise McInerney 5
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Decision to Use XML Data Type
Design Choices
Getting Data In and Out
Impact on Performance & Maintenance
Q & A
6. August 19, 2008 Denise McInerney 6
XML Data Type Overview
Introduced in SQL Server 2005
Allows storage of XML documents in database
Before SS 2005
Store in text columns
Shredded into relational structures
Produced using FOR XML
Define a column or variable as XML
CREATE TABLE T1
(Col1 int, Col2 xml)
Will behave like other data types, mostly
Can query with XML data type methods
Can be validated against an XML schema
7. August 19, 2008 Denise McInerney 7
XML Data Type Overview
XML data type not a replacement for
relational structure
Appropriate for
semi- or unstructured data
containment hierarchy, instead of
references among entities
Large object storage (documents)
Platform independence, data portability
See Books Online for more
8. August 19, 2008 Denise McInerney 8
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Reason for Using XML
Design Choices
Getting Data In and Out
Impact on Performance & Maintenance
Q & A
9. August 19, 2008 Denise McInerney 9
Project Definition
Existing e-commerce system
~4 million orders
Front-end application being completely
rewritten
Opportunity to change the data model
10. August 19, 2008 Denise McInerney 10
Project Definition
What is the data?
Custom printed products
Contents—what is printed
Name, address, etc.
Descriptors—how it’s printed
Location on item, font face, color, size
Every product different
New products introduced regularly
11. August 19, 2008 Denise McInerney 11
Project Definition
Stored in HUGE relational tables
Nested 5 deep
3 sets
1 million – 30 million rows
Ongoing maintenance & performance
problems
Existing design did not scale
Data shared with two external systems
14. August 19, 2008 Denise McInerney 14
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Decision to Use XML Data Type
Design Choices
Getting Data In and Out
Impact on Performance & Maintenance
Q & A
15. August 19, 2008 Denise McInerney 15
Decision to Use XML Data Type
Data seemed like it “wanted” to be in
XML
Data met criteria in Microsoft
recommendation
Hierarchical
Structure changes
Needed to be portable
New application design based on using
XML documents
16. August 19, 2008 Denise McInerney 16
Decision to Use XML Data Type
Convince the rest of the team
Present how it could work
Yes, it can be queried
No, it will not eat up terabytes
Proof-of-concept
Sample XML document in column
Prove the application can use it
17. August 19, 2008 Denise McInerney 17
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Decision to Use XML Data Type
Design Choices
Getting Data In and Out
Impact on Performance & Maintenance
Q & A
18. August 19, 2008 Denise McInerney 18
Design Choices
Define what is in & what is out
Very important choices
XML schema definition determined by needs
of application
Everything else up for discussion
Know your WHEREs
99% of queries based on ID
ID gets its own column
Create & update dates also
Everything else in XML doc/column
19. August 19, 2008 Denise McInerney 19
Design Choices
Redundant data?
In XML column and relational table
Determined to be necessary in one case
Be careful here!
To Bind or Not to Bind?
Decided “no”
Many XML schemas, more to come
Application doing validation
20. August 19, 2008 Denise McInerney 20
Design Choices
Final table had 4 columns
Three tables, one design
One for work-in-progress
One for customer-initiated save
One for final product
“Final” insert validated against XSD
21. August 19, 2008 Denise McInerney 21
Design Choices
Index on XML?
Can improve performance, can be costly
Decided not necessary
All queries first based on PK
Return small number of rows
Less XML to be shredded
22. August 19, 2008 Denise McInerney 22
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Decision to Use XML Data Type
Design Choices
Getting Data In and Out
Impact on Performance & Maintenance
Q & A
23. August 19, 2008 Denise McInerney 23
Getting Data In
For new records, simple insert
Entire XML doc in column
Existing 4 million records, not so simple
Data had to be preserved, available to
customers right away
Some bad data existed
Data conversion biggest challenge
24. August 19, 2008 Denise McInerney 24
Getting Data In
Started with FOR XML query
Tried to create XML doc as defined by
XSD
Table design made FOR XML difficult
FOR XML
Column name = element or attribute name
Data = element or attribute value
In relational tables, 1 column contained
name, 1 column contained value
Painstaking process, slow performance
25. August 19, 2008 Denise McInerney 25
Getting Data In
Realized SQL alone was not right tool
Better answer through collaboration
Used FOR XML to get relational to XML
Used code to transform 1st XML to final
XML document
27. August 19, 2008 Denise McInerney 27
Getting Data In
Lots of trial and error
Broke it into pieces
Bad data
Many different use cases
Phased approach
Did bulk of conversion before launch
Night of launch ~10,000 records to convert
Prepared for problems
Preserved data at every stage of conversion
Needed it when bugs arose
Collaboration with developer & production
DBA crucial to success
28. August 19, 2008 Denise McInerney 28
Getting Data Out
Most very simple SELECT
SELECT xml_col
WHERE id_col = myID
Some use of VALUE method
Retrieves value of element or attribute
Great performance
Fixing bugs
MODIFY method
30. August 19, 2008 Denise McInerney 30
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Decision to Use XML Data Type
Design Choices
Getting Data In and Out
Impact on Performance &
Maintenance
Q & A
31. August 19, 2008 Denise McInerney 31
Impact on Performance &
Maintenance
In production 9 months
Query performance much better
No timeouts
Index maintenance problems
eliminated
Database shrunk
32. August 19, 2008 Denise McInerney 32
Impact on Performance &
Maintenance
BEFORE
Relational Tables + Indexes
77.5 Gigabytes
AFTER
Table with XML column + PK index
20 Gigabytes
74% reduction in used space
33. August 19, 2008 Denise McInerney 33
Agenda
Introduction
XML Native Data Type Overview
Project Definition
Reason for Using XML
Design Choices
Getting Data In and Out
Impact on Performance & Maintenance
Q & A
34. August 19, 2008 Denise McInerney 34
Additional Resources
Books OnLine
http://msdn.microsoft.com/xml/default.aspx?
pull=/library/en-
us/dnsql90/html/sql2k5xml.asp
http://msdn.microsoft.com/xml/default.aspx?
pull=/library/en-
us/dnsql90/html/sql2k5xmloptions.asp
http://msdn.microsoft.com/xml/default.aspx?
pull=/library/en-
us/dnsql90/html/sql25xmlbp.asp
MS Newsgroups, SQLJunkies.com,
SQLTeam.com…and every SQL Server site
Contact me: denise.mcinerney@sqlpass.org