添加Where子句时出了什么问题? [英] What goes wrong when I add the Where clause?

查看:103
本文介绍了添加Where子句时出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的查询:

 Select Distinct BOLTYPENAME, BOLTYPE.BOLTYPE From BOLTYPE
 Inner Join WORKORDER on WORKORDER.BOLTYPE=BOLTYPE.BOLTYPE
 Inner Join BOLMAIN On BOLMAIN.BOLID=WORKORDER.BOLID 
 Where BOLMAIN.CORID=156

当我在没有Where子句的情况下运行此查询时,需要0.1秒。但添加where子句会导致返回1分钟。所有表都有相关索引,并且已经拆分。三个表中的行数为:

When I run this query without the "Where" clause, it takes 0.1 secs. But adding the where clause causes it to take 1 minute to return. All tables have relevant indexes and they have been de-fragmented. The number of rows in the three tables is:

BOLTYPE:11行

BOLTYPE: 11 rows

BOLMAIN:71,455行

BOLMAIN: 71,455 rows

WORKORDER:197,500行

WORKORDER: 197,500 rows

以下是执行计划:

没有 Where子句(0.1秒):

Without the Where Clause (0.1 sec):

使用 Where子句(60秒):

With the Where Clause (60 sec):

有什么问题可以解决?

更新:以下是相关的索引定义:

Update: Here are the relevant Index definitions:

CREATE NONCLUSTERED INDEX [BOLIDX] ON [dbo].[WORKORDER]
([BOLID] ASC)
GO

CREATE NONCLUSTERED INDEX [CORIDX] ON [dbo].[BOLMAIN]
([CORID] ASC)
INCLUDE ([BOLID])
GO

CREATE NONCLUSTERED INDEX [BOLTYPEIDX] ON [dbo].[WORKORDER]
([BOLTYPE] ASC)
GO


解决方案

重新创建CORIDX索引,使其覆盖BOLID。您正在加入BOLID,因此您希望它成为索引的一部分,而不仅仅是其中一个列。

Recreate the CORIDX index so it covers BOLID. You're joining on BOLID, so you want it to be part of the index, not just one of the included columns.

换句话说:

CREATE NONCLUSTERED INDEX [CORIDX] ON [dbo].[BOLMAIN]
([CORID] ASC, [BOLID] ASC)

这篇关于添加Where子句时出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