Skip to content

Instantly share code, notes, and snippets.

@itarato
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarato/7f7976ea9448c68c3673 to your computer and use it in GitHub Desktop.
Save itarato/7f7976ea9448c68c3673 to your computer and use it in GitHub Desktop.

Analysis of title property loads in Drupal

Task: load title of node

Count: 1'000 (nodes)

The measurement happens on a (close to) clean Drupal 7.34 install. Each iteration started with a clear cache - which can be observed where caching is available and first call is much worse. There are 10 different scenarios:

Note: means and medians are only for times, as memory consumptions are constant through iterations.

1: Noop

noop

2: Single SQL query

$var = db_query("
  SELECT title
  FROM {node}
")->fetchCol();

SQL all

3: Separate SQL query

$vars = array();
for ($i = 1; $i <= 1000; $i++) {
  $vars[] = db_query("
    SELECT title
    FROM {node}
    WHERE nid = :nid
  ", array(':nid' => $i))->fetchField();
}

SQL 1

4: Using db_select once

$var = db_select('node', 'n')->fields('n', array('title'))->execute()->fetchCol();

db select all

5: Using db_select individually

$vars = array();
for ($i = 1; $i <= 1000; $i++) {
  $vars[] = db_select('node', 'n')->fields('n', array('title'))->condition('nid', $i)->execute()->fetchField();
}

db select 1

6: Loading multiple nodes at once

$nodes = node_load_multiple(range(1, 1000, 1));
$vars = array();
foreach ($nodes as $node) {
  $vars[] = $node->title;
}

node load all

7: Loading entities at once

$nodes = entity_load('node', range(1, 1000, 1));
$vars = array();
foreach ($nodes as $node) {
  $vars[] = $node->title;
}

entity load all

8: Loading nodes separately

$vars = array();
for ($i = 1; $i <= 1000; $i++) {
  $node = node_load($i);
  $vars[] = $node->title;
}

node load 1

9: Loading nodes separately and using metadata wrapper

$vars = array();
for ($i = 1; $i <= 1000; $i++) {
  $node = node_load($i);
  $entity = entity_metadata_wrapper('node', $node);
  $vars[] = $entity->title->value();
}

entity metadata 1

10: Using service containers to load content factories and load content individually with node load

$f = cw_tool_get_container()->get('node.controller.factory');
$vars = array();
for ($i = 1; $i <= 1000; $i++) {
  $n = $f->initWithId($i);
  $vars[] = $n->data()->title;
}

factory 1

Compare times

time mean

time median

Compare memory consumption

memory mean

memory median

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment