Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Taming the Distributed Database Problem: A Case Study Using MySQL


Listing 1 compare_table_CRC.pl: Tells if two tables in two remote databases are different

#!/usr/bin/perl -w
# compare_table_CRC
# (C) Giuseppe Maxia 2003
#
# Compares two tables in two remote databases
# and checks for any difference, by comparing a global CRC
# of all the records.
# Does not return individual different records. Use the 
# find_differences script for such purposes
use strict;
use DBI;

# This script is for educational purposes only.
# Parameters are embedded in the script itself.
# For production, it woyld be better to use either
# a config file or any Getopt::XX modules
my $host1 = "localhost";
my $host2 = "192.168.59.9";
my $db1 = "xcompany";
my $db2 = "xcompany";
my $tablename = "employees";
my ($user1, $password1) = ('myuser', 'secret');
my ($user2, $password2) = ('myuser', 'secret');

my $dbh1 =  DBI->connect("DBI:mysql:$db1;"
    . "host=$host1;port=3306",
    $user1, 
    $password1, 
    {RaiseError => 1, PrintError =>0}) 
    or die "can't connect : $DBI::errstr";

my $dbh2 =  DBI->connect("DBI:mysql:$db2;"
    . "host=$host2;port=3306",
    $user2, 
    $password2, 
    {RaiseError => 1, PrintError =>0}) 
    or die "can't connect : $DBI::errstr";

unless (grep {$_ eq $tablename} $dbh1->tables()) {
    die "$tablename not found in $db1\n"
}

unless (grep {$_ eq $tablename} $dbh2->tables()) {
    die "$tablename not found in $db2\n"
}

my $fields = get_fields($dbh1, $tablename);

my $check_table = qq{
   SELECT 
   COUNT(*) AS cnt, 
   CONCAT(SUM(CONV(SUBSTRING(\@CRC:=MD5(CONCAT_WS('/##/',$fields)),1,8),16,10)),
    SUM(CONV(SUBSTRING(\@CRC, 9,8),16,10)),
    SUM(CONV(SUBSTRING(\@CRC,17,8),16,10)),
    SUM(CONV(SUBSTRING(\@CRC,25,8),16,10))
    ) AS sig 
    FROM $tablename 
    };

print $check_table,$/;

my $probe1 = $dbh1->selectall_arrayref($check_table);
my $probe2 = $dbh2->selectall_arrayref($check_table);

print "$db1\t $probe1->[0]->[0]\t $probe1->[0]->[1]\n";
print "$db2\t $probe2->[0]->[0]\t $probe2->[0]->[1]\n";

my $result = (($probe2->[0][0] ne $probe1->[0][0]) 
    or ($probe2->[0][1] ne $probe1->[0][1])) ?
        "DIFFERENCE FOUND" :  "NO DIFFERENCE" ; 
 
print "$result\n";

# returns the list of fields, ready to insert into the CRC query
sub get_fields {
    my ($dbh, $tablename) = @_;
    my $sth = $dbh->prepare(qq{describe $tablename});
    $sth->execute();
    my @fields=();
    while (my $row = $sth->fetchrow_hashref()) {
        # discards TIMESTAMP fields from the comparison
        next if lc $row->{Type} eq 'timestamp';
        my $field ="`$row->{Field}`";
        # if the field is nullable,
        # then a COALESCE function is used
        # to prevent the whole CONCAT from becoming NULL
        if (lc $row->{Null} eq 'yes') {
            $field = qq[coalesce($field,"#NULL#")];
        }
        push @fields, $field;
    }
    return join ",", @fields; 
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.