SQL查询需要很长时间才能执行

Jon*_*nah 0 sql-server sql-server-2005

USE Pooja
GO
----Create TestTable
CREATE TABLE TestTable(RtJobCode VARCHAR(20), RtProfCode smallint,RtTestCode smallint,ProfCode smallint,TestCode smallint)
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (RtJobCode, RtProfCode,RtTestCode,ProfCode,TestCode)
SELECT RtJobCode,RtTestCode,TestCode,RtProfCode,ProfCode
FROM dbo.ResultTest,dbo.Test,dbo.Profiles
WHERE RtTestCode=ANY(Select TestCode from dbo.Test)

----Verify that Data in TestTable
SELECT *
FROM TestTable

GO
Run Code Online (Sandbox Code Playgroud)

上面的代码试图从名为resutltest和profiles和test的表中取出条目,

问题是在创建一个多维数据集期间我遇到了一些在所有表中都不一致的数据,所以我尝试了对表的连接,但由于表包含大量的列,所以它是不可行的,所以尝试制作这段代码只是不停地执行而不停止显示任何数据

Resulttest的Rttestcode是testcode的外键

Fil*_*Vos 5

您的查询非常慢,因为它在ResultTest,Test和Profiles之间制作了一个笛卡尔积.您需要提供"加入"条件以将表链接在一起.

SELECT RtJobCode
     , RtTestCode
     , TestCode
     , RtProfCode 
     , ProfCode
FROM dbo.ResultTest r 
JOIN dbo.Test t
  ON r.RtTestCode = t.TestCode
JOIN dbo.Profiles p
  ON r.RtProfCode = p.ProfCode 
Run Code Online (Sandbox Code Playgroud)

我推测这是您正在寻找的查询.请注意将ResultTest和Test链接在一起的条件以及将ResultTest和Profiles链接在一起的条件.