示例#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;
 }
示例#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);
示例#3
0
<?php

require '../src/autoload.php';
use MysqlShard\Generator\StatsGenerator;
use MysqlShard\Adapters\MysqlShard;
use MysqlShard\Tools\Config;
use MysqlShard\Strategy\MonthStrategy;
$shard = new MysqlShard();
$strategy = new MonthStrategy(null, 'lines', $shard->getConfig(), '2016-06');
$shard->setStrategy($strategy);
$shard->setNoExec();
foreach ($shard as $shardItem) {
    $query = StatsGenerator::getCreateTableSQL();
    $shardItem->query($query, $strategy);
    echo $shardItem->getQuery(), "\n";
}
示例#4
0
<?php

require '../src/autoload.php';
use MysqlShard\Generator\CycleDataGenerator;
use MysqlShard\Adapters\MysqlShard;
use MysqlShard\Strategy\CycleStrategy;
$shard = new MysqlShard();
$strategy = new CycleStrategy(null, 'months', $shard->getConfig());
$shard->setStrategy($strategy);
//$shard->setNoExec();
foreach ($shard as $shardItem) {
    $query = CycleDataGenerator::getCreateTableSQL();
    //    $query = CycleDataGenerator::getDropTableSQL();
    $shardItem->query($query, $strategy);
    echo $shardItem->getQuery(), "\n";
}
示例#5
0
<?php

require '../src/autoload.php';
use MysqlShard\Adapters\MysqlShard;
use MysqlShard\Tools\Config;
use MysqlShard\Strategy\CycleStrategy;
use MysqlShard\Tools\Service;
$shard = new MysqlShard();
$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);