1

Here is a normal client side sending an AJAX request to the server, without using any libraries.

About The Database:

There is only 1 table in the DB having 2 columns and 2 rows. The Storage Engine in InnoDB

NOTE: This is a test DB. I also have another DB having 4 tables with 5000-6000 records and 18 other tables with less than 500 records

client.php:

<html>
...
<body>
  <button class="btn__">Send</button>
  <script>
    var btn = document.querySelector('.btn__');
    btn.onclick = function()
    {
       var xmlhttp = new XMLHttpRequest();
       xmlhttp.onreadystatechange = function()
       {
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
         {
            // document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
         }
       };
       xmlhttp.open("POST", "server_files/server.php", true);
       xmlhttp.send();
    }
  </script>
</body>
...
</html>

server.php (CASE #1): No connection query to the database

<?php

  // $con = mysqli_connect("host","username","pass","test_db");

  echo json_encode([
    'success' => true
  ]);
  exit();

(response takes ~59ms)

server.php (CASE #2): Connection query to the database

<?php

  $con = mysqli_connect("host","username","pass","test_db");

  echo json_encode([
    'success' => true
  ]);
  exit();

(response takes ~452ms; this shouldn't be happening and it didn't happened until a week ago)

Why is it taking a long time for the response if I include that line of connection to the database ?

EDIT: (just putting if it'll be helpful to understand better)

When I run SHOW PROCESSLIST; when the client is sending the AJAX request, I see this,

Result of SHOW PROCESSLIST

Devang Mistry
  • 115
  • 1
  • 7

1 Answers1

0

From the process-list it seems that it is not a local database, i.e., not on the localhost and you are not accessing it using localhost or 127.0.0.1.

Therefore it could be network or DNS related issue. Check DNS resolution issues and network latency problems.

If the DB Server is on the same host and you are using localhost in the connection string, then try changing it to 127.0.0.1

Nasoo
  • 136
  • 7
  • It is in the production environment. So I'm using the domain name in the place of `host` – Devang Mistry May 09 '16 at 14:58
  • Try the following two commands and add the results to your question. Run these on your web server. dig hostname_used_in_script & ping hostname_used_in_script. Just post the query time from the dig command and avg. time taken from ping command – Nasoo May 09 '16 at 15:05
  • Thanks! I changed the connection string from my domain name( `example.com` ) to its IP address. Thankyou so much. I've been trying to fix this thing since a week. Thank you :) – Devang Mistry May 09 '16 at 15:13