Sorry if it was discussed before, I didn't find it. Also, this is not a bug, rather an optimization. But I don't know to mark it.

Please tale a look at drupal_lookup_path() function.

I feel that we can just replace this query $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}')); with reading 1 additional variable (and this variable can be set when we save first alias at the table and when delete last one from the table).

Comments

ultraBoy’s picture

Priority: Minor » Normal

That's an easy fix! Just show your interest, support the idea and I'll make a patch!

Crell’s picture

Roll a patch, submit it, and it can get reviewed. If you're offering to write it, let's see the code! (I'm all for fewer queries.)

Crell’s picture

Category: bug » task
zeta ζ’s picture

Status: Active » Closed (works as designed)

My understanding, please correct me if I’m wrong as I’m not a php guru, is that global and static variables are lost between page views. This code already limits the queries to 1 page-1 with a static variable, so where else can we store this info? in the database?

I think current solution is optimal.

ultraBoy’s picture

Status: Closed (works as designed) » Active

Sorry, since u're "not a php guru" please do not touch issues about code, especially core code.

Crell’s picture

Version: 6.x-dev » 7.x-dev
zeta ζ’s picture

@ultraBoy Not being a guru implies that I am willing to be corrected, not that I know nothing.

I notice that you haven’t touched this issue about code for 7 months – nor has anyone else: I’m just trying to clear-up this queue.

If the fix is so easy, why not submit a patch as you offered, and were swiftly encouraged, to do. If you had done so, it might have got into 6.x. I’m leaving this active, just in case.

Please correct me if I’m wrong, or I might assume I am a php guru after all. My freedom to contribute is protected by the GPL – Don’t touch.

Susurrus’s picture

Relevant code:

  // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases
  if (!isset($count)) {
    $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
  }

This query looks like it's only performed if no path aliases exist. Is this still an issue?

zeta ζ’s picture

This query is only performed if we haven’t counted them yet (this page).

The code works: This issue is about trying to optimise it.

See http://drupal.org/node/34341 – use of isset().

Susurrus’s picture

I agree that this code should probably be reworked so that $count is initialized to null instead of nothing with a check of empty as per #9, I don't see how this could be optimized with regards to static variables reset between page loads, as that's my understanding of PHP. At some point we need to access the DB for information, and I think that's this point.

zeta ζ’s picture

I don’t think it needs reworking: The test makes sure we only count the database once page-1.

I agree that this can’t be optimised, as, between page loads, we have to query the database either to count the rows, or to retrieve the variable from the cache ∴ there is nothing to be gained. See #4.

ultraBoy’s picture

"we have to query the database either to count the rows, or to retrieve the variable from the cache"
This looks false to me. Because I think all the variables are read already, so no db query needed.

Crell’s picture

The variables table is loaded from the database once in one big query and then cached in the cache table. I believe adding a "yes there are path aliases" variable would indeed eliminate the count query. The catch, however, is that it does not solve the main problem with the path alias system: Once enabled, it's one hit per link on the page no matter how many or how few aliases you have. That system desperately needs an overhaul, but none of the attempts to date have worked. I suspect whatever does eventually happen will render this issue a moot point, but in the mean time I would not oppose a patch to switch to a variable early in the D7 cycle, just in case nothing else improves. :-)

treksler’s picture

-1

That variable is going to be a pain to keep in sync.
Any third party module can nuke aliases without updating the variable.

The system should be fixed.
One db hit per link is laughable and needs fixing.

Lots of attempts have worked better than the current system for the vast majority of sites,
however none have handled the few very large sites (like drupal.org) better than the current system,
so nothing has been committed :(

damien tournoud’s picture

Status: Active » Closed (duplicate)

This is a duplicate of (at least) #196862: COUNT(*) is an expensive query in InnoDB..