Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP with MySQL is slow

(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 problem is in PHP-MySQL or XAMPP??

When I ran it through a phpMyAdmin query, here's the result: Showing rows 0 - 29 ( ~50 total. The query took 0.0015 seconds). It appears the problem lies not in Ajax after all, but perhaps in PHP. How can I fix this? (I've also just edited the question title.)

Answer: Add the following line in the hosts file located in ”C:\Windows\System32\drivers\etc”

   127.0.0.1 localhost

The question before:

Is it normal for jQuery Ajax with SQL queries on the other side have the minimum duration of 1 second? I've tried $.get, $.post,$.getjson,$.ajax({type:'POST'}), $.ajax({type:'GET'}). As I've said, it's the minimum. It could get worse to about 3 seconds even. I doubt it's the SQL queries though, as when I try them in phpMyAdmin, the results come up extremely fast.

It doesn't matter also if the queries are very simple and the table only has 2 elements, it would still follow the 1 sec minimum. I'm using the latest XAMPP, and I don't know if it matters, but I'm accessing the files through localhost and 127.0.0.1.

I'm running it on a local environment, on the same laptop I made these files on. jQuery is updated. The returned value/array is json_encoded. I'm using mysqli. The database is in InnoDB, and within are only about five tables, and there are hardly anything on them. Here's a very simple sample query:

File index.php

    var test_id =2;
    testcall();
    function testcall(){
        $.ajax({
            url: 'testajax2.php',
            type: 'POST',
            data: {test_id: test_id},
            success: function(data){

            }
        });
    }

File testajax2.php

    $mysqli = new mysqli('localhost', 'root', '', 'testdb');
    $mysqli->set_charset("utf8");
    if(mysqli_connect_errno()) {
        echo "Connection Failed: " . mysqli_connect_errno();
        exit();
    }

    $testreceive = $_POST['test_id'];
    $query = "SELECT First_Name from tblperson";
    $result = $mysqli->query($query);
    $row = $result->fetch_all(MYSQLI_ASSOC);

    echo json_encode($row);

The tblperson contains 50 records, and there are only four columns. According to Firebug, it took 1.03 seconds to do that extremely simple task. I'm not sure what it really means, but viewing through the Net tab in Firebug, the bar is entirely violet. 0 and 1.03 seconds Waiting. +1.03 seconds and 0 receiving.

It also doesn't matter if I send them as json_encode($row) or foreach($row as $value){ echo $value['First_Name']; }. It would still be about at least 1 second. I've tried on Chrome and Safari, and though I can't have the exact duration, I can tell that it's about the same. But for a simple Ajax call with no SQL queries. If I remember correctly, it's very fast. I'd be back with a sample and duration output.

like image 999
Wap Avatar asked Nov 27 '12 12:11

Wap


1 Answers

Add the following line in the hosts file located in ”C:\Windows\System32\drivers\etc”

   127.0.0.1 localhost

This solved my problem. PHP-MySQL can now complete its task depending on how complex somewhere between 20-500 ms.

like image 103
Wap Avatar answered Nov 15 '22 15:11

Wap