Article View: cn.bbs.comp.database
Article #402SQL Server XML������(Best Practices)
From: Galaxy��VOLUNTEERS��Database
Date: Thu, 23 Apr 2009 12:57
Date: Thu, 23 Apr 2009 12:57
168 lines
8776 bytes
8776 bytes
1. XML����
XML�洢�봦���ѯ������ȡ������ݿ�������XML��ݵĽṹ�����ȡ��Ƿ�Ҫʹ��XML
���ģ�ͣ�����
�Dz����а�ṹ������ݣ���Ҫ�����ĵ��ṹ���νṹ�ı��������ݣ���ɱ�Ľ�
����
1.1 ��� Markups
ͬ�����ݿ����Բ�ͬ�ķ�ʽ����ǣ������ݣ�*****ntֵ����Ԫ�����Ϣ��attribute
ֵ����Խ�����
*****nt��Խ�����Ķ����Ҷ������Ч�IJ�ѯ�ƻ��а����߳��ı�ǻ����Ӵ洢�ijɱ�
�����磺
a.
<item type="book"><title>Writing Secure Code</title></item>
<item type="DVD"><title>The Godfather</title></item>
���Ҫ��ѯbook����ҪдΪ��/item[@type = "book"]
b.
<book><title>Writing Secure Code</title></book>
<DVD><title>The Godfather</title></DVD>
���Ҫ��ѯbook��ֻ��Ҫд��/book ������Ч�ʸ��Ҳ������������Ҽ�����prima
ry XML index��
��XML����IJ�������item�㣩������Ǿ����ǣ�Specific Markups�����һ���
�ǣ�Generic
Markups�����ŵ㡣
���ڱ����ͻ��ĵ�XML��typed XML�������������������㣺
<book title="Writing Secure Code"/>
<DVD title="The Godfather"/>
��<DVD><title>The Godfather</title></DVD>���ָ�ʽ����Ϊ*****nt-centric markup
����ѯ��ʽ
Ϊ/DVD[title = "The Godfather"]
��<DVD title="The Godfather"/>���ָ�ʽ��attribute-centric markup����ѯ��ʽΪ/
DVD[@title =
"The Godfather"] �����Լ���һ��JOIN��
1.2 Typed �� Untyped XML�����ͻ�������ͻ���XML��
�����ͻ���XML��untyped XML��û����XML schema����ʾ����SQL Server�ڲ���Unicode
�ַ����ʽ��
���������ǵIJ�����Ҫ���ת������Ӧ�����͡����� (/book/price)[1] > 19.99������
<price>ת��Ϊ
decimal�Ĺ�̡��������ƵıȽϾͻ�dz�����Դ����������XML schema��������Ϣ��
��Ҫ�ԡ�
������Ϣ�����¼������������ã�
a. ������µ�XML����ȱ���֤�Ƿ���schema��Ȼ���Զ�������ʽ�洢���������
ת��
b. ���ͻ���ֵ���洢��XML������
c. ���ͻ�����ݻ����Լ��ٷ�Χɨ�裬����(/book/price)[1]�е�����[1]��XML
schema�涨��<price>
Ϊ��ֵʱ���Dz���Ҫ�ġ�
1.3 ����
����ʹ��UDF���û��Զ��庯������ü����У�computed column���������ڼ�������
����������ڼ�������Ԥ����õģ���ѯ�ٶȸ�졣
����
�鼮����ISBN�ţ���ISBN������Ϊ�����еĹ������
a. �����ȡISBN�ŵĺ���
CREATE FUNCTION udf_get_book_ISBN (@xData xml) RETURNS varchar(20)
WITH SCHEMABINDING
BEGIN
RETURN @xData.value('(/book/@ISBN)[1]', 'varchar(20)')
END
b. ��Ӽ�����
CREATE TABLE docs (id int PRIMARY KEY, xCol XML)
ALTER TABLE docs ADD ISBN AS dbo.udf_get_book_ISBN(xCol)
c. ��ӷǾ�������
CREATE INDEX COMPUTED_IDX ON docs (ISBN)
d. д��ѯ
���ʹ��ISBN�У���ѯ���£�
SELECT xCol
FROM docs
WHERE xCol.exist ('/book/@ISBN[. = "0-2016-3361-2"]') = 1
���ʹ��ISBN��
SELECT xCol
FROM docs
WHERE ISBN = '0-2016-3361-2'
�����Խ������Ա?Property Table��
2. ��������XML���
--����ʹ��BCP IN��BULK INSERT��OPENROWSET��������Ϊ����ε�case�������Թ�
3. XML����
3.1 ��ͨ����
�Ƽ���������XML����primary XML index����ʵ�����Ǵ�����һ��B+���������Զ���P
ATH��PROPERTY��VALUE�����μ�XML����secondary XML indexes����
a. PATH����
������������/book[@ISBN = "0-2016-3361-2"]��·�����ʽ��·��Խ��Խ��Ч
b. PROPERTY����
������XML�еĶ�properties��ѯ
c. VALUE����
�����ڴ������ᣨ//�������ͨ�������(/book[@* = "novel"])���IJ�ѯ
ά������Ŀ���Ҳ����Ҫ���忼�ǵġ�
--����XML���ָ��º���ε�case�������Թ�
--����XMLȫ���������ε�case�������Թ�
4. ��ѯ�Ż�
4.1 ʹ��exist()����������Ƿ����
������ʹ��exist()������value()
�������µIJ�ѯ��
SELECT *
FROM docs
WHERE xCol.exist('(/book/title/text())[.="Writing Secure Code"]') = 1
��
SELECT *
FROM docs
WHERE xCol.value('(/book/title)[1]', 'varchar(50)') = 'Writing Secure Code'
4.2 ��XML blobs��������XML��
����tempDB�ļ������ṩ��õĿ���չ�ԡ�
���ٶ����XML�������ת��
4.3 ָ��Singleton Elements������Ԫ�أ�
�������ͻ���XML��Ĭ����singleton��
�����ͻ���XML����Ҫ��[1]��xCol.query ('/book/title')�ᱻ�Զ�ת��ΪxCol.query
('(/book/title)[1]')��
4.4 ���ڷ����ͻ���XML��text�ۺ�
(/book/title[.="Writing Secure Code"])[1]��Ҫ�ۺ�<title>�����е�text�ڵ�
���<title> *****ntֻ��һ��text�ڵ㣬��ô����ʹ��(/book/title/text())[1][.
= "Writing Secure Code"]������
4.5 ��XQuery��XML DML���ʽ����
XQuery��XML DML�����Զ��������ʹ��sql:column() ��sql:variable() ��
4.6 �����Ԥ�Ƶ��Ż�
�������ķ�֧����/book[@ISBN = "1-8610-0157-6"]/author/first-name�����ʹ��/
book[author/first-name = "Davis"]����ĸ�ʽ��
�������Ƶ�·�������/book[1]/title[1]��ͬ�� (/book/title)[1]
ʹ��Context Node�������Ľڵ㣩
���µķ���
SELECT *
FROM docs
WHERE xCol.exist ('/book[@subject = "security"]') = 1
�ֱ��/book��/book/@subject������������������˺����Ƿ����ֵ��security��
���ʹ�����±�
SELECT *
FROM docs
WHERE xCol.exist ('/book/@subject[. = "security"]') = 1
ֻ���/book/@subject ��������
��̬��ѯ�뱾�ε�case�������Թ�
���尸������������case����ϸ������
�ο����ϣ�
Performance Optimizations for the XML Data Type in SQL Server 2005
http://technet.microsoft.com/en-us/library/ms345118.aspx
XML Best Practices for Microsoft SQL Server 2005
http://technet.microsoft.com/en-us/library/ms345115.aspx
--
Welcome to Database Board
��ӭ������ݿ��
http://bbs.sjtu.edu.cn/bbsdoc?board=database
[m[1;32m�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 207.46.55.30][m
Technical Details
Message-ID:
<4fWdZL$b4Z@bbs.sjtu.edu.cn>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!news2.euro.net!feeds.phibee-telecom.net!news.byr.edu.cn!news.shec6.edu.cn!SJTUShow Full Headers
Complete Article Headers
X-Google-Thread: 101948,2219ed5556d01120,start X-Google-Attributes: gid101948,domainid0,public,usenet X-Google-Language: Chinese,GB Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!news2.euro.net!feeds.phibee-telecom.net!news.byr.edu.cn!news.shec6.edu.cn!SJTU From: Dem.bbs@bbs.sjtu.edu.cn (Galaxy��VOLUNTEERS��Database) Newsgroups: cn.bbs.comp.database Subject: SQL Server XML�����Ż�(Best Practices) Date: 23 Apr 2009 12:57:17 GMT Organization: ��ˮ˼Դ Message-ID: <4fWdZL$b4Z@bbs.sjtu.edu.cn> Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 8bit X-Filename: database/M.1240491429.A