SQL takes a long time-consuming tests on the TiDB and Mysql

Before seeing the performance comparison TiDB and MySql are a lot of pressure measurement, stand-alone case TiDB and MySql that there are some gaps in the short time-consuming request, but I recently came across the scene more is the number of lines to be scanned is not a small sql in the case of a single sql time-consuming problem, so we made a simple test of this type sql consuming.

TiDB deploy stand-alone environment

According to official documents ( directly docker-composer deployment https://pingcap.com/docs-cn/dev/how-to/get-started/deploy-tidb-from-docker-compose/)

git clone https://github.com/pingcap/tidb-docker-compose.git  # 下载

cd tidb-docker-compose && docker-compose pull # 拉取镜像
docker-compose up -d # 启动

Command-line client connections

mysql -h 127.0.0.1 -P 4000 -u root

test

Table Structure

create table testTable (
    `id` int(10) unsigned auto_increment primary key,
    `uid` int(10) not null default 0,
    `day` int(6) not null default 0,
    `field1` varchar(16) not null default '',
    `field2` tinyint(3) not null default 0,
    `field3` tinyint(3) not null default 0,
    `field4` tinyint(3) not null default 0,
    `field5` varchar(32) not null default '',
    `field6` varchar(32) not null default '',
    `field7` varchar(32) not null default '',
    key `uid_day_idx` (`uid`,`day`,`field1`, `field3`)
) engine=InnoDB default charset=utf8;

Insert data

  • 5000 random uid

  • day evenly distributed 20190801 ~ 20190830

  • filed1 randomly 'a0' ~ 'a9'
  • 0 to 10 randomly selected field3

Insert data script

<?php
$maxRecordNum = 10000000;

$tableName = 'testTable';
$host = '127.0.0.1';
$port = '4000';
$dbname = 'test';
$username = 'root';
$password = '';
$dsn = "mysql:host={$host};dbname={$dbname};port={$port}";

function getDay($maxRecordNum, $i) {
    $item = (int)($maxRecordNum / 30);
    $day = 20190801;
    $add = (int)($i/ $item);
    return $day + $add;
}

function getRandomFieldOne() {
    static $fieldsOne = [
        "'a0'", "'a1'", "'a2'", "'a3'", "'a4'", "'a5'", "'a6'", "'a7'", "'a8'", "'a9'",
    ];
    return $fieldsOne[rand(0, count($fieldsOne) -1)];
}

function getRandomFieldTwo() {
    return rand(0, 1);
}

function getRandomFieldThree() {
    return rand(0, 10);
}

function getRandomFieldFour() {
    return rand(0, 8);
}

function generateRecordsValue($day) {
    $minUid = 200000000;
    $maxUid = $minUid + 5000;

    $arrRecord = [
        'uid' => rand($minUid, $maxUid),
        'day' => $day,
        'field1' => getRandomFieldOne(),
        'field2' => getRandomFieldTwo(),
        'field3' => getRandomFieldThree(),
        'field4' => getRandomFieldFour(),
        'field5' => "'static'",
        'field6' => "'static'",
        'field7' => "'static'",
    ];
    return $arrRecord;
}

try {
    $db = new PDO($dsn, $username, $password);

    $db->query("truncate {$tableName};");
    $db->query("alter table {$tableName} AUTO_INCREMENT=1;");
    $arr = [];
    for ($i = 1; $i <= $maxRecordNum; $i++) {
            $day = getDay($maxRecordNum, $i);
            $arr[] = '(' . implode(',', generateRecordsValue($day)) . ')';
        if ($i % 10000 === 0) {
            $sql = "INSERT INTO {$tableName} (" . implode(',', array_keys(generateRecordsValue(0))) . ") values" . implode(',', $arr) .';';
            $res = $db->query($sql);
            $arr = [];
            sleep(1);
            echo "{$i}\n";
        }
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

Test sql script

Discover the range of 15 days, 2000 uid, comes with two field conditions

<?php
$tableName = 'testTable';
$host = '127.0.0.1';
$dbname = 'test';
$username = 'root';
$port = '4000';
$password = '';
//$port = '3306';
//$password = 'guapi123';
$dsn = "mysql:host={$host};dbname={$dbname};port={$port}";

try {
    $db = new PDO($dsn, $username, $password);
    $arrUid = [];
    $minUid = 200000000;
    for ($i = 0; $i < 2000; $i++) {
        $arrUid[] = $minUid + $i;
    }
    $total = 0;
    for ($i = 0; $i < 15; $i++) {
        $startDay = 20190801 + $i;
        $endDay = $startDay + 14;
        $sql = "select field4 from {$tableName} where uid in (" . implode(',', $arrUid) . ") and day >= {$startDay} and day<={$endDay} and field1 = 'a0' and field3 in (3, 5);";
        $startTime = microtime(true);
        $res = $db->query($sql);
        $endTime = microtime(true);
        $cost = (int)(($endTime - $startTime)  * 1000);
        echo "cost:{$cost}ms\n";
        $total += $cost;
    }
    echo "avg cost:" . (int)($total / 15) . "ms\n";

} catch (Exception $e) {
    echo $e->getMessage();
}

Test results (Mysql and TiDB caching strategies are default configuration)

TiDB

cost:1744ms
cost:646ms
cost:720ms
cost:614ms
cost:644ms
cost:659ms
cost:662ms
cost:731ms
cost:728ms
cost:669ms
cost:816ms
cost:682ms
cost:778ms
cost:857ms
cost:718ms
avg cost:777ms

Mysql

cost:5256ms
cost:5165ms
cost:5300ms
cost:5461ms
cost:5376ms
cost:5334ms
cost:5435ms
cost:5339ms
cost:5314ms
cost:5278ms
cost:5346ms
cost:5244ms
cost:5387ms
cost:5497ms
cost:5633ms
avg cost:5357ms

Guess you like

Origin www.cnblogs.com/Me1onRind/p/11372026.html