<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      SQL點滴10—使用with語句來寫一個稍微復雜sql語句,附加和子查詢的性能對比

       今天偶爾看到sql中也有with關鍵字,好歹也寫了幾年的sql語句,居然第一次接觸,無知啊。看了一位博主的文章,自己添加了一些內容,做了簡單的總結,這個語句還是第一次見到,學習了。我從簡單到復雜地寫,希望高手們不要見笑。下面的sql語句設計到三個表,表的內容我用txt文件復制進去,這里不妨使用上一個隨筆介紹的建立端到端的package的方法將這些表導入到數據庫中,具體的就不說了。

      從這里下載文件employees.txt,customers.txt,orders.txt

      參考文章:http://www.rzrgm.cn/wwan/archive/2011/02/24/1964279.html

      使用package導入數據:http://www.rzrgm.cn/tylerdonet/archive/2011/04/17/2017471.html

      簡單的聚合

      從orders表中選擇各個年份共有共有多少客戶訂購了商品

      •  第一種寫法,我們可以寫成這樣
        1select YEAR(o.orderdate) orderyear,COUNT(distinct(custid)) numCusts
        2from Sales.Orders o
        3group by YEAR(o.orderdate)
        4go
        要注意的是如果把group by YEAR(o.orderdata)換成group by orderyear就會出錯,這里涉及到sql語句的執行順序問題,有時間再了解一下          
      • 第二種寫法,
        1select orderyear,COUNT(distinct(custid))numCusts
        2from (select YEAR(orderdate) as orderyear,custid from sales.orders) as D
        3group by orderyear
        4go
        在from語句中先得到orderyear,然后再select語句中就不會出現沒有這個字段的錯誤了
      • 第三種寫法,
        1select orderyear,COUNT(distinct(custid)) numCusts
        2from (select YEAR(orderdate),custid from sales.orders) as D(orderyear,custid)
        3group by orderyear
        4go
        在as D后面加上選擇出的字段,是不是更加的清楚明了呢!
      • 第四種寫法,with出場了
        1with c as(
        2select YEAR(orderdate) orderyear, custid from sales.orders)
        3select orderyear,COUNT(distinct(custid)) numCusts from c group by orderyear
        4go
        with可以使語句更加的經湊,下面是權威解釋。  
            
        指定臨時命名的結果集,這些結果集稱為公用表表達式 (CTE)。該表達式源自簡單查詢,并且在單條 SELECT、INSERT、UPDATE、MERGE 或 DELETE 語句的執行范圍內定義。該子句也可用在 CREATE VIEW 語句中,作為該語句的 SELECT 定義語句的一部分。公用表表達式可以包括對自身的引用。這種表達式稱為遞歸公用表達式。               

                                                      ----MSDN

      • 第五種寫法,也可以借鑒第三種寫法,這樣使語句更加清楚明了,便于維護
        1 with c(orderyear,custid) as(
        2  select YEAR(orderdate),custid from sales.orders)
        3  select orderyear,COUNT(distinct(custid)) numCusts from c group by c.orderyear
        4  go
        上面5中寫法都得到相同的結果,如下圖1:圖1

      添加計算

      • 現在要求要求計算出訂單表中每年比上一年增加的客戶數目,這個稍微復雜
        1 with yearcount as(
        2  select YEAR(orderdate) orderyear,COUNT(distinct(custid)) numCusts from sales.orders group by YEAR(orderdate))
        3  select cur.orderyear curyear,cur.numCusts curNumCusts,prv.orderyear prvyear,prv.numCusts prvNumCusts,cur.numCusts-prv.numCusts growth
        4  from yearcount cur left join yearcount prv on cur.orderyear=prv.orderyear+1
        5 go
        這里兩次使用到with結果集。查詢得到的結果如下圖2

        圖2

      復雜的計算

      • 查找客戶id,這些客戶和所有來自美國的雇員至少有一筆交易記錄,查詢語句如下
        1 with TheseEmployees as(
        2 select empid from hr.employees where country='USA'),
        3 CharacteristicFunctions as(
        4 select custid,
        5 case when custid in (select custid from sales.orders as o where o.empid=e.empid) then 1 else 0 end as charfun
        6 from sales.customers as c cross join TheseEmployees as e)
        7 select custid,min(charfun) from CharacteristicFunctions group by custid having min(charfun)=1
        8 go
        這里嵌套with語句,第with語句查找美國雇員的id,第二個語句使用這個結果和擁有客戶的客戶id和擁有關系標識做笛卡爾積運算。最后從這個笛卡爾積中通過標識找到最終的custid。
        結果如下圖3

        圖3

      這里只有簡單地介紹,沒有深入,高手們不要見笑啊。

      ?

      ---------------------------------------------------------分界線----------------------------------------------------------

      with語句和子查詢的性能比較

      在博友SingleCat的提醒下,對with語句做一些性能測試,這里使用的測試工具是SQL Server Profile。我選擇了最后一個語句,因為這個語句比較復雜一點。開始的時候單獨執行一次發現他們的差別不大,就差幾個毫秒,后來想讓他們多執行幾次,連續執行10

      次看看執行的結果。下面貼出測試用的語句。

      1 /*with查詢*/
      2 declare @withquery varchar(5000)
      3 declare @execcount int=0
      4 set @withquery='with TheseEmployees as(
      5 select empid from hr.employees where country=N''USA''),
      6 CharacteristicFunctions as(
      7 select custid,
      8 case when custid in (select custid from sales.orders as o where o.empid=e.empid) then 1 else 0 end as charfun
      9 from sales.customers as c cross join TheseEmployees as e)
      10 select custid from CharacteristicFunctions group by custid having min(charfun)=1 order by custid
      11 '
      12 while @execcount<10
      13 begin
      14 exec (@withquery);
      15 set @execcount=@execcount+1
      16 end
      17
      18 /*子查詢*/
      19 declare @subquery varchar(5000)
      20 declare @execcount int=0
      21 set @subquery='select custid from Sales.Orders where empid in
      22 (select empid from HR.Employees where country = N''USA'') group by custid
      23 having count(distinct empid)=(select count(*) from HR.Employees where country = N''USA'');
      24 '
      25 while @execcount<10
      26 begin
      27 exec (@subquery);
      28 set @execcount=@execcount+1
      29 end

      SQL Server Profile中截圖如下

      從圖中可以看到子查詢語句的執行時間要少于with語句,我覺得主要是with查詢中有一個cross join做了笛卡爾積的關系,于是又實驗了上面的那個簡單一點的,下面是測試語句。

      1 /*with語句*/
      2 declare @withquery varchar(5000)
      3 declare @execcount int=0
      4 set @withquery='with c(orderyear,custid) as(
      5 select YEAR(orderdate),custid from sales.orders)
      6 select orderyear,COUNT(distinct(custid)) numCusts from c group by c.orderyear'
      7 while @execcount<100
      8 begin
      9 exec (@withquery);
      10 set @execcount=@execcount+1
      11 end
      12
      13 /*子查詢*/
      14 declare @subquery varchar(5000)
      15 declare @execcount int=0
      16 set @subquery='select orderyear,COUNT(distinct(custid)) numCusts
      17 from (select YEAR(orderdate),custid from sales.orders) as D(orderyear,custid)
      18 group by orderyear'
      19 while @execcount<100
      20 begin
      21 exec (@subquery);
      22 set @execcount=@execcount+1
      23 end

       

      這次做10次查詢還是沒有多大的差距,with語句用10個duration,子查詢用了11個,有時候還會翻過來。于是把執行次數改成100,這次還是子查詢使用的時間要少,截圖如下

      最終結論,子查詢好比with語句效率高。

      posted @ 2011-04-18 21:35  nd  閱讀(19170)  評論(19)    收藏  舉報
      主站蜘蛛池模板: 欧美乱大交xxxxx疯狂俱乐部| 老师破女学生处特级毛ooo片| 在线一区二区中文字幕| 在线中文字幕亚洲日韩2020| 精品视频福利| 中文字幕有码日韩精品| 国产人妻大战黑人第1集| 蜜臀av入口一区二区三区| 97超级碰碰碰久久久久| 亚洲线精品一区二区三八戒| 日韩精品 在线 国产 丝袜| 亚洲小说乱欧美另类| 欧美激情内射喷水高潮| av新版天堂在线观看| 蜜桃成熟色综合久久av| 中文幕无线码中文字夫妻| 国产成人无码A区在线观看视频| 国产精品一区二区无线| 人妻伦理在线一二三区| 十八岁污网站在线观看| аⅴ天堂中文在线网| 色翁荡熄又大又硬又粗又视频| 日韩精品理论片一区二区| 宝贝腿开大点我添添公口述视频| 亚洲中文无码永久免费| 成年在线观看免费人视频| 亚洲自拍偷拍激情视频| 国产成人精品无码免费看| 丰满人妻无码∧v区视频 | 国产极品美女网站在线观看| 国产旡码高清一区二区三区| 欧美综合婷婷欧美综合五月 | 九九成人免费视频| 亚洲国产中文字幕在线视频综合 | 免费观看欧美猛交视频黑人| 欧产日产国产精品精品| 无码国产偷倩在线播放老年人| 2020国产成人精品视频| 熟妇人妻一区二区三区四区| 国产日女人视频在线观看| 亚洲乱码av中文一区二区|