2008-12-08 82 views
1

好吧,我不是在MySQL很大,但我知道指数会帮助我在这里,但是我已经做了一些堵塞,不能找到一个帮...帮助与索引

任何人有任何想法?

explain 
select `users_usr`.`id_usr` AS `id_usr`, 
`users_usr`.`firstname_usr` AS `firstname_usr`, 
`users_usr`.`lastname_usr` AS `lastname_usr`,`users_usr`.`social_usr` AS `social_usr`,`users_usr`.`address1_usr` AS `address1_usr`, 
`users_usr`.`address2_usr` AS `address2_usr`,`users_usr`.`city_usr` AS `city_usr`,`users_usr`.`state_usr` AS `state_usr`,`users_usr`.`zip_usr` AS `zip_usr`, 
`users_usr`.`email_usr` AS `email_usr`,`credit_acc`.`given_credit_acc` AS `given_credit_acc`,`credit_acc`.`credit_used_acc` AS `credit_used_acc`, 
`credit_acc`.`date_established_acc` AS `date_established_acc`,`credit_acc`.`type_acc` AS `type_acc`,`credit_acc`.`bureau_status_acc` AS `bureau_status_acc`, 
sum((`credit_balance`.`debit_acc` - `credit_balance`.`credit_acc`)) AS `balance` 



from (((`users_usr` 
left join `credit_acc` on((`users_usr`.`id_usr` = `credit_acc`.`uid_usr`))) 
left join `cfc_cfc` on((`credit_acc`.`id_cfc` = `cfc_cfc`.`id_cfc`))) 
join `credit_acc` `credit_balance` on((`credit_balance`.`credit_used_acc` = `credit_acc`.`id_acc`))) 

where ((`credit_acc`.`type_acc` = _latin1'init') 
and (`credit_acc`.`status_acc` = _latin1'active') 
and (`credit_acc`.`linetype_acc` = _latin1'personal')) 

group by `credit_balance`.`credit_used_acc` order by `users_usr`.`id_usr` 

给我

id select_type table   type possible_keys      key    key_len ref         rows Extra       
------ ----------- -------------- ------ ----------------------------------- --------------- ------- --------------------------------- ------ ------------------------------- 
    1 SIMPLE  credit_balance index credit_used_acc,cash_report_index credit_used_acc 40  (NULL)        14959 Using temporary; Using filesort 
    1 SIMPLE  credit_acc  eq_ref PRIMARY,type_acc,type_acc_2,uid_usr PRIMARY   8  cc.credit_balance.credit_used_acc  1 Using where      
    1 SIMPLE  cfc_cfc   eq_ref PRIMARY        PRIMARY   4  cc.credit_acc.id_cfc     1 Using index      
    1 SIMPLE  users_usr  eq_ref PRIMARY,id_usr      PRIMARY   4  cc.credit_acc.uid_usr     1 



Table  Non_unique Key_name   Seq_in_index Column_name    Collation Cardinality Sub_part Packed Null Index_type Comment 
---------- ---------- ----------------- ------------ ----------------------- --------- ----------- -------- ------ ------ ---------- ------- 
credit_acc   0 PRIMARY      1 id_acc     A    14016 (NULL) (NULL)   BTREE    
credit_acc   1 type_acc      1 type_acc     A     11 (NULL) (NULL) YES  BTREE    
credit_acc   1 type_acc      2 date_acc     A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 type_acc      3 affiliate_aff   A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 type_acc_2     1 type_acc     A     11 (NULL) (NULL) YES  BTREE    
credit_acc   1 type_acc_2     2 date_acc     A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 type_acc_2     3 complete_acc    A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 type_acc_2     4 commission_refunded_acc A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 credit_used_acc    1 credit_used_acc   A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 credit_used_acc    2 id_acc     A    14016 (NULL) (NULL)   BTREE    
credit_acc   1 credit_used_acc    3 type_acc     A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 uid_usr      1 uid_usr     A     7008 (NULL) (NULL) YES  BTREE    
credit_acc   1 cash_report_index    1 credit_used_acc   A     7008 (NULL) (NULL) YES  BTREE    
credit_acc   1 cash_report_index    2 type_acc     A    14016 (NULL) (NULL) YES  BTREE    
credit_acc   1 cash_report_index    3 date_established_acc  A    14016 (NULL) (NULL) YES  BTREE    
+0

最后加入什么?之前没有看到! – benlumley 2008-12-08 21:46:56

回答

0

你的EXPLAIN输出显示你已经拥有的索引是可能是有用的,但查询引擎已决定不使用它们。

