Opened 18 years ago

Last modified 17 years ago

#2306 closed defect

Many-to-many queries very slow on MySQL 3.23.58 — at Version 4

Reported by: anonymous Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: dev
Severity: normal Keywords: qs-rf-fixed
Cc: mir@…, freakboy3742@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adrian Holovaty)

We have a MySQL 3.23.58 database with about 1000 Room and Photo objects, connected by a many-to-many relationship.

Given a room r, asking Django to evaluate r.photos.all() runs an explicit join:

SELECT `kords3_photo`.`id`,`kords3_photo`.`image`
FROM `kords3_photo`
LEFT OUTER JOIN `kords3_room_photos` AS `m2m_kords3_photo__room`
ON `kords3_photo`.`id` = `m2m_kords3_photo__room`.`photo_id`
WHERE (`m2m_kords3_photo__room`.`room_id` = 1)
ORDER BY `kords3_photo`.`image` ASC

which takes around 1 second, when the same query rewritten as an implicit join:

SELECT `kords3_photo`.`id`,`kords3_photo`.`image`
FROM `kords3_photo` , `kords3_room_photos`
WHERE `kords3_photo`.`id` = `kords3_room_photos`.`photo_id`
AND `kords3_room_photos`.`room_id` = 1
ORDER BY `kords3_photo`.`image` ASC

would take under a millisecond.

Change History (4)

comment:1 by Adrian Holovaty, 18 years ago

OK, why the heck is Django doing an outer join? This is upsetting. Thanks for pointing this out.

comment:2 by Luke Plant, 18 years ago

I changed it in r2613 due to #1535

I was unaware of serious performance implications - sorry!

comment:3 by mir@…, 18 years ago

Cc: mir@… added

The ORM does have severe problems handling 'or' and 'and' for queries with joins. #1535 only solved parts of them. Look at #1801 and #2080.

comment:4 by Adrian Holovaty, 18 years ago

Description: modified (diff)
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top