Skip to content

Commit 6d1289c

Browse files
author
Joe Green
committed
skip large gaps in index
1 parent b35da7b commit 6d1289c

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

build/db-sync.phar

579 Bytes
Binary file not shown.

src/DbSync/Comparison/Hash.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class Hash extends HashAbstract {
2323
protected $where;
2424

2525
protected $total;
26-
26+
27+
protected $start;
28+
2729
private static $intTypes = array(
2830
'tinyint',
2931
'smallint',
@@ -75,11 +77,9 @@ public function setTable($sourcetable, $desttable, $comparisonColumns, $syncColu
7577
{
7678
$this->limitKey = $key;
7779

78-
$query = 'SELECT %s(' . $this->limitKey . ') FROM ' . $this->sourcetable . ' WHERE 1' . $this->where;
79-
80-
$this->start = $this->source->fetchOne(sprintf($query, 'MIN'));
80+
$this->start = $this->getAggregateCount('MIN');
8181

82-
$this->total = $this->source->fetchOne(sprintf($query, 'MAX'));
82+
$this->total = $this->getAggregateCount('MAX');
8383

8484
return;
8585
}
@@ -89,6 +89,20 @@ public function setTable($sourcetable, $desttable, $comparisonColumns, $syncColu
8989

9090
$this->total = $this->source->fetchOne('SELECT count(*) FROM ' . $this->sourcetable . ' WHERE 1' . $this->where);
9191
}
92+
93+
private function getAggregateCount($aggregate, $above = null)
94+
{
95+
$where = '';
96+
97+
if($above)
98+
{
99+
$where = ' AND ' . $this->limitKey . ' >= ' . $this->source->quote($above);
100+
}
101+
102+
$query = 'SELECT %s(' . $this->limitKey . ') FROM ' . $this->sourcetable . ' WHERE 1' . $this->where . $where;
103+
104+
return $this->source->fetchOne(sprintf($query, $aggregate));
105+
}
92106

93107
public function total()
94108
{
@@ -132,6 +146,11 @@ private function buildComparisonHash()
132146

133147
return "CONCAT(" . implode(',', $str) . ")";
134148
}
149+
150+
public function nextValidIndex($block)
151+
{
152+
return $this->limitKey ? ($this->getAggregateCount('MIN', $block) ?: $block) : $block;
153+
}
135154

136155
private function compareLimit($offset, $blockSize)
137156
{

src/DbSync/Comparison/LimitIterator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function next()
4848
if($this->row >= $this->blockSize)
4949
{
5050
$this->row = 0;
51+
5152
$this->block += $this->blockSize;
5253
}
5354
}
@@ -61,10 +62,15 @@ private function nextBlock()
6162
return;
6263
}
6364

64-
$this->block += $this->blockSize;
65+
$this->block = $this->nextBlockIndex();
6566
}
6667
}
6768

69+
private function nextBlockIndex()
70+
{
71+
return $this->comparsion->nextValidIndex($this->block + $this->blockSize);
72+
}
73+
6874
public function key()
6975
{
7076
return $this->block + $this->row;

src/DbSync/Connection.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ function setConnection(PDO $connection)
6363
*/
6464
public function query($sql, $bind = array())
6565
{
66-
6766
is_array($bind) or $bind = array($bind);
6867

6968
$stmt = $this->getConnection()->prepare($sql);
@@ -99,6 +98,14 @@ public function fetchNumeric($sql, $bind = array())
9998
return $this->query($sql, $bind)->fetch(PDO::FETCH_NUM);
10099
}
101100

101+
/**
102+
*
103+
*/
104+
public function quote($value)
105+
{
106+
return $this->_connection->quote($value);
107+
}
108+
102109
/**
103110
*
104111
* @param string $sql

0 commit comments

Comments
 (0)