Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This project is not covered by Drupal’s security advisory policy.
INTRODUCTION
The Asynchonous MySQL driver for Drupal, provides the option to perform asynchronous queries via DBTNG.
Still a work in progress, but seems to work ok.
REQUIREMENTS
- Drupal 7
- MySQL
INSTALLATION
- Copy (or symlink) the database subdir to the database driver location, so that Drupal can locate and use it.
- %> cd /path/to/drupal/path/to/module
- %> cp -r database /path/to/drupal/includes/database/mysql_async
- Clear cache.
CONFIGURATION
- Before enabling, remember to clear the cache.
- To enable change the driver in settings.php
<?php
$databases['default']['default']['driver'] = 'mysql_async';
$databases['default']['default']['async_max_connections'] = 30;
$databases['default']['default']['async_min_connections'] = 5;
// Use async queries for all request. Highly experimental.
$databases['default']['default']['default_options'] = array('async' => TRUE);
?>
USAGE
Perform two heavy queries asynchronously:
<?php
// Initiate queries.
$q1 = db_query("MY HEAVY QUERY ONE", array(), array('async' => TRUE));
$q2 = db_query("MY HEAVY QUERY TWO", array(), array('async' => TRUE));
// Wait no more than 30 seconds for queries to complete.
$until = microtime(TRUE) + 30;
do {
$done = $q1->rowCount(TRUE) !== NULL && $q2->rowCount(TRUE) !== NULL;
} while (!$done && microtime(TRUE) < $until);
// Get results.
$r1 = $q1->rowCount(TRUE) !== NULL ? $q1->fetchAll() : NULL;
$r2 = $q2->rowCount(TRUE) !== NULL ? $q2->fetchAll() : NULL;
var_dump($r1);
var_dump($r2);
?>
PREFETCH CACHE
The Drupal Mysql Async driver also contains a cache backend suitable for Cache Heuristic.
To configure:
<?php
// Add async support to the default database driver.
$databases['default']['default']['driver'] = 'mysql_async';
// Add cache backends. Note that Cache Consistent is necessary due to cache
// operations are not performed in the default database connection connection,
// and is thus potentially outside a transaction.
$conf['cache_backends'][] = 'sites/all/modules/contrib/cache_heuristic/cache_heuristic.inc';
$conf['cache_backends'][] = 'sites/all/modules/contrib/cache_consistent/cache_consistent.inc';
$conf['cache_backends'][] = 'sites/all/modules/contrib/mysql_async/mysql_async.cache.inc';
// Setup backends.
$conf['cache_default_class'] = 'HeuristicCache';
$conf['heuristic_cache_default_class'] = 'ConsistentCache';
$conf['consistent_cache_default_class'] = 'DrupalDatabaseCacheAsync';
?>
FAQ
- Q: Does my module need to require this module as a dependency?
- A: No. Your code can be backwards compatible. By using rowCount(TRUE) to check if a request has finished, your code will still work when not using the mysql_async driver.
CREDITS
Inspired by APDQC - https://www.drupal.org/project/apdqc
Project information
- Seeking new maintainer
The current maintainers are looking for new people to take ownership. - Maintenance fixes only
Considered feature-complete by its maintainers. - Module categories: Developer Tools
- Created by gielfeldt on , updated
- This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.