2012-08-02 54 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