Slow performance MySql

后端 未结 2 1122
庸人自扰
庸人自扰 2020-12-06 15:45

I\'m trying out performance of a system I\'m building, and it\'s really slow, and I don\'t know why or if it should be this slow. What I\'m testing is how many single INSERT

2条回答
  •  广开言路
    2020-12-06 16:23

    Your test isn't a very good way of judging performance. Why because you are preparing a statement 1000 times. That's not the way prepared statements are supposed to be used. The statement is prepared once and different parameters bound multiple times. Try this:

    public function __construct() {
        $this->mDb = new mysqli($this->DBHOST, $this->DBUSERNAME
                              , $this->DBPASSWORD, $this->DBNAME
                              , $this->DBPORT);
        $this->isConnected = true;
        $queryString = 'INSERT INTO `users`(`email`, `company_id`) '
                        .'VALUES (?, 1)';
        $this->stmt_insert = $this->mDb->prepare($queryString);
    
    }
    

    and

    public function insertUser($user) {
        $this->stmt_insert->bind_param('s', $user);
        if ($this->stmt_insert->execute()) {
            return 1;
        } else {
            return 0;
        }
    }
    

    And, you will be seeing a huge boost in performance. So to recap, there's nothing wrong with your system, it's just the test that was bad.

    Update:

    Your Common Sense has a point about preparing in advance and reusing the prepared statement not giving a big boost. I tested and found it to be about 5-10%

    however, There is something that does give a big boost. Turning Autocommit to off!. Inserting 100 records which previously took about 4.7 seconds on average dropped to < 0.5s on average!

    $con->autocommit(false);

    /loop/ $con->commit();

提交回复
热议问题