Hello.

Now our team works on the project that is built on Drupal 5.3 with many modules plugged in and has a lot of content.
A lot of functionality is already implemented and now I'm researching performance problem.

Number of queries is a real problem.

I implemented some caching for several modules but number of queries still to high.

I'm using devel module for researching.

I found that drupal_lookup_path executed very often so I wrote some enhancement for this Drupal core function.

Drupal code (in includes/path.inc):

    if ($action == 'alias') {
      if (isset($map[$path])) {
        return $map[$path];
      }
      $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path));
      $map[$path] = $alias;
      return $alias;
    }

with my changes:

    // in the static variables definition part
    static $searched = array();

    if ($action == 'alias') {
      if (isset($map[$path])) return $map[$path];
      
      $last_symbol = substr($path, -1, 1);
      if ($last_symbol >= '0' && $last_symbol <= '9') {
        $path_mask = substr($path, 0, -1);
        if (!isset($searched[$path_mask])) {
          $searched[$path_mask] = true;
          $result = db_query("SELECT src, dst FROM {url_alias} WHERE src LIKE '%s_' LIMIT 10", substr($path, 0, -1));
          while ($row = db_fetch_object($result)) {
            $map[$row->src] = $row->dst;
          }
        }
        return isset($map[$path]) ? $map[$path] : false;
      }
      
      $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path));
      $map[$path] = $alias;
      return $alias;
    }

And now research results:
Native Drupal code:

  • 518 queries in 1278.11 milliseconds
  • 518 queries in 1390.44 milliseconds
  • 518 queries in 1282.59 milliseconds
  • 518 queries in 1326.65 milliseconds
  • 518 queries in 1295.69 milliseconds
  • Average is 518 queries in 1314 milliseconds

With my "enhancement"

  • 427 queries in 1387.08 milliseconds
  • 427 queries in 1230.76 milliseconds
  • 427 queries in 1224.74 milliseconds
  • 427 queries in 1248.26 milliseconds
  • 427 queries in 1241.46 milliseconds
  • Average is 427 queries in 1266 milliseconds

So, as you can see number of queries decresed by 17.5%, but total execution time decreased by 3.65% because of LIKE query.

Comments

kbahey’s picture

There is another approach relying on an automatically generated whitelist, so no lookups happen if we know for sure that no alias exists for this "type".

You can get the patch I made for this from #106559, and see the benchmarks there.
--
Drupal performance tuning, development, customization and consulting: 2bits.com
Personal blog: Baheyeldin.com

--
Drupal performance tuning and optimization, hosting, development, and consulting: 2bits.com, Inc. and Twitter at: @2bits
Personal blog: Ba