Getting Views to do SELECT statements on a slave db with Pressflow

Events happening in the community are now at Drupal community events on www.drupal.org.
tom friedhof_'s picture

I was looking for an easy way to get views to pull select statements from a slave database with pressflow without having to hack the views module. I tried doing this through a hook_views_data_alter(), but that didn't seem to work.

I added another db connection to my settings.php file like so:

$db_url['default'] = "mysql://user:pass@masterdb/mydb;
$db_url['slave'] = "mysql://user:pass@slavedb/mydb;
$db_url_slave = "mysql://user:pass@slavedb/mydb;

and then in a custom module I did this: (mymodule.views.inc)

/**
* Modify all views to query the Slave database
*/
function mymodule_views_data_alter(&$data) {
  foreach($data as $table => $value) {
    $data[$table]['table']['base']['database'] = 'slave';
  }
}

I set the module weight to 200, still with no luck. Views was functioning, but wasn't using the database connection that I created called $db_url['slave'].

I ended up just hacking views and changed any db_query and db_query_range to the respected pressflow db_query_slave and db_query_range_slave for any SELECT queries in the views.inc file.

I'm curious if anyone has come up with a cleaner solution of doing this. I have attached a patch for the views.inc file in case it benefits anyone.

AttachmentSize
views_slave.patch_.txt2.78 KB

Comments

I will try to test this soon

rgristroph's picture

This looks good, I will try to test it soon. I will have to set up a pair of mysql replication servers to do it.

One strategy I will use in testing, is I will stop replication to simulate a very large lag in the slave.

Any news on this subject

arielkung's picture

Any news on this subject guys?

Right hook?

Here's an article on this

q0rban's picture

Here's an article on this very subject for you. :)
http://www.lullabot.com/articles/querying-slave-database-with-views

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: