0

我在我的服务器上使用 Django ORM、eav-django、mysql 5.1。当它进行这样的查询时,它完全杀死了我的小型服务器(实际上也是我的 4core 开发机器)。这是因为它指的是自己,即 bon_attr 表吗?

有没有什么好的技巧可以加快速度(缓存除外)?

SELECT DISTINCT `bon_property`.`id`, `bon_property`.`company_id`, `bon_property`.`remote_id`, `bon_property`.`category_id`, `bon_property`.`slug`, `bon_property`.`address_id`, `bon_property`.`address_given_id`, `bon_property`.`agent_contact_id`, `bon_property`.`date_modyfied`, `bon_property`.`status` 

FROM `bon_property` 
INNER JOIN `bon_attr` ON (`bon_property`.`id` = `bon_attr`.`entity_id`) 
INNER JOIN `bon_attr` T5 ON (`bon_property`.`id` = T5.`entity_id`) 
INNER JOIN `bon_attr` T6 ON (T5.`id` = T6.`id`) 
INNER JOIN `bon_attr` T10 ON (`bon_property`.`id` = T10.`entity_id`) 
INNER JOIN `bon_attr` T11 ON (T10.`id` = T11.`id`) 

WHERE ((`bon_attr`.`value_float` <= 1  
AND `bon_attr`.`entity_type_id` = 18 ) 
AND (`bon_attr`.`schema_id` = 23  
AND `bon_attr`.`entity_type_id` = 18 ) 
AND (T6.`value_float` >= 1  
AND T6.`entity_type_id` = 18 ) 
AND (T6.`schema_id` = 23 
AND T6.`entity_type_id` = 18 ) 
AND `bon_property`.`company_id` = 27  
AND (T11.`value_float` >= 200000  
AND T11.`entity_type_id` = 18 ) 
AND (T11.`schema_id` = 42  
AND T11.`entity_type_id` = 18 ) 
AND `bon_property`.`status` = 1 ) 

ORDER BY `bon_property`.`id` ASC;

即使这个查询正在杀死服务器:

SELECT  * FROM `bon_property` 

INNER JOIN `bon_attr` T3 ON (`bon_property`.`id` = T3.`entity_id`) 
INNER JOIN `bon_attr` T4 ON (T3.`id` = T4.`id`) 
INNER JOIN `bon_attr` T8 ON (`bon_property`.`id` = T8.`entity_id`) 
WHERE `bon_property`.`status` = 1

大约 4 分钟后,慢查询日志显示 Query_time: 430.093778 Lock_time: 0.000247 Rows_sent: 0 Rows_examined: 164591659 并且我检查了大量行...
我的配置:http ://dpaste.com/hold/833263/

编辑类似查询的解释:

1   PRIMARY T4  ref PRIMARY,entity_type_id,bon_attr_299a9dd7,bon_attr_...   bon_attr_6f9d19 4   const   9770    Using where; Using temporary; Using filesort
1   PRIMARY T9  ref PRIMARY,entity_type_id,bon_attr_299a9dd7,bon_attr_...   bon_attr_6f9d19 4   const   12037   Using where
1   PRIMARY T3  eq_ref  PRIMARY PRIMARY 4   om.T4.id    1   Using where
1   PRIMARY bon_property    eq_ref  PRIMARY,bon_property_543518c6   PRIMARY 4   om.T3.entity_id 1   Using where
1   PRIMARY T8  eq_ref  PRIMARY PRIMARY 4   om.T9.id    1   Using where; Distinct
5   DEPENDENT SUBQUERY  U1  ref entity_type_id,bon_attr_299a9dd7,bon_attr_6f9d19    entity_type_id  12  const,func,const    1   Using where; Using index
5   DEPENDENT SUBQUERY  U0  eq_ref  PRIMARY PRIMARY 4   func    1   Using where; Using index
4   DEPENDENT SUBQUERY  U1  ref entity_type_id,bon_attr_299a9dd7,bon_attr_147166d7  entity_type_id  8   const,func  17  Using where; Using index
4   DEPENDENT SUBQUERY  U0  eq_ref  PRIMARY PRIMARY 4   func    1   Using where; Using index
3   DEPENDENT SUBQUERY  U1  ref entity_type_id,bon_attr_299a9dd7,bon_attr_6f9d19    entity_type_id  12  const,func,const    1   Using where; Using index
3   DEPENDENT SUBQUERY  U0  eq_ref  PRIMARY PRIMARY 4   func    1   Using where; Using index
2   DEPENDENT SUBQUERY  U1  ref entity_type_id,bon_attr_299a9dd7,bon_attr_147166d7  bon_attr_147166d7   5   const   1   Using where
2   DEPENDENT SUBQUERY  U0  eq_ref  PRIMARY PRIMARY 4   func    1   Using where; Using index

编辑 2 是的,这是一个索引它的问题,甚至更多 EXPLAIN 以及 Duke Silver 都非常有帮助。

4

0 回答 0