Experiencing a delay when connecting to the MySQL database, the process is taking

Why does my InnoDB database connection experience intermittent delays of an exact number of seconds?

A little context

In my MySQL database, I have a small 'users' table with 150 records and a 'connections' table that continues to grow (up to 22,500 records). The total size of the tables and indexes is less than 5 MB.

During user activity, some changes are made to the 'connections' table, with approximately 5-50 records being altered or added. Overall, the application performs well with load times averaging below ~100 ms.

However, there are instances where performance is not consistent.

Diving deeper

Even under light loads, page load times can suddenly spike from 60 ms to anywhere between 1,000 ms and 10,000 ms.

By utilizing Symfony's profiler, I identified that more than 95% of the delay was attributed to the 'getRepository' statement, despite individual queries taking only around 1 ms each. This discovery led me to suspect that the slow component could be the process of connecting to the database. To investigate further, I created a test script that regularly establishes connections to the database.

<?php // execute this script via command line using watch
$a = microtime(true);
$pdo = new PDO('mysql:host=127.0.0.1;dbname=mydb','myuser','mypass');
file_put_contents( 'performance.txt', (microtime(true)-$a).PHP_EOL, FILE_APPEND );

The perplexity

Upon testing, I observed that establishing a connection consistently took either 1-3 ms, 1,001-1,003 ms, 2,001-2,003 ms, 3,001-3,003 ms, and so forth. Each time measurement amounted to an integer number of seconds in addition to the regular time elapsed. Surprisingly, there were no intermediary values like 400 ms or 800 ms. Interestingly, when there were no write operations occurring, the connection process happened almost instantaneously. However, as soon as writes were initiated through the application, the extended time intervals surfaced.

What could explain this peculiar behavior? Could it be related to the functionality of the InnoDB page_cleaner, operating at 1,000 ms intervals?

More importantly, how can this issue be resolved? While considering transitioning to MEMORY tables, I believe there must be more sophisticated solutions available.

UPDATE

As requested, here are the variables and global status.

Additional details: My direct connection is to 127.0.0.1 (referencing the code snippet above), and experimentation with the skip-name-resolve flag did not yield any improvements. Notably, the server in use is Debian-based.

UPDATE 2

Further investigation revealed that the delays occurred precisely after 1, 3, 7, or 15-second intervals. Remarkably, these increments follow a consistent pattern: 1 second, +2s, +4s, +8s. Is it possible that this points to a timeout-related issue?

Answer №1

It's not unusual for reverse DNS lookup to take an extended period of time. This delay can be exacerbated by the size of the host_cache, leading to unpredictable behavior.

To disable reverse DNS lookup, simply add the following line to your my.cnf file:

[mysqld]
skip-name-resolve

Keep in mind that all permissions must be granted by IP address rather than hostname if you decide to make this change.

For more information, refer to the official MySQL manual.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with the positioning of the datepicker is not functioning properly

I'm currently facing an issue with the position of a date picker plugin from jquery. The search box on my website allows users to select a range of dates using the date picker, but when enabled, the date picker appears at the bottom left corner of the ...

How can I eliminate the price column from my WooCommerce email notifications?

I am currently enhancing the layout of customer emails generated by my woocommerce store. Successfully, I have removed the subtotal and total rows, as well as eliminated the word "Price" from the price column header row. Nevertheless, despite numerous atte ...

Troubleshooting: Error Connecting PHP to MySQL on Localhost Using Dreamweaver

Attempting to access my localhost database, which I created with phpMyAdmin, from Dreamweaver has been a bit challenging. While I successfully set up the site and server, I encountered an issue when trying to select the database in Dreamweaver. The error m ...

insert information from json file into mysql database

Do you see any issues with the code? It seems like the data is not being inserted into the database, and I can't figure out what's causing the problem. Could someone lend a hand? Here's the PHP code: ...

The json_decode function in PHP along with AJAX is returning an error stating "Invalid

Shown below is a sample json string data [{"ratetype":"Y","hotelPackage":"N","roomtype":"Deluxe Double Room","roombasis":",Complimentary Wi-Fi Internet, Breakfast, ","roomTypeCode":"0000015468","ratePlanCode":"0000120709","ratebands":{"validdays":"1111111 ...

I'm looking to add autocomplete functionality to a text input in my project, and then retrieve and display data from a MySQL database using

Looking to enhance user experience on my form where users can select inputs. Specifically, I want to implement a feature where as the user starts typing in a text input field with data from a MYSQL database, suggestions will be displayed. The form is locat ...

Troubleshooting a problem with jQuery and Drupal behavior when the document is ready

Encountering a strange issue with my jQuery code within Drupal 7. I implemented the following snippet : (function ($) { Drupal.behaviors.exampleModule = { attach: myPopUpFunction..... })(jQuery); Interestingly, on Mac browsers, the popup loads af ...

PHP and MySQL powered blogging platform

If I am creating a blog system and I would like to incorporate 'tags' into my blogposts. These tags function in a way that is similar to what you see here, as they help to categorize posts with similar topics. My plan is to store the tags in the ...

Communicating between PHP chat client and server

Currently, I am developing a basic PHP web chat application that interacts with a MySQL database. The communication is facilitated through AJAX requests - when a user posts a message, it gets saved in the database. function sendData(){ var textData = $(& ...

Guide on running a MySQL query in an asynchronous function in a Node.js application

Encountering some challenges while using MySQL in a node/express JS application. It seems that when trying to access the MySQL database within an asynchronous function, the code will 'skip over' the SQL query and run synchronously. This is the co ...

Transmitting document through HTML form using Laravel integration with Mailgun API

I'm currently working on a feature where users can select a file via an HTML form and have it sent as an email attachment using Mailgun. I want to avoid storing the file on my server. The goal is to allow for uploading various file types such as PNG, ...

Storing various types of content in a database using Django

Looking for input on managing user consent for a web application built with Python and Django. I need users to agree to the use of their personal data, with this consent being stored in the database. The consent model is structured as follows: class Cons ...

Tips for showing/hiding textboxes based on select options:

I am currently working on a project that allows users to enter their personal information. I need help figuring out how to show or hide textboxes based on the selection made in a dropdown menu. The dropdown menu in question is for marital status. The opt ...

Show the json data on a PHP table

I've been attempting to showcase JSON content in a PHP table, but I keep encountering errors. It seems like there are some syntax issues that I can't quite pinpoint. Any suggestions on what needs to be modified? PS. I'm using the Slim frame ...

Facing challenges in PHP with setting headers and sending emails via cPanel

For PHP, I'm struggling to set the header for the from address and have tried various options: The correct code is provided below, but it doesn't seem to be attached to the header. Although the code appears to be default from, it doesn't c ...

Converting Php into a recursive function

I am currently working with two classes. the ArrayCompare class: <?php namespace App\Tools\RegexExtract; class ArrayCompare { public function compare(Array $arrayToCompare) { $elementData = new ElementMetaData(); ...

Utilizing JQuery Ajax in Synchrony with UI Dialogs

I am struggling with creating an ajax pop up when a button is clicked in the order include of my main smarty template. Despite including jquery, jquery ui, and the necessary js file script, the page just reloads instead of displaying the pop-up as intended ...

Running a Laravel artisan command on a schedule of every 5 seconds

My current project involves working with a system that sends webhooks whenever there is a change to a resource within the system. These webhooks contain the ID of the updated resource. For instance, if someone modifies product ID 1234 in the system, my ser ...

Wondering how to access the basic SCORM report on Moodle?

I recently created an application using php.ini where I integrated Moodle sessions. Currently, my challenge lies in displaying basic reports of the course scores in my custom application. Once a user enrolls for a course, I aim to showcase their details on ...

In PHP, you can use the preg_match function to match

Can someone help me create a regular expression that will find the first N characters in an array until a tab or comma separation is encountered? This is what the array looks like: array ( 0 => '001,Foo,Bar', 1 => '0003,Foo,Bar&a ...