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 3 find_differences.pl: Implementation of the range search algorithm

#!/usr/bin/perl -w
# find_differences
# (C) Giuseppe Maxia 2003
# 
# Compare two tables in two remote databases, looking for different records.
# Requires a "shadow table", created by make_shadow_table
# 
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 $db1 = "xcompany";
my $db2 = "xcompany";
my $host1 = "localhost";
my $host2 ="192.168.59.9";
my ( $user1, $password1 ) = ( 'myuser', 'secret' );
my ( $user2, $password2 ) = ( 'myuser', 'secret' );

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

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

$dbh1->do(qq{set SQL_LOG_OFF=1 }); # prevents writing on the general log file
$dbh2->do(qq{set SQL_LOG_OFF=1 }); # which can become quite big if you <BR>
                                   # have many records

my @probes =();

my $initialprobe = qq{
  SELECT 
    span, COUNT(*) AS cnt, 
    CONCAT(SUM(CONV(SUBSTRING(signature,1,8),16,10)), 
    SUM(CONV(SUBSTRING(signature,9,8),16,10)),
    SUM(CONV(SUBSTRING(signature,17,8),16,10)),
    SUM(CONV(SUBSTRING(signature,25,8),16,10))) AS sig 
  FROM 
    shadow_table 
  GROUP BY 
    span };

my $probe1 = $dbh1->selectall_arrayref($initialprobe);
my $probe2 = $dbh2->selectall_arrayref($initialprobe);
my @totest = ();
my $maxdiff = 0;

for (0.. @{$probe1} -1) {
    if ($probe1->[$_]->[2] ne $probe2->[$_]->[2]) {
        push @totest, $probe1->[$_]->[0];
        $maxdiff += $probe1->[$_]->[1];
    }
}
my $get_probe_sig = qq{SELECT signature from shadow_table where span = ?};
my $sth1 = $dbh1->prepare($get_probe_sig);

for (@totest) {
    $sth1->execute($_);
    my $columns = $sth1->fetchall_arrayref();
    my $query = qq{SELECT PK FROM shadow_table WHERE span = $_ AND
    signature NOT IN (}
    . (join(",", map {"'$_->[0]'"} @$columns ))
    . ")";
    my $sth2 = $dbh2->prepare($query);
    $sth2->execute();
    while (my $different = $sth2->fetchrow_arrayref()) {
        print "$different->[0]\t";
    }
}

print "\n";


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.