许多连接的MySQL计数需要太长时间

时间:2016-01-27 10:36:33

标签: php mysql symfony doctrine-orm doctrine

在我的项目中,我进行了3次查询,其中一次花费了太多时间 我使用doctrine 2和PHP,并使用QueryBuilder发出请求 我有车辆清单(900 000条记录),客户清单(600 000条记录),实用工具(600 000条记录)和_client_has_groupe_etablissement(600 000条记录)。
在我的第一个查询中,我计算了车辆总数。需要0.25s =>完美。
我的secound查询允许通过具有多个连接的不同过滤器来查找车辆。需要0.5s =>完美。
这是查询:

SELECT [MANY THINGS] 
FROM vehicule v0_ 
INNER JOIN categorie_vehicule c1_ ON v0_.idCategorieVehicule = c1_.idCategorieVehicule 
INNER JOIN energie e2_ ON v0_.idEnergie = e2_.idEnergie 
LEFT JOIN vehicule_lastrdv v6_ ON (v6_.idVehicule = v0_.idVehicule) 
LEFT JOIN client c4_ ON v0_.idUtilisateur = c4_.idUtilisateur 
LEFT JOIN utilisateur u5_ ON c4_.idUtilisateur = u5_.idUtilisateur 
LEFT JOIN ​_client_has_groupe_etablissement t3_​ ON c4_.idUtilisateur = t3_.idUtilisateur 
LEFT JOIN groupe_etablissement g7_ ON t3_.idGroupeEtablissement = g7_.idGroupeEtablissement 
WHERE v0_.actifVehicule = 1 
ORDER BY v0_.idVehicule DESC 
LIMIT 207 
OFFSET 0;

最后一个查询是上一个请求的总返回行数:

SELECT COUNT(v0_.idVehicule) AS sclr_0 
FROM vehicule v0_ 
INNER JOIN categorie_vehicule c1_ ON v0_.idCategorieVehicule = c1_.idCategorieVehicule 
INNER JOIN energie e2_ ON v0_.idEnergie = e2_.idEnergie 
LEFT JOIN vehicule_lastrdv v3_ ON v3_.idVehicule = v0_.idVehicule 
LEFT JOIN client c4_ ON v0_.idUtilisateur = c4_.idUtilisateur 
LEFT JOIN utilisateur u5_ ON c4_.idUtilisateur = u5_.idUtilisateur 
LEFT JOIN ​_client_has_groupe_etablissement t6_​ ON c4_.idUtilisateur = t6_.idUtilisateur 
LEFT JOIN groupe_etablissement g7_ ON t6_.idGroupeEtablissement = g7_.idGroupeEtablissement 
WHERE v0_.actifVehicule = 1;

但这个查询需要20秒

这是对此请求的解释,但似乎都很好

id  select_type table                   partitions  type?   possible_keys                                                         key                               key_len   ref                               rows    Extra
1   PRIMARY     v0_                                 ref     IDX_D0599D4B9F7AC00B,IDX_D0599D4B323EB152,idx_vehicule_actifVehicule  idx_vehicule_actifVehicule        1         const                             440436  100.00  Using where
1   PRIMARY     e2_                                 eq_ref  PRIMARY                                                               PRIMARY                           4         r4-p19d.v0_.idEnergie             1       100.00  Using index
1   PRIMARY     c1_                                 eq_ref  PRIMARY                                                               PRIMARY                           4         r4-p19d.v0_.idCategorieVehicule   1       100.00  Using index
1   PRIMARY     <derived2>                          ref     <auto_key0>                                                           <auto_key0>                       5         r4-p19d.v0_.idVehicule            10      100.00  
1   PRIMARY     c4_                                 eq_ref  PRIMARY                                                               PRIMARY                           4         r4-p19d.v0_.idUtilisateur         1       100.00  Using index
1   PRIMARY     u5_                                 eq_ref  PRIMARY                                                               PRIMARY                           4         r4-p19d.c4_.idUtilisateur         1       100.00  Using index
1   PRIMARY     t3_                                 ref     PRIMARY,IDX_30B01ADE5D419CCB                                          PRIMARY                           4         r4-p19d.c4_.idUtilisateur         1       100.00  Using index
1   PRIMARY     g7_                                 eq_ref  PRIMARY                                                               PRIMARY                           4         r4-p19d.t3_.idGroupeEtablissement 1       100.00  Using index
2   DERIVED     rdv                                 index   PRIMARY,IDX_86645D0C4FFC60ED,idx_rdv_idRDV_dateRDV_idAgenda           idx_rdv_idRDV_dateRDV_idAgenda    17        981941                                     33.33  Using where; Using index; Using temporary; Using filesort
2   DERIVED     agenda                              eq_ref  PRIMARY,IDX_2B41CD41EA190502                                          PRIMARY                           4         r4-p19d.rdv.idAgenda              1       100.00  Using where
2   DERIVED     etablissement                       eq_ref  PRIMARY                                                               PRIMARY                           4         r4-p19d.agenda.idEtablissement    1       100.00  
2   DERIVED     _vehicule_has_controle              ref     IDX_1EAA071A9F61066B                                                  IDX_1EAA071A9F61066B              5         r4-p19d.rdv.idRDV                 1       100.00

我想知道如何直接或使用doctrine减少MySQL中的执行时间。

我已经尝试了SQL_CALC_FOUND_ROWS,但需要相同的时间 还有countRows或在学说中计算

0 个答案:

没有答案