2

我有一个复杂的查询,其中涉及 LEFT JOIN 语句中的子查询。处理时间约为 2 秒。如果我用临时表替换子查询,我会遇到相同的 ~2s。如果我在临时表中创建索引,我有 ~0.05s。

我试图弄清楚为什么我不能直接在 LEFT JOIN STATEMENT 中使用 Mysql USE INDEX 或 FORCE INDEX。

这是一个简单的查询,它将返回“检查您的 mysql 语法”错误。

SELECT * 
FROM table1 t1 
LEFT JOIN (SELECT id_project FROM table2) t2 
FORCE INDEX FOR JOIN (id_project) 
   ON (t2.id_project = t1.id_project);

为什么我不能将 FORCE INDEX 与 LEFT JOIN 子查询一起使用?

--edit:这是返回mysql错误的实际查询

SELECT  
    p.id_projet, pa.piste_affaire, ty.type, p.e_force, gr.groupe, p.client, p.intitule_affaire, 
    ag.agence, st.statut, p.winratio, p.justification_nogo, p.webdoc, sp.sponsor, dis.domain_is, 
    cons.constructeur, p.date_chgt_statut, p.date_creation, 
    p.ca_estimatif, p.tcv, p.marge,
    tarp.target_price, p.marketable_price, 
    p.remise_propal_initiale, p.remise_propal_reel,
    p.date_de_signature, p.debut_build, p.fin_build,
    his.date, his.login, co.contrat, 
    p.duree_contrat, p.fqp, cap.capitalisation, 
    p.international, p.risk_management, p.asi, 
    GROUP_CONCAT(com.commentaire,',') AS COMMENT

FROM  
    piste_affaire pa, statut st, TYPE ty, agence ag, 
    domain_is dis, sponsor sp, constructeur cons, groupe gr, 
    target_price tarp, contrat co, historique his, capitalisation cap, 
    projet p 
    LEFT JOIN commentaire com ON (p.id_projet=com.id_projet)
    LEFT JOIN bizdev bizd  ON (p.id_bizdev = bizd.id_bizdev )
    LEFT JOIN (SELECT eco_projet.id_projet, 
                GROUP_CONCAT(
                CONCAT(ecosysteme) 
                ORDER BY ecosysteme SEPARATOR ', ' ) AS ecosysteme
                FROM ecosysteme NATURAL JOIN eco_projet, projet 
                WHERE eco_projet.id_projet = projet.id_projet  
                GROUP BY eco_projet.id_projet) AS tmpeco 
                /* if I delete that line, everything works as intended with ~2s query*/
                FORCE INDEX FOR JOIN (id_projet) 
                /* ** */
        ON (tmpeco.id_projet = p.id_projet)

WHERE 
    ag.id_division != 2 AND
    dis.id_division != 2 AND
    st.id_division != 2 AND
    pa.id_division != 2 AND 
    cons.id_division != 2 AND 
    p.type_division = 1 AND 
    pa.id_piste_affaire = p.id_piste_affaire AND
    p.id_statut = st.id_statut AND
    p.id_type = ty.id_type AND
    p.id_agence = ag.id_agence AND
    p.id_domain_is = dis.id_domain_is AND
    p.id_sponsor = sp.id_sponsor AND
    p.id_constructeur = cons.id_constructeur AND
    p.id_groupe = gr.id_groupe AND
    p.id_target_price = tarp.id_target_price AND
    p.id_contrat = co.id_contrat AND
    p.id_projet = his.id_projet AND
    p.id_capitalisation = cap.id_capitalisation 
GROUP BY p.id_projet;
4

1 回答 1

2

为什么是子查询?尝试这个:

SELECT * 
FROM table1 t1 
LEFT JOIN table2 t2 
FORCE INDEX FOR JOIN (id_project) 
ON (t2.id_project = t1.id_project);

您甚至可能不需要使用上述查询强制索引。

于 2012-10-08T14:17:44.517 回答