PHP with MySQL is slow

前端 未结 3 1549
灰色年华
灰色年华 2020-12-09 19:25

(IMPORTANT) EDIT 3: Running the testajax2.php by itself and not Ajax. The duration is about the same, 1.02-1.03 seconds. So I guess that means the

3条回答
  •  一个人的身影
    2020-12-09 19:40

    It could be happening in a couple of places: 1) the query itself, or 2) the creation of the mysqli instance (and resulting db connection). If I had to guess, I suspect your problem is (2).

    One quick-and-dirty debugging method is to use microtime().

    http://www.php.net/manual/en/function.microtime.php

    Example:

    $start = microtime(TRUE); // Start counting
    
    $mysqli = new mysqli('localhost', 'root', '', 'testdb');
    
    $temp = microtime(TRUE) - $start;
    echo "Time to connect: {$temp}";  // Check elapsed time
    
    $mysqli->set_charset("utf8");
    if(mysqli_connect_errno()) {
        echo "Connection Failed: " . mysqli_connect_errno();
        exit();
    }
    
    $temp = microtime(TRUE) - $start;
    echo "Time to setup: {$temp}";  // Check elapsed time
    
    $testreceive = $_POST['test_id'];
    $query = "SELECT First_Name from tblperson";
    $result = $mysqli->query($query);
    
    $temp = microtime(TRUE) - $start;
    echo "Time to query: {$temp}";  // Check elapsed time
    
    $row = $result->fetch_all(MYSQLI_ASSOC);
    
    $temp = microtime(TRUE) - $start;
    echo "Time to fetch {$temp}";  // Check elapsed time
    

提交回复
热议问题