2012-08-02 53 views
0

所以我知道基本的SQL,但我不是專家。我維護的系統似乎每隔幾天就會放慢一點,並且我認爲它是來自某個地方的錯誤查詢。SQL查詢優化 - 降低服務器效率

從我所知道的情況來看,我已經將問題縮小到了一兩頁。以下是我認爲導致問題的頁面上的查詢。

select a.s_purchase_order as order_id, a.order_type, a.nobackorder, a.order_note, a.note, a.rqst_dlvry_date, b.customer_name ,c.store_name,(c.store_name + ',' + isnull(c.address1 + ',', ' ') + isnull(c.city + ',', ' ') + isnull(c.state_cd+ ',', ' ') + isnull(c.zipcode, ' ')) as store_info, d.supplier_account 
from VW_CustomerOrder a, Customer b, Store c, eligible_supplier d 
where a.customer = c.customer 
and a.store = c.store 
and a.customer = b.customer 
and c.customer *= d.customer 
and c.store *= d.store 
and a.supplier *= d.supplier 
and a.purchase_order = @order_id 
and a.customer = @customer_id 
and [email protected]_id 
and a.supplier = @supplier_id 

是否有明顯的東西會很慢或導致系統隨着時間變慢?

如果沒有關於表格的更多信息,您甚至可能無法回答。如果是的話,讓我知道,我可以張貼更多的細節。

謝謝!

+0

做你的加入在FROM子句(如下面的答案)不在where子句中,它提高了可讀性。另外,你使用的是什麼DBMS? MySQL的? SQL Server?其他? – Ghost 2012-08-02 17:23:12

+0

@Yaroslav不要將自己的語法偏好強加於他人。您的大寫SQL關鍵字不比小寫關鍵字更正確,並且它們看起來(在我看來)是可怕的。 – meagar 2012-08-02 18:53:44

+0

謝謝你們,這是一個好主意,但我現在得到一個錯誤。感謝您的幫助 – fullOfQuestions 2012-08-02 19:40:50

回答

1

怎麼樣做一些內部連接到解決這個問題,請檢查你的基地,看指數和外鍵的表,這始終是querys有益的,性能

 
select 
    a.columun_a, b.column_a from table_a a 
inner join table_b b on a.id = b.id 
where 
    b.column_b = "some value" 
+0

這似乎是一個好主意,但是當我添加它們時,出現此錯誤:'1016,級別15,狀態3,過程sp_s_getCustOrderSummary,行12 外連接運算符不能是在包含連接表的查詢中指定。' – fullOfQuestions 2012-08-02 19:38:55

+0

我將其更改爲'from VW_CustomerOrder內部連接a.customer上的Customer b = b.customer內部連接將store c上的a.customer = a.customer = d.customer'上的c.customer inner join eligible_supplier d – fullOfQuestions 2012-08-02 19:39:19