1

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 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
;

This query worked fine in SQL Studio.

Any ideas??

2 Answers 2

3

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.

1
  • Thanks to you Mike and Jeff. It was an oversight on my part. I originally had the Where clause joining the two tables together but removed it to try and find the root cause of a different syntax problem. Then I just forgot to put it back in when trying to solve this problem. The actual query has a lot more information in the select statement that makes use of both tables.
    – AFJ
    Commented Jul 27, 2011 at 2:56
1

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;
1
  • Thanks for the tip about the Where clause Jeff. I didn't know you could put a whole query in the From clause and thought that the only way to do this was by using the Where statement. I will try using this from now on to see if it makes things easier.
    – AFJ
    Commented Jul 27, 2011 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.