PITS Blog
  • Home
  • Services
    • Dedicated team Have your own development team at our location in Kerala, India. We tailor our employees individually to your project.
    • Website & web shop Get into the online business and sell your products around the clock. Or convince yourself with a modern website on desktop and mobile devices.
    • Application development We develop very individual web or software applications for you for a wide variety of needs.
    • Hybrid or native iOS & Android Apps Online business is often done on the go today, and the trend is rising. Invest now and make your software mobile-compatible.
  • Credentials
  • Technology
  • Process
  • About us
  • Contact
  • White papers
  • Jobs
  • Blog
  • Startups
�
Avatar

Reduce page load time using asynchronous database queries

By Francis Varghese on June, 24 2016
demo post

By Visakh R N

WordPress and .NET

By Priyanka K

Roadmap to become a DevOps Engineer

By Francis Varghese

Gutenberg - More Than Just An Editor

By Priyanka K

Traditionally in MySQL, we execute queries sequentially i.e. queries will be executed one after other only. So if we are executing multiple queries then each query will be blocked until and unless the previous query is executed. This works to a limit, but as the system expands, this method ends up having a significant impact on overall system performance and latency.

Suppose you are developing a web application and you are in a situation to fetch a large amount of data from database before page load. In such situations your webpage is going to take a long time to load. For example if you are firing 4 database queries to fetch the required data, and each query is taking around 2 seconds (Oh yeah! a big query) your total query execution itself is going to cost you around 8 seconds if you execute those queries sequentially.

An ideal solution for this problem is to use asynchronous database queries to speed up the webpage.

Using asynchronous database queries you will be able to execute these 4 queries mentioned in the above example in 4 parallel threads. An example on how to do it using MySQLi and PHP is mentioned below.

<?php

$query[] = “YOUR FIRST QUERY”;
$query[] = “YOUR SECOND QUERY”;
$query[] = “YOUR THIRD QUERY”;
$query[] = “YOUR FOURTH QUERY”;

$linksArr = array();
$queryCount = count($query);
for ($i = 0; $i < $queryCount; $i++) {
$link = mysqli_connect(“dbHost”, “dbUser”, “dbPassword”, “dbName”);
$link->query($query[$i], MYSQLI_ASYNC);
$linksArr[] = $link;
}

$resultArr = array();
$processed = 0;
do {
$links = $errors = $reject = array();
foreach ($linksArr as $link) {
$links[] = $errors[] = $reject[] = $link;
}
if (!mysqli_poll($links, $errors, $reject, 1)) {
continue;
}
foreach ($links as $link) {
if ($result = $link->reap_async_query()) {
$resultArr[] = $result->fetch_all(MYSQLI_ASSOC);
if (is_object($result))
mysqli_free_result($result);
} else
die(sprintf(“MySQLi Error: %s”, mysqli_error($link)));
$processed++;
}
} while ($processed < count($linksArr));

foreach ($linksArr as $link) {
mysqli_close($link);
}

print_r($resultArr);

Above code will give you a full result set array which will have results of 4 queries you just executed. Each query will be starting in each db thread without waiting for the previous query result, thus saving total execution time. Using this method we can significantly reduce the query execution time to below 4 seconds instead of initial 8 seconds. Every solution will have a minor flaw and the inconvenience that we will be facing here is that we need to open 4 connections instead of one because of the lack of connection pooling in PHP.

NB : Some functions used above will only work with `mysqlnd` (mysql native driver) support.

We'd love to hear from you.

Contact us

Switzerland
thomas(at)pitsolutions(dot)com
+41 (0) 43 558 4360

India
enquiries(at)pitsolutions(dot)com
+91 (0) 471 270 0615 / 715

UAE
mohammed(at)pitsolutions(dot)com
+971 (0) 4 359 8610

USA
arnab(at)pitsolutions(dot)com
+1 (0) 703 945 8076

Copyright © 2019PIT Solutions AG.An ISO 9001:2015 certified company. All Rights Reserved

SCROLL TO TOP