以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML 与 数据库 』  (http://bbs.xml.org.cn/list.asp?boardid=17)
----  [讨论]let子句在SQLServer2005中的等效问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=17&rootid=&id=47302)


--  作者:蒙面的羔羊
--  发布时间:5/22/2007 10:21:00 AM

--  [讨论]let子句在SQLServer2005中的等效问题
hi :)

因为SS2005不支持let子句, 在改写有let语句的查询时遇到一些问题,想看看大家有什么好的解决.

我查了msdn, 这个帖子能解决一些问题:
If the LET expression does not contain any construction, then replace all occurrences of the LET variable with that expression. This workaround will evaluate the expression each time and have some performance impact. If the expression yields a scalar value, you can use sql:variable() to compute it once outside the XQuery and use it in XQuery. A similar use is shown in http://blogs.msdn.com/spal/archive/2005/04/06/405943.aspx.

If the LET expression contains a construction, then you need to break up the query and do it partly in Transact-SQL and partly using XQuery. If you send me your query, I (or someone from our team) can help with the rewrite.


但对于下面这个改写,一直找不到好的方法:
原查询:
for $p in /site/people/person
let $l := for $i in /site/open_auctions/open_auction/initial
where $p/profile/@income > (5000 * $i/text())
return $i
return <items name=$p/profile/@income> count ($l) </items>

改写后的查询:
for $p in /site/people/person,
$i in /site/open_auctions/open_auction/initial
where ($p/profile/@income)[1] ge (5000 * ($i/text())[1])
return <items name="{$p/profile/@income}">{count(/site/open_auctions/open_auction/initial[text()[1] le ($p/profile/@income)[1]])}</items>

原查询中用let定义了一个序列$l ,其中包含在值连接
where $p/profile/@income > (5000 * $i/text())
return $i
中得到的$i节点集合,对每个$p只需要做1次这种值连接,连接得到的集合可以保存起来,供后续查询使用。

改写后,因为无法使用let赋值,只有在count中再次使用连接,而不能直接使用刚才得到的结果
(/site/open_auctions/open_auction/initial[ text()[1] le ($p/profile/@income)[1]] )

而这类连接是非常消耗时间的,我在10M的数据上测试,上面的查询花费了19分钟,

现在的问题是,有什么办法可以避免这种重复计算,谢谢 :)


--  作者:蒙面的羔羊
--  发布时间:5/22/2007 11:42:00 AM

--  
另一个关于重构的问题:

原查询:
Q10:根据其兴趣分组列出所有的人员,标签使用法语.
FOR $i IN DISTINCT
/site/people/person/profile/interest/@category
LET $p := FOR $t IN /site/people/person
WHERE $t/profile/interest/@category = $i
RETURN <personne>
<statistiques>
<sexe> $t/gender/text() </sexe>,
<age> $t/age/text() </age>,
<education> $t/education/text()</education>,
<revenu> $t/income/text() </revenu>
</statistiques>,
<coordonnees>
<nom> $t/name/text() </nom>,
<rue> $t/street/text() </rue>,
<ville> $t/city/text() </ville>,
<pays> $t/country/text() </pays>,
<reseau>
<courrier> $t/email/text() </courrier>,
<pagePerso> $t/homepage/text()</pagePerso>
</reseau>,
</coordonnees>
<cartePaiement> $t/creditcard/text()</cartePaiement>
</personne>

RETURN
<categorie>
<id> $i </id>,
$p
</categorie>

注意,这里把序列$p放在最后返回的结果<categorie>中了,如果改写成

'
for $i in distinct-values(/site/people/person/profile/interest/@category),
$t in /site/people/person
return
<categorie><id>{$i}</id> <personne> <statistiques> <sexe>{$t/gender/text()}</sexe>, <age>{$t/age/text()}</age>, <education>{$t/education/text()}</education>, <revenu>{$t/income/text()}</revenu> </statistiques>, <coordonnees> <nom>{$t/name/text()}</nom>, <rue>{$t/street/text()}</rue>, <ville>{$t/city/text()}</ville>, <pays>{$t/country/text()}</pays>, <reseau> <courrier>{$t/email/text()}</courrier>, <pageperso>{$t/homepage/text()}</pageperso> </reseau>, </coordonnees> <cartepaiement>{$t/creditcard/text()}</cartepaiement> </personne> </categorie> '

则查询得到的结果类似下面的情况:

categorie>
  <id>category38</id>
  <personne>
    <statistiques>
      <sexe />, <age />, <education />, <revenu /></statistiques>, <coordonnees><nom>Torkel Prodrodmidis</nom>, <rue />, <ville />, <pays />, <reseau><courrier />, <pageperso /></reseau>, </coordonnees><cartepaiement>5144 2585 6148 3007</cartepaiement></personne>
</categorie>
<categorie>
  <id>category38</id>
  <personne>
    <statistiques>
      <sexe />, <age />, <education />, <revenu /></statistiques>, <coordonnees><nom>Gul Prodrodmidis</nom>, <rue />, <ville />, <pays />, <reseau><courrier />, <pageperso /></reseau>, </coordonnees><cartepaiement /></personne>
</categorie>
<categorie>
  <id>category38</id>
  <personne>
    <statistiques>
      <sexe />, <age />, <education />, <revenu /></statistiques>, <coordonnees><nom>Theron Aseltine</nom>, <rue />, <ville />, <pays />, <reseau><courrier />, <pageperso /></reseau>, </coordonnees><cartepaiement>6099 6159 7416 8032</cartepaiement></personne>
</categorie>
<categorie>
  <id>category38</id>
  <personne>
    <statistiques>
      <sexe />, <age />, <education />, <revenu /></statistiques>, <coordonnees><nom>Guillermo Litecky</nom>, <rue />, <ville />, <pays />, <reseau><courrier />, <pageperso>http://www.stanford.edu/~Litecky</pageperso></reseau>, </coordonnees><cartepaiement>1437 9349 4810 4089</cartepaiement></personne>
</categorie>

这也和原查询的意图不同


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
31.250ms