The query in update_load() performs an unnecessary ORDER BY. The results are placed into an associative array where order is unimportant, and the ORDER BY clause causes mysql to create a temporary table to do the sort which costs a significant amount of time over a simple select without the sort.

Also, there is no index on the vid key in the file_revisions table, causing the query to run much slower. On powerful hardware (quad xeon running at 3 ghz with 4G ram), these load queries take 0.3 seconds to perform on mysql 5.0.22 without the help of the mysql query cache when the file revisions table has 60k entries.

I'm not sure how to submit a db schema adjustement for this so I will attach it to this issue. The code for mysql for the index that is needed is:

ALTER TABLE file_revisions ADD INDEX (vid);

Using the second half of the primary key is horrendously slow as compared to using a direct index into the table. These two changes combined cause this query to take less than 0.01 seconds.

Attached patch generated against drupal 4.7.0 using:
diff upload.module fixed_upload.module > upload_load_module.patch

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dries’s picture

Status: Reviewed & tested by the community » Needs work

To submit a SQL schema update:

1. update the database schemas in the database directories.
2. add an entry to updates.inc.

Next, roll a patch.

jhenry’s picture

FileSize
415 bytes

Patch rolled against 4.7.2 and attached.

jhenry’s picture

FileSize
437 bytes

fixed sql patch, missing return.

moshe weitzman’s picture

use unified diff format. see http://drupal.org/diffandpatch (and see the handbook pages above this one)

jhenry’s picture

FileSize
1.54 KB

udiff against current cvs attached

jhenry’s picture

Status: Needs work » Needs review
dopry’s picture

RE: #5

+1.
Code style looks good. It is indeed a frivolous ORDER BY clause as far as I can tell.
And that indexed VID is a good idea.

drumm’s picture

Status: Needs review » Needs work

This is why the order by exists: http://drupal.org/node/57307. (Feel free to re-status and explain why if you think no ordering is still better.)

Dries’s picture

Maybe with the index, the query is already a lot faster? This is a simple query, and should be fast -- both with and without the 'order by'-clause.

jhenry’s picture

The main problem with doing the order by in MySQL is that it creates a temporary table to do the sort because it doesn't use the file id index to generate the result set. It is much faster to do a ksort in php than to create the temporary table in MySQL.

Numbers on my hardware:
1. With no index and order by on the query: 0.3 seconds
2. With no index and no order by on the query: 0.12 seconds
3. With an index and no order by on the query: < 0.01 seconds

ksort in php: < 0.01 seconds

jhenry’s picture

The last number was supposed to be less than 0.01 in my above comment. I guess the comment form doesn't like greater-than signs.

dopry’s picture

@jhenry: what is it with the index and order?

jhenry’s picture

0.01 to 0.05 seconds depending on load (potentially much greater if the temp table memory cache is full, but that's a db issue). I'd be satisfied with only adding the index to the table for performance if the order is important to other users.

jhenry’s picture

Does drupal want this database fix if I keep the ORDER BY?

dopry’s picture

I say index it, but keep the order by.

jhenry’s picture

Status: Needs work » Needs review
FileSize
796 bytes

New patch with order by optimization removed.

drumm’s picture

Version: 4.7.0 » x.y.z
Status: Needs review » Needs work

Looks good, but we need a change in either upload_install() or system_install() (wherever the CREATE TABLE lives)

jhenry’s picture

Status: Needs work » Needs review
FileSize
2.46 KB

updated schemas

jhenry’s picture

Anyone have comments on the patch?

dopry’s picture

just tested on my local install. Update went smoothly nothing seems broken afterwards with mysql.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

seems we have sufficient review here.

drumm’s picture

Status: Reviewed & tested by the community » Needs work

None of these files exist anymore. You are looking for modules/system/system.install.

dopry’s picture

arg. you're correct I'm testing on 4.7.2. sry.

davemicc’s picture

Status: Needs work » Needs review
FileSize
1.55 KB

I believe this patch works against cvs.

dopry’s picture

Status: Needs review » Reviewed & tested by the community

The patch seems identical to the original which had been well recieved and reviewed already. It applies cleanly to HEAD. The index was properly added to mysql db after running the update. The pgsql looks about right.

drumm’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD.

Anonymous’s picture

Status: Fixed » Closed (fixed)