I have table which is having about 1000 rows.I have to update a column(“X”) in the table to ‘Y’ for n ramdom rows. For this i can have following query
update xyz set X='Y' when m in (
'SELECT m FROM (SELECT m
FROM xyz
order by dbms_random.value
) RNDM
where rownum < n+1);
Is there another efficient way to write this query. The table has no index.
Please help?
I would use the ROWID:
The actual reason I would use ROWID isn’t for efficiency though (it will still do a full table scan) – your SQL may not update the number of rows you want if column
misn’t unique.With only 1000 rows, you shouldn’t really be worried about efficiency (maybe with a hundred million rows). Without any index on this table, you’re stuck doing a full table scan to select random records.
[EDIT:] “But what if there are 100,000 rows”
Well, that’s still 3 orders of magnitude less than 100 million.
I ran the following:
[created about 50,000 rows on my system – non-indexed, just like your table]
This took approximately 1.5 seconds. Maybe it was 1 second, maybe up to 3 seconds (didn’t formally time it, it just took about enough time to blink).