note
gmax
<p>If I understand correctly, the gist of what you are asking is "how can I find which records are missing from a given sequence?"</p>
<p>If this is true, one method to find out is to use an auxiliary table containing all the sequence numbers, and perform a LEFT JOIN to your table.</p>
<p>First, create such table and fill it with the values from 1 to the maximum record number you want to consider. What [Corion] said about maximum record number and unique numbers in Transaction are still valid and you should check them. However, assuming that TransactionID is unique, this method will find the missing rows very fast.</p>
<code>
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:driver:database",
"user","password",{RaiseError=>1})
or die;
$dbh->do(qq{
create table sequence (id int not null primary key))
});
my $sth = $dbh->prepare(qq{
insert into sequence values (?)
});
$dbh->{autocommit} =0;
$dbh->begin;
for ( 1.. 1_200_000) {
$sth->execute($_);
if (($_ % 1000) == 0) {
$dbh->commit;
$dbh->begin;
}
}
$dbh->commit;
my $min = 968000;
my $max = 1029829;
my $sequence_query = qq{
SELECT
a.id
FROM
sequence a
LEFT JOIN Transaction b ON (a.id=b.TranstactionID)
WHERE
a.id BETWEEN $min AND $max
AND b.TranstactionID IS NULL
};
$sth = $dbh->prepare($sequence_query);
$sth->execute();
while (my ($row) = $sth->fetchrow_array) {
print "$row is missing \n";
}
$dbh->disconnect();
</code>
<p>HTH</p>
<pre>
_ _ _ _
(_|| | |(_|><
_|
</pre>
280899
280899