简体   繁体   English

SQL Server Query在按特定值过滤时永远不会完成

[英]SQL Server Query never completes when filtered by specific value

I have a table named inventory_transaction in a SQL Server 2008 R2 production environment. 我在SQL Server 2008 R2生产环境中有一个名为inventory_transaction的表。 The table and indexes, as well as the maintenance package to maintain them, were developed by a professional vendor. 表和索引以及维护它们的维护包由专业供应商开发。 We have had no problems for almost 10 years since implementing the program that uses this table, and there is no fragmentation above 8% on any of the indexes for the table. 自从实现使用该表的程序以来,我们已经有近10年没有问题了,并且表的任何索引都没有超过8%的碎片。

However, I am having some trouble now. 但是,我现在遇到了一些麻烦。 When I try to filter this table by one specific value, the query will not complete. 当我尝试按一个特定值过滤此表时,查询将无法完成。 Any other value will make the query execute in < 1 sec. 任何其他值将使查询在<1秒内执行。

Here is the specific query that will not complete: 以下是无法完成的特定查询:

DECLARE @BASE_ID VARCHAR(30) = 'W46873'    

SELECT * 
FROM PROD.DBO.INVENTORY_TRANS  
WHERE WORKORDER_BASE_ID = @BASE_ID

I let it run for 35 minutes before cancelling it. 我让它运行35分钟然后取消它。 I never receive an error message. 我从未收到过错误消息。 If I put literally any other value in @BASE_ID , it will execute in <1 sec. 如果我在@BASE_ID@BASE_ID任何其他值,它将在<1秒内执行。 If I change the equal operator on WORKORDER_BASE_ID to use LIKE and change @BASE_ID to @BASE_ID = 'W4687%' , it will work. 如果我更改WORKORDER_BASE_ID上的等于运算符以使用LIKE并将@BASE_ID更改为@BASE_ID = 'W4687%' ,它将起作用。 All values in this column are the same format (W + 5 numbers). 此列中的所有值都是相同的格式(W + 5个数字)。

The data type for @BASE_ID is the same as the column WORKORDER_BASE_ID on the table. @BASE_ID的数据类型与表上的WORKORDER_BASE_ID列相同。 There is a nonclustered index that includes this column which is being used by in other query plans that are completing for other values. 有一个非聚集索引包含此列,正在为其他值完成的其他查询计划中使用该列。 Since this one won't complete, I'm not sure what the actual execution plan it is using is. 由于这个不会完成,我不确定它使用的实际执行计划是什么。

Any ideas what could be causing this or what I could do to fix it? 什么可能导致这个或我可以做些什么来解决它? I can provide more information as needed. 我可以根据需要提供更多信息。 This issue is causing timeouts within the program, which creates a lot of problems. 此问题导致程序内部超时,从而产生许多问题。


EDIT1: Running the query with OPTION(RECOMPILE) doesn't help. EDIT1:使用OPTION(RECOMPILE)运行查询没有帮助。

DECLARE @BASE_ID VARCHAR(30) = 'W46873'    

SELECT * 
FROM VMFGTEST.DBO.INVENTORY_TRANS  
WHERE WORKORDER_BASE_ID = @BASE_ID
OPTION (RECOMPILE)

Your query may have been blocked by another process. 您的查询可能已被其他进程阻止。 While running your query in SQL Server Management Studio (SSMS), check the tab title. 在SQL Server Management Studio(SSMS)中运行查询时,请检查选项卡标题。 The parenthesized number at the end is the Server Process ID (SPID): 最后带括号的数字是服务器进程ID(SPID):

在此输入图像描述

While your query is still executing, execute the following command in another window to check if your query is being blocked: 当您的查询仍在执行时,请在另一个窗口中执行以下命令以检查您的查询是否被阻止:

/* Replace 70 with actual SPID */
SELECT * FROM sys.sysprocesses WHERE spid = 70

If the blocked column contains a positive number, this is the SPID of the process blocking your query. 如果被阻止的列包含正数,则这是阻止查询的进程的SPID。 For more details about this process, execute the following command: 有关此过程的更多详细信息,请执行以下命令:

/* Replace 62 with the actual SPID of the process in question */
DBCC INPUTBUFFER(62)
SELECT * FROM sys.sysprocesses WHERE spid = 62

The EventInfo may hint you on what is being executed in this connection, while login_time , hostname , program_name and other columns could pinpoint the connected user. EventInfo可能会提示您在此连接中正在执行的操作,而login_timehostnameprogram_name和其他列可能会查明已连接的用户。 For example another transaction may still be active. 例如,另一个交易可能仍处于活动状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM