Skip to content
Advertisement

Oracle: How to update every row in a table within the procedure if rows are grouped?

Imagine I have a table with dublicates (I have simplified the problem and data): enter image description here

My procedure with Parent_id as a parametr goes through every row and find value for ‘Matched’ column in a different table.

It takes about 8 seconds for my procedure to find a value for Matched column. Usually there are thousands of rows. So it takes hours to finish the task.

The problem is that I have to add value to every row I have. Even if those rows are dublicates. If I could group those rows in a cursor, it will save up to 70% of time. But how can I update value of ‘Matched’ in every single row if GROUP BY is used?: enter image description here

Advertisement

Answer

Seems like you could simply do an update using the group-by values as a key.

Essentially, for each row returned by the cursor, do something like:

UPDATE table SET matched = the_cursor.matched
  WHERE parent_id = the_cursor.parent_id
    AND name = the_cursor.name
    AND address = the_cursor.address

For this to be efficient, you would need an index covering at least one of the columns used in the WHERE clause.

But as mentioned in the comments, it all raises the larger question – do you really need to do this row-by-row, or can you use a single MERGE or UPDATE statement to update the entire table?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement