I recently had to move my database from SQL Studio over to Oracle and I am learning about all the different syntaxes that must be used.
Anyways, I have run into a problem today where a query I am trying to execute runs forever (> 15 min) and never seems to finish.
As you can see I use the With statement to create 2 temp. tables from which I run my query. If I query either of these tables individually, I get results in < 2 seconds. As soon as I add the other table in the From statement it doesn't seem to do anything.
This query works fine:
With Laser as
(
SELECT Data.Serial_Number, Equipment.Equipment_Desc, Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN
ORDER BY Equipment.Equipment_SN) AS RN
FROM Data, Equipment
where Data.Equipment_Station = Equipment.Equipment_SN and Equipment.Equipment_Desc like '%laser Etch%'
)
,AssyQC AS
(
SELECT Data.Serial_Number, Equipment.Equipment_Desc,Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN
ORDER BY Equipment.Equipment_SN) AS RN
FROM Data, Equipment
where Data.Equipment_Station = Equipment.Equipment_SN and Equipment.Equipment_Desc like '%QSMC SI%'
)
select to_char(Laser.SCAN_TIME, 'mm') as "Laser Month", to_char(Laser.SCAN_TIME, 'dd') as "Laser Day"
from Laser
;
Now, if I add the other table to the From statement, even though I don't use it anywhere else (select, where, etc.) the query doesn't do anything. It runs and runs and runs...
With Laser as
(
SELECT Data.Serial_Number, Equipment.Equipment_Desc, Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN
ORDER BY Equipment.Equipment_SN) AS RN
FROM Data, Equipment
where Data.Equipment_Station = Equipment.Equipment_SN and Equipment.Equipment_Desc like '%laser Etch%'
)
,AssyQC AS
(
SELECT D开发者_运维知识库ata.Serial_Number, Equipment.Equipment_Desc,Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN
ORDER BY Equipment.Equipment_SN) AS RN
FROM Data, Equipment
where Data.Equipment_Station = Equipment.Equipment_SN and Equipment.Equipment_Desc like '%QSMC SI%'
)
select to_char(Laser.SCAN_TIME, 'mm') as "Laser Month", to_char(Laser.SCAN_TIME, 'dd') as "Laser Day"
from Laser, AssyQC
;
This query worked fine in SQL Studio.
Any ideas??
Without any columns from AssyQC
in the final select
, I don't see why you're including it at all, but assuming you're just not including them in this example, try joining the tables:
With Laser as
(
SELECT Data.Serial_Number, Equipment.Equipment_Desc, Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN
ORDER BY Equipment.Equipment_SN) AS RN
FROM Data, Equipment
where Data.Equipment_Station = Equipment.Equipment_SN and Equipment.Equipment_Desc like '%laser Etch%'
)
,AssyQC AS
(
SELECT Data.Serial_Number, Equipment.Equipment_Desc,Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN
ORDER BY Equipment.Equipment_SN) AS RN
FROM Data, Equipment
where Data.Equipment_Station = Equipment.Equipment_SN and Equipment.Equipment_Desc like '%QSMC SI%'
)
select to_char(Laser.SCAN_TIME, 'mm') as "Laser Month", to_char(Laser.SCAN_TIME, 'dd') as "Laser Day"
from Laser, AssyQC
where
Laser.Serial_Number = AssyQC.Serial_Number
;
When you have multiple tables in your from
clause without joining them, you're doing an implicit cross join which, if your result sets are large, can cause the query to run a long time; from wikipedia:
In other words, it will produce rows which combine each row from the first table with each row from the second table.
As @Mike indicated, it looks like you are missing a join.
Is there a particular reason you are using the "With" syntax?
I could be wrong, but it does not look like you need this feature for this query. To me, it is harder to read and that would require more time to maintain over time.
Would this version help?
SELECT to_char(Laser.SCAN_TIME, 'mm') as "Laser Month",
to_char(Laser.SCAN_TIME, 'dd') as "Laser Day"
FROM ( SELECT Data.Serial_Number,
Equipment.Equipment_Desc,
Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN ORDER BY Equipment.Equipment_SN) AS RN
FROM Data,
Equipment
WHERE Data.Equipment_Station = Equipment.Equipment_SN
AND Equipment.Equipment_Desc like '%laser Etch%') laser,
( SELECT Data.Serial_Number,
Equipment.Equipment_Desc,
Data.SCAN_TIME,
ROW_NUMBER() OVER (PARTITION BY Data.Serial_Number, Equipment.Equipment_SN ORDER BY Equipment.Equipment_SN) AS RN
FROM Data,
Equipment
WHERE Data.Equipment_Station = Equipment.Equipment_SN and Equipment.Equipment_Desc like '%QSMC SI%') AssyQC
WHERE laser.serial_number = assyQC.serial_number;
精彩评论