MySQL query taking lot of time in an AJAX request MySQL query taking lot of time in an AJAX request ajax ajax

MySQL query taking lot of time in an AJAX request


The problem was in the connection string. I was using my domain name (example.com) for connecting to the Database. So I changed it to my IP address and it resolved the problem.

Thanks everyone for your help.


I think you must first figure out whether its query which is taking time or not.- Put the same query in phpmyadmin and run the query and check the time. If query takes time then try to remove limit and run or use indexing for it.- If query does not take time then check for any error or issue in network while fetching the data.Remove the query from the ajax for time being and send static data. Check what time it it takes time.

Hope this helps.


You should check MySQL's total max_connections allowed. And see the connection is closed or freed properly after executing the every query. The sequence should be like connection open > execute query > connection close. This is one of the reason to slowdown the ajax query. if connection is continue to open without closing and it will exceeds connection limit by ajax request.

example:

$db=new mysqli("localhost","root","","test"); //open the connection$db->query("[Sql commands]");// execute query//some php$db->close(); //close the connection

MySQL system variable max_connections determines the number of connections which MySQL/MariaDB will accept. The default value is 151 connections, which allows 150 normal connections plus one connection from the SUPER account.

The first thing to decide is what new maximum value you want to set for max_connections. There are several considerations to take into account when increasing the number of MySQL/MariaDB connections. The maximum number which can be supported by the system will depend on:

  1. The amount of available RAM.
  2. How much RAM each connection takes (simple queries will require lessRAM than more labor-intensive connections).
  3. The acceptable response time.
  4. According to the MySQL documentation, most Linux systems should beable to support 500-1000 connections without difficulty.

To see the current number of max_connections log in to the MySQL/MariaDB command line client with the command:

mysql> show variables like 'max_connections';

Set the new max_connections value with the command:

mysql> SET GLOBAL max_connections=[desired new maximum number];