6

Looking at Drupal code, I notice that sometimes a query is being executed using array('target' => 'slave') as options, and I don't understand in which case that should be done.

I thought it was done when query where executed in parallel to others, but then I thought that could be said for almost any query executed by Drupal, when more than one user is looking at the pages of a site.
In which cases should array('target' => 'slave') be used?

  // node_update_index()
  $result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit, array(), array('target' => 'slave'));
// search_index()
$node = db_query('SELECT title, nid, vid FROM {node} WHERE nid = :nid', array(':nid' => $linknid), array('target' => 'slave'))->fetchObject();

Also, looking at Drupal core code, why is array('target' => 'slave') sometimes used, and sometimes no?

2

SELECT (read) queries can go to the slave, other such as UPDATE and DELETE (write) queries should go to master then propagate across to the slave database.

Also, looking at Drupal core code, why is array('target' => 'slave') sometimes used, and sometimes no?

That's probably just historical / legacy reasons due to the nature of how the Drupal code base is gradually committed. Potentially any SELECT queries could target the slave database.

  • 1
    In theory I think this answer is right. In practice, sites must consider the amount of lag they experience between the primary and the read replica (slave) and the risk that a query sent to the read replica will show outdated information. Queries run in cron or as part of batch operations can likely hit the read replica. Retrieving an entity right after that entity has been edited should likely be retrieved from the primary database. – greggles May 9 '17 at 23:51
0

I guess this depends on your database architecture. If you have 1 (ore more) dedicated slave database servers (in read only), then it makes sense to send 'safe' read queries to them. This frees your master database server up to deal with writes and hopefully gives your application better performance overall.

You also may be interested in this related post on getting drupal core to send more queries towards them How to get core to leverage a MySQL master/slave configuration?

  • 1
    Drupal sets the target to "slave" without knowing the database architecture. The question is why sometimes Drupal sets it, and sometimes doesn't set it. – kiamlaluno Oct 13 '12 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.