日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQL查询与优化

發布時間:2025/3/21 数据库 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL查询与优化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實驗案例一:比較不同查詢語句使用索引的情況<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Adventrueworks數據庫的production.workorder表中分別按照要求創建相應的索引,然后執行不同的查詢語句,比較使用索引的狀態,并提供相應的截圖和步驟,在實驗過程中,需要同步完成題目中所提出的問題。

推薦步驟:

(1)??? 創建非聚集、非唯一值索引

IX_startdate(startdate)

IX_workorder_productid(productid) (2)創建索引IX_startdate(startdate,enddate),請問有什么現象,為什么? (3)創建索引IX_workorder_productid(productid)請問有什么現象,為什么? 將會出現如下現象,因為已經有索引IX_startdate,重名了。 (4)創建索引IX_workorder_productid2(productid) ? 5)截圖select*from production.workorder where productid=732的執行計劃,寫明該查詢語句使用了哪個索引? ? 由下圖可以看出該查詢語句使用了productid 6)截圖select*from production.workorder where productid>732的執行計劃,寫明該查詢語句使用了哪個索引?為什么與步驟5執行的結果不同? 由下圖可以看出,執行該語句將使用索引掃描,不再使用索引查詢,因為>屬于運算符,查詢部會使用索引 7)使用索引提示,強制select*from production.workorder where productid=732使用索引IX_workorder_productid2,執行計劃是什么樣的? 由下圖可以看出,執行該語句后將使用IX_workorder_productid2索引 8)禁止索引IX_workorder_productid,查看select*from production.workorder where productid=732的執行計劃,寫明該查詢語句使用了哪個索引? <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /> 由下圖可以看出,執行該語句后將使用IX_workorder-productid索引 9)禁止索引XI_workorder_productid,并且使用索引提示,強制查詢select*from production.workorder where productid=732,使用索引IX_workorder_productid,請問有什么現象? 由下圖看出,執行該語句后,將使用IX_workorder_productid2索引 實驗案例二:分析查詢語句使用索引的情況

Production.WorkOrder表創建索引,索引名分別為:

IX_WorkOrder_ProductIDStartdateEnddate(ProductID,StartDate,EndDate)

XI_Workorder_StartdateEnddate(Startdate,Enddate)

執行查詢語句,分析各個查詢語句的使用索引情況,并說明為什么(要求有執行計劃的截圖)

>查詢1.select*from production.workorder

Where startdate>cast(‘<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />2001-07-12’ as datetime)

And startdate<cast(‘2001-07-14’ as datetime)

>查詢2.select*from production.workorder

Where startdate>cast(‘2001-07-12’ as datetime)

And startdate<cast(‘2001-07-14’ as datetime)

And productid=717

>查詢3.select*from production.workorder

Where startdate>cast(‘2001-07-12’ as datetime)

And startdate<cast(‘2001-07-14’ as datetime)

And productid>717

推薦步驟:

(1)??? 首先使用smss為數據庫中的production.workorder表創建索引

(2)??? 然后在smss的任務欄上啟用預計的執行計劃

(3)??? 分別在查詢分析器窗口中輸入上面提供的語句并執行

(4)??? 通過“執行計劃”窗口分別查看索引的使用情況

(5)??? 將三組語句的執行結果進行截圖并進行比較

(6)??? 提交截圖、執行過程和分析結果

實驗案例三:分析創建索引表后查詢計劃的變化

執行下面的語句時,因為where的條件語句應用到了運算符,所以不會引用索引,將執行索引掃描

productID移動到最上面,因為索引首先執行的是where后的條件語句,因此就使用了startdate索引

?

?

如果將startdate上移到最上面,那么在執行完where后的條件語句,將執行select后面的語句,因此將會使用XI_Work

Order_StartdateEnddateProductID索引:

轉載于:https://blog.51cto.com/caohongjiang/314702

總結

以上是生活随笔為你收集整理的SQL查询与优化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。