[Solved] Oracle Update Statement clarification required

EverSQL Database Performance Knowledge Base

Oracle Update Statement clarification required

Database type:

I am updating a table based on values from another table using the Serial Number field as the common field to look up values. However from below 2 queries i get two different results. Can some one explain why the two outputs are different? Should not the update statement update 47200 records?

UPDATE TBL_SERIAL_NUMBER_MASTER A
SET (A.name) = (SELECT B.name 
                FROM TBL_DEVICE_LOCALITY B 
                WHERE A.SERIAL_NUMBER = B.SERIAL_NUMBER AND ROWNUM <=1 )
WHERE EXISTS ( SELECT 1 
               FROM TBL_DEVICE_LOCALITY 
               WHERE SERIAL_NUMBER = A.SERIAL_NUMBER 
                AND TBL_ODIN_DEVICE_LOCALITY.HOST_NAME IS NOT NULL );

Results int: 35,311 rows updated.

select count(*)
from TBL_SERIAL_NUMBER_MASTER A, TBL_DEVICE_LOCALITY B
WHERE A.SERIAL_NUMBER = B.SERIAL_NUMBER AND B.HOST_NAME IS NOT NULL;

Returns: Count = 47200

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
CREATE INDEX tbl_locality_idx_serial_number ON TBL_DEVICE_LOCALITY (SERIAL_NUMBER);
The optimized query:
SELECT
        A.name 
    FROM
        TBL_SERIAL_NUMBER_MASTER A 
    WHERE
        EXISTS (
            SELECT
                1 
            FROM
                TBL_DEVICE_LOCALITY 
            WHERE
                TBL_DEVICE_LOCALITY.SERIAL_NUMBER = A.SERIAL_NUMBER 
                AND TBL_ODIN_DEVICE_LOCALITY.HOST_NAME IS NOT NULL
        )

Related Articles



* original question posted on StackOverflow here.