Skip to content

Commit dd2cbe6

Browse files
committed
add to output number of rows retrieved
1 parent b341fe2 commit dd2cbe6

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ It was developed to compare MySQL replicas, in particular for a MySQL InnoDB clu
66

77
## Algorithm
88

9+
The tool first retrieves schema information and ensures that tables can be compared. Two tables can be compared when they carry the same columns and the same primary keys. The tool issues warning when columns or primary keys are different type or different ordinal position.
10+
911
The tool compares md5 of the row, simply speaking `select concat(pk1, pk2) PK, md5(concat(pk1, pk2, coalesce(c1, 'null'), coalesce(c2, 'null'), ...)) MD5 from mytable order by pk1, pk2`. If some primary keys are missing on either master or slave database, or md5 value is not matching, the tool reports the difference, showing the primary keys.
1012

1113
The tool being optimized for tables very similar in content, it retrieves 1 row alternatively from master then slave table. As data is sorted by PK, it keeps a sorted list of differences (pk and md5 value) in memory. As soon as there is a match, any value lower than the match pair is immediately reported and removed from memory. Memory footprint shall then remain small.
@@ -26,6 +28,7 @@ Here is a sample output from the test cases.
2628

2729
```
2830
Loading Database Schema Metadata MASTER
31+
2932
Loading Database Schema Metadata Slave #1
3033
Comparing Database Schema Metadata MASTER with Slave #1
3134
ERROR object:onlyina, differenceType:TABLE_MISSING_IN_SLAVE_SCHEMA
@@ -68,6 +71,8 @@ In scope tables: tab1 tab2 tab3 tab5
6871
ERROR object:tab3, differenceType:DATA_ROW_MISSING_IN_SLAVE_TABLE, note:(uid,sometext2)=(4,d2)
6972
ERROR object:tab3, differenceType:DATA_ROW_EXCESS_IN_SLAVE_TABLE, note:(uid,sometext2)=(4,d3)
7073
ERROR object:tab3, differenceType:DATA_ROW_EXCESS_IN_SLAVE_TABLE, note:(uid,sometext2)=(6,e2)
71-
Comparing Database Schema MASTER with Slave #1 - Complete. Duration(ms): 56
74+
Comparing Database Schema MASTER with Slave #1 complete
75+
Duration(ms): 53. Total rows retrieved from master: 19, slave: 20
76+
7277
Job Done. Count Summary warningCount:14, errorCount:24
7378
```

src/main/java/com/geckotechnology/mySqlDataCompare/App.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ public static void main(String[] args)
3838
for(int i = 1; i<args.length; i++) {
3939
MySQLSchemaRetriever slaveSchemaReader = null;
4040
Schema slaveSchema = null;
41+
MySQLTableDataComparer tableDataComparer = null;
4142
long startTime = System.currentTimeMillis();
4243
try {
44+
System.out.println();
4345
System.out.println("Loading Database Schema Metadata Slave #" + i);
4446
slaveSchemaReader = new MySQLSchemaRetriever(args[i]);
4547
slaveSchemaReader.openConnection();
@@ -61,7 +63,7 @@ public static void main(String[] args)
6163
}
6264
System.out.println();
6365
//start comparison
64-
MySQLTableDataComparer tableDataComparer = new MySQLTableDataComparer(masterSchemaReader, slaveSchemaReader);
66+
tableDataComparer = new MySQLTableDataComparer(masterSchemaReader, slaveSchemaReader);
6567
for(Table table:tablesReadyToBeDataAnalyzed)
6668
tableDataComparer.compareTable(table);
6769
tableDataComparer.printDataDifferenceDetails();
@@ -77,7 +79,12 @@ public static void main(String[] args)
7779
if(slaveSchemaReader != null)
7880
slaveSchemaReader.closeConnection();
7981
long endTime = System.currentTimeMillis();
80-
System.out.println("Comparing Database Schema MASTER with Slave #" + i + " - Complete. Duration(ms): " + (endTime - startTime));
82+
System.out.println("Comparing Database Schema MASTER with Slave #" + i + " complete");
83+
System.out.print("Duration(ms): " + (endTime - startTime));
84+
if(tableDataComparer != null)
85+
System.out.print(". Total rows retrieved from master: " + tableDataComparer.getMasterTotalRetrievedRows() +
86+
", slave: " + tableDataComparer.getSlaveTotalRetrievedRows());
87+
System.out.println();
8188
}
8289
}
8390

@@ -89,6 +96,7 @@ public static void main(String[] args)
8996

9097
}
9198

99+
System.out.println();
92100
System.out.println("Job Done. Count Summary warningCount:" + warningCount + ", errorCount:" + errorCount);
93101
if(errorCount>0)
94102
System.exit(1);

src/main/java/com/geckotechnology/mySqlDataCompare/MySQLTableDataComparer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ public class MySQLTableDataComparer {
1212
private ArrayList<SchemaDifference> dataDifferences = new ArrayList<SchemaDifference>();
1313
private MySQLSchemaRetriever masterSchemaReader;
1414
private MySQLSchemaRetriever slaveSchemaReader;
15+
private int masterTotalRetrievedRows;
16+
private int slaveTotalRetrievedRows;
1517

1618
public MySQLTableDataComparer(MySQLSchemaRetriever masterSchemaReader, MySQLSchemaRetriever slaveSchemaReader) {
1719
this.masterSchemaReader = masterSchemaReader;
1820
this.slaveSchemaReader = slaveSchemaReader;
1921
}
2022

23+
public int getMasterTotalRetrievedRows() {
24+
return masterTotalRetrievedRows;
25+
}
26+
27+
public int getSlaveTotalRetrievedRows() {
28+
return slaveTotalRetrievedRows;
29+
}
30+
2131
public void compareTable(Table table) throws Exception {
2232
//note that only 1 SQL statement is used for both tables, even if column or PK columns are not in the same order
2333
StringBuilder selectSQL = table.createSQLToGetAllRows();
@@ -35,6 +45,7 @@ public void compareTable(Table table) throws Exception {
3545
//Get 1 row from master DB
3646
if(hasMasterResultSetNext && masterResultSet.next()) {
3747
OneRow masterOneRow = new OneRow(masterResultSet.getString(1), masterResultSet.getString(2));
48+
masterTotalRetrievedRows++;
3849
int slaveRowIndex = slaveRows.indexOf(masterOneRow);
3950
if(slaveRowIndex != -1) {
4051
//slave row matching PK has been found
@@ -69,6 +80,7 @@ public void compareTable(Table table) throws Exception {
6980
//Get 1 row from slave DB
7081
if(hasSlaveResultSetNext && slaveResultSet.next()) {
7182
OneRow slaveOneRow = new OneRow(slaveResultSet.getString(1), slaveResultSet.getString(2));
83+
slaveTotalRetrievedRows++;
7284
int masterRowIndex = masterRows.indexOf(slaveOneRow);
7385
if(masterRowIndex != -1) {
7486
//slave row matching PK has been found

0 commit comments

Comments
 (0)