Well, basically, like the title says.

In both D6 and D7, the final query taxonomy_get_term_by_name() sends to the database is similar to:

SELECT t.tid, t.* FROM term_data t WHERE t.name = 'test';

The name field doesn't have an index on it. The attached patch adds an index on the 'name' field.

CommentFileSizeAuthor
taxonomy_term_data_add_name_index.patch1.04 KBbrianV

Comments

brianV’s picture

Just to clarify, example query is from D6, and should rightly be:

D6:

SELECT t.tid, t.* FROM term_data t WHERE LOWER(t.name) LIKE LOWER('%test');

D7's query is a bit more complex, with a few JOINS, but still needs the index on the 'name' column.

Anonymous’s picture

There are already 2 indexes on the name column, but they're not being used by these queries because the fields are 2nd and 3rd order in a multi-column index.

Do we need an index on name alone? Or can we swap the order of the vid_name index to put name first? If we can't swap, should we separate the vid_name index to be 2 indexes on both fields separately?

We need some DB folks on this. Thanks for bringing it up, brianV.

catch’s picture

Issue tags: +Performance

The reason it doesn't have an index in D6 is because LOWER() queries never use an index anyway. See #279851: Replace LOWER() with db_select() and LIKE() where possible for background.

However now we use LIKE() in D7, and this doesn't use wildcards either, so it should use an index - very nice find.

I'm not aware of anywhere in core where the vid, name index is used - I'd rather see us drop that as well as using the name one (unless I've got that wrong).

catch’s picture

Status: Needs review » Reviewed & tested by the community

I found where vid, name is used - in taxonomy_autocomplete(). So just adding the extra index here is fine, RTBC.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)
Issue tags: -Performance

Automatically closed -- issue fixed for 2 weeks with no activity.