26
On a web site, I am using django to make some requests :
The django line :
CINodeInventory.objects.select_related().filter(ci_class__type='equipment',company__slug=self.kwargs['company'])
generates a MySQL query like that :
SELECT *
FROM `inventory_cinodeinventory`
INNER JOIN `ci_cinodeclass` ON ( `inventory_cinodeinventory`.`ci_class_id` = `ci_cinodeclass`.`class_name` )
INNER JOIN `accounts_companyprofile` ON ( `inventory_cinodeinventory`.`company_id` = `accounts_companyprofile`.`slug` )
INNER JOIN `accounts_companysite` ON ( `inventory_cinodeinventory`.`company_site_id` = `accounts_companysite`.`slug` )
INNER JOIN `accounts_companyprofile` T5 ON ( `accounts_companysite`.`company_id` = T5.`slug` )
WHERE (
`ci_cinodeclass`.`type` = 'equipment'
AND `inventory_cinodeinventory`.`company_id` = 'thecompany'
)
ORDER BY `inventory_cinodeinventory`.`name` ASC
The problem is that for only 40 000 entries in the main table, it takes 0.5 seconds to process.
I checked all indexes, create the ones that is required for sorting or joinning : I still have problem.
The funny thing is that if I replace the last INNER JOIN by a LEFT JOIN, the request is 10x faster ! Unfortunately, As I am using django for requesting, I do not have access to the SQL requests it generates (I do not want to do raw SQL myself).
for the last join as an "INNER JOIN" the EXPLAIN gives:
+----+-------------+---------------------------+--------+----------------------------------------------------------------------------------------------------------+------------------------------------+---------+------------------------------------------------+-------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len |