http://dev.mysql.com/doc/refman/5.0/en/using-explain.html说:

使用临时

要解决查询,MySQL需要来 创建一个临时表来保存 结果。如果 查询包含以不同方式列出列的GROUP BY和ORDER BY 子句,则通常会发生这种情况。

您的查询包括:

GROUP BY `credit_balance`.`credit_used_acc` 
ORDER BY `users_usr`.`id_usr` 

你的名字在这两个条款不同的列,所以这意味着查询需要一个临时表,并在磁盘上的排序结果。磁盘I/O是SQL性能的主要敌人。这可能比通过应用索引可以弥补的性能更不利。

所以我建议尝试删除ORDER BY子句,看看它是否摆脱了“使用临时”评论。

编辑好吧,我已经做了一些更多的测试,并仔细查看了你的表格。

我认为你有很多索引是多余的,不适用于这个当前查询。这可能是索引对其他查询有用,或者它们可能只是您实验中的剩菜。

一个观察是,您加入cfc_cfc是不必要的,但我从您的评论中看到,您已将它取出。尽管它似乎没有修复EXPLAIN报告。

另一种观察是,你的LEFT JOIN是不必要的;它可能是INNER JOIN,因为无论如何,这些列的WHERE子句中都有条件。它不是一个外连接的目的,外连接往往会变慢。

索引可能对您在连接条件中使用的列或行限制或GROUP BY或ORDER BY子句有用。 MySQL不能在给定查询中的每个表中使用多个索引,因此定义复合索引是有意义的。但是您应该在查询中使用的列上定义索引。如果您只使用三列索引的第2列和第3列(即不是索引中的第一列),则索引无效。

当然也有含蓄的所有主键和外键约束创建的索引,但这里是我创建的唯一额外的索引:

KEY columns_used_in_query (uid_usr, type_acc, status_acc, linetype_acc), 

,如果你把你的情况应该没有关系的优化方案在WHERE子句或连接条件中,只要它们不是外连接的条件的一部分。但是我注意到所有其他事物都是平等的,优化器似乎会根据您首先定义的那个选择一个索引!

我仍然没有将EXPLAIN报告中的临时表& filesort注释掉,但我认为这些更改会加快查询速度。

+0

好吧,我杀了命令,并没有改变任何查询。但是,我确实在JUST status_acc上创建了一个索引,它使我从所检查的行的100%中抽取了大约20%,但我仍然有一个临时文件和文件夹。 – bbutle01 2008-12-08 21:43:14

0

收回一些,重新开始。

这是我对你的查询的解释。

uu.select uu.id_usr,
uu.firstname_usr,
uu.lastname_usr,
uu.social_usr,
uu.address1_usr,
uu.address2_usr,
uu.city_usr,
UU .state_usr,
uu.zip_usr,
uu.email_usr,
ca.given_credit_acc,
ca.credit_used_acc,
ca.date_established_acc,
ca.type_acc,
ca.bureau_status_acc,
总和(cb.debit_acc - cb.credit_acc)AS balance

从users_usr AS UU

左加入credit_acc与CA上uu.id_usr = ca.uid_usr

加入credit_acc AS上ca.credit_used_acc CB = ca.id_acc

其中ca.type_acc =' INIT”
和ca.status_acc = '积极的'
和ca.linetype_acc = '个人' 由cb.credit_used_acc
为了通过uu.id_usr

+0

拉出了cfc_cfc表,忘了我不再使用它了,没有什么帮助,因为它只是在那里看着一排,但最好是更干净。 – bbutle01 2008-12-08 21:58:53

0

你的查询(具有该优化的部分

组联接,何在,团体和排序)可以简化为:

SELECT
uu.select uu.id_usr,
ca.given_credit_acc,
ca.c redit_used_acc,
ca.type_acc,
sum(cb.debit_acc - cb。credit_acc)AS balance

FROM
users_usr AS UU

LEFT JOIN
credit_acc AS上uu.id_usr = ca.uid_usr
AND ca.type_acc CA = '初始化'
AND ca.status_acc = '主动'
AND ca.linetype_acc = '个人'
- credit_acc需要在uid_usr + type_acc _ status_acc索引

JOIN
credit_acc AS上ca.credit_used_acc CB = ca.id_acc
- credit_acc需要BY uu.id_usr

注意,我把上credit_used_acc指数

GROUP BY cb.credit_used_acc
ORDER WHERE子句并将其移入JOIN中 - MySQL似乎喜欢这种调整。

请注意关于索引的评论。看看你是否能够简化工作(并优化),然后添加其他字段。

您怎么看比尔?