If you run a select on every table ordered by the primary key you can compare the rows without loading the full tables in memory.

For instance, supposing the primary key my_key is of some string type:

my $sql = <<EOSQL; select my_key, foo1, foo2, ... from bar order by my_key EOSQL my $sth1 = $dbh1->prepare($sql); my $sth2 = $dbh2->prepare($sql); unless ($sth1->execute and $sth2->execute) { die ...; } my $row1 = $sth1->fetchrow_arrayref; my $row2 = $sth1->fetchrow_arrayref; while ($row1 and $row2) { if ($row1->[0] lt $row2->[0]) { print "$row1->[0] only in db 1\n" $row1 = $sth1->fetchrow_arrayref; } elsif ($row1->[0] gt $row2->[0]) { print "$row2->[0] only in db 2\n" $row2 = $sth2->fetchrow_arrayref; } else { # a better equality check for rows could be needed # if the db software is different or if the db # schemas are not identical: if ("@$row1" eq "@$row2") { print "row $row1->[0] differ\n" } $row1 = $sth1->fetchrow_arrayref; $row2 = $sth2->fetchrow_arrayref; } } while ($row1) { print "$row1->[0] only in db 1\n" $row1 = $sth1->fetchrow_arrayref; } while ($row2) { print "$row2->[0] only in db 2\n" $row2 = $sth2->fetchrow_arrayref; }

In reply to Re: Comparing databases - what is the best way? by salva
in thread Comparing databases - what is the best way? by Win

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.