Beispiel #1
0
 public function run()
 {
     $conf = Config::get('sharding');
     $shard = new Adapters\MysqlShard($conf);
     $strategy = new Strategy\LinearStrategy(null, self::name, $shard->getConfig());
     $name = self::name;
     $pos = strlen(self::name) + 2;
     $sql = "SELECT  substr( TABLE_NAME, {$pos}) AS num, TABLE_ROWS as `count`  \n                FROM information_schema.TABLES \n                WHERE TABLE_SCHEMA \n                    LIKE '{$name}_%' \n                    AND  TABLE_NAME REGEXP '^({$name}_)[0-9]+\$'";
     $tables = [];
     for ($cnn_id = 0; $cnn_id < $shard->getInstanceCount(); $cnn_id++) {
         try {
             $mysql = $shard->getConnectionById($cnn_id);
         } catch (Exception $e) {
             echo $e->getMessage();
         }
         $res = $mysql->query($sql);
         while (($row = $res->fetch_assoc()) != null) {
             $tables[$row['num']] = $row['count'];
         }
     }
     $sum = count($tables);
     $fillTables = 0;
     $startTabId = 0;
     foreach ($tables as $tab_id => $count) {
         if ($count > 0) {
             $fillTables++;
         }
         if ($tab_id > $startTabId) {
             $startTabId = (int) $tab_id;
         }
     }
     if ($sum - $fillTables > self::emptyTables) {
         $out = sprintf('fulling tables %d from %d %s', $fillTables, $sum, date('Y-m-d'));
         echo $out, PHP_EOL;
         exit;
     }
     $out = sprintf('generate: full tables %d from %d', $fillTables, $sum);
     $startTabId++;
     echo 'generate tables:', PHP_EOL;
     //        $shard->setNoExec();
     $table_names = '';
     for ($i = $startTabId; $i < $startTabId + self::count; $i++) {
         $table_names .= self::name . '_' . $i . PHP_EOL;
         $strategy->setId(Strategy\LinearStrategy::MAX_RECORD_COUNT * $i + 1);
         $table = Generator\LinesGenerator::getCreateTableSQL($i);
         $shard->query($table, $strategy);
         //                echo $shard->getQuery(), PHP_EOL,PHP_EOL;
     }
     echo $table_names, PHP_EOL;
 }
Beispiel #2
0
<?php

require '../src/autoload.php';
use MysqlShard\Adapters\MysqlShard;
use MysqlShard\Tools\Config;
use MysqlShard\Strategy\LinearStrategy;
use MysqlShard\Tools\Service;
$shard = new MysqlShard();
$strategy = new LinearStrategy(null, 'lines', $shard->getConfig());
$shard->query("select 1", $strategy);
Beispiel #3
0
<?php

require '../src/autoload.php';
use MysqlShard\Adapters\MysqlShard;
use MysqlShard\Tools\Config;
use MysqlShard\Strategy\LinearStrategy;
use MysqlShard\Generator\LinesGenerator;
$shard = new MysqlShard();
$strategy = new LinearStrategy(null, 'lines', $shard->getConfig());
// create 10 tables for shards 0..9
for ($i = 0; $i < 10; $i++) {
    $query = LinesGenerator::getCreateTableSQL($i);
    $strategy->setId($i * LinearStrategy::MAX_RECORD_COUNT + 1);
    $shard->query($query, $strategy);
}
Beispiel #4
0
$strategy = new CycleStrategy(null, 'months', $shard->getConfig());
$shard->setStrategy($strategy);
foreach ($shard as $shardItem) {
    $shardItem->query("TRUNCATE TABLE  %db.xdata", $strategy);
}
$shard->reset();
$time = microtime(true);
// $shard->setNoExec();
for ($i = 1; $i < 100; $i++) {
    $strategy->setId($i);
    $shard->push("INSERT INTO `%db`.`xdata` (id,data) VALUES( {$i}, 'xxx')");
    //	$res = $shard->query( "INSERT INTO `%db`.`xdata` (id,data) VALUES( $i, 'xxx')", $strategy);
    //	echo "shard_id=", $strategy->getShardId(),'  ' , $shard->getQuery(),"\n";
}
$shard->flush();
echo microtime(true) - $time, PHP_EOL;
exit;
$shard->reset();
foreach ($shard as $shardItem) {
    $shardItem->query("TRUNCATE TABLE  %db.xdata", $strategy);
}
$shard->reset();
$time = microtime(true);
for ($i = 1; $i < 100; $i++) {
    $strategy->setId($i);
    $res = $shard->query("INSERT INTO `%db`.`xdata` (id,data) VALUES( {$i}, 'xxx')", $strategy);
    //	echo "sid=", $strategy->getShardId(),'  ' , $shard->getQuery(),"\n";
}
echo microtime(true) - $time, PHP_EOL;
//
// exit();