Home > Enterprise >  Quickly find a record appearing more than twice in a table
Quickly find a record appearing more than twice in a table

Time:12-02

Title pretty much says it all, using Oracle SQL, I'd like to get, as quickly as possible, three records that share an ID from a very large table. The rows are not duplicates, they share one ID (rID) but differ in another (mID).

One approach I know I could do (that would be very slow) would be to load the first say 1000 records into a C# program, then execute a COUNT query to count the number of records with each ID until I hit one with 3 records and return that ID. I know this is a terrible approach but should give an idea of what I want to get out of this.

I've tried using GROUP BY, and this would work but would be unacceptably slow, I don't care about the state of the rest of the table, I just need a single ID that has three records. Ideally I'd do something like a GROUP BY that would stop after finding the first ID with three or more records and just return that one. There are over a million records in the table so efficiency is important.

CodePudding user response:

What you describe translates to:

select the_id
from the_table
group by the_id
having count(*) >= 3
fetch first row only;

This should be as fast as it gets. You can help Oracle by providing an index on the id. That's about it.

  • Related