Hudge联盟呼叫中止

时间:2014-11-24 10:32:29

标签: mysql performance union

我的问题是需要返回相同(补充)字段的大型联合组合,具体取决于不同的焦点。

该网站是一项基于拼车的广告服务:用户可以建议带孩子上学,也可以要求他们的孩子被带走。

涉及7个表:

  • Besoin(确定需要[提案或请求]],
  • 用户,
  • Annonce(包含来自2个不同的特定广告信息 表(km_annoncesparents或km_annoncesaccompas)取决于 Besoin的类型(提案或请求),
  • Perimetre(表示提案的行动领域),
  • 服务(告知提案的时间范围),
  • LieuDepart(起始地点),
  • LieuArrivee(到达地点)

仅使用2个表:Besoin和User。其他人必须加入Besoin类型。整个结果将从与用户的邮政编码匹配的最近位置排序到搜索调用所选择的动态范围。

以下查询效果很好,直到我们在“Besoin”表中达到2000行。但它是一个可怕的怪物,我无法畏惧地盯着它!并且,樱桃蛋糕,服务器现在中止此查询,此消息“<>:1317查询执行被中断”。当然是时间限制或类似的事情......

我不是SQL专家,如果可能,有人会帮助我简化和优化此请求。我没有决定数据库模型。这是给我的规范的一部分,改变这个模型根本不可行......

提前感谢您的建议。请求:

SELECT * FROM ( 

    // Parent request. Starting point is the exact zipcode matching the search
    ( 
    SELECT distinct User.civilite, User.nom, User.prenom, User.adresse, User.ville as Ville, User.email_confirm, User.cni_verifie, User.diplome_verifie, User.permis_verifie, Besoin.id AS B_id, Besoin.do_accomp, Besoin.req_accomp, Besoin.infos, Besoin.reference, Annonce.type_transport, Annonce.created, null as globalPerimetre , Annonce.cp_depart as CP, Annonce.depart_dom, Annonce.arrivee_dom, LieuDepart.ville as ville_depart, LieuDepart.title as title_depart, LieuArrivee.ville as ville_arrivee, LieuArrivee.title as title_arrivee, 0 as garde, 0 as devoirs FROM km_besoins Besoin 
    INNER JOIN km_users User ON User.id = Besoin.user_id 
    INNER JOIN km_annoncesparents Annonce ON Annonce.besoin_id = Besoin.id 
    left JOIN km_lieux LieuDepart on LieuDepart.id = Annonce.lieu_depart_id 
    left join km_lieux LieuArrivee on LieuArrivee.id = Annonce.lieu_arrivee_id 
    where Besoin.etat="publie" and User.valide=1 and Besoin.valide=1 and (Annonce.date is null or Annonce.date >= '2014-11-24') and (Besoin.req_accomp = 1 or Besoin.do_accomp = 1) and Annonce.cp_depart ='75000' 
    GROUP BY B_id 
    )


UNION

    // Parent request. Arrival point is the exact zipcode matching the search
    ( 
    SELECT distinct User.civilite, User.nom, User.prenom, User.adresse, User.ville as Ville, User.email_confirm, User.cni_verifie, User.diplome_verifie, User.permis_verifie, Besoin.id AS B_id, Besoin.do_accomp, Besoin.req_accomp, Besoin.infos, Besoin.reference, Annonce.type_transport, Annonce.created, null as globalPerimetre , Annonce.cp_arrivee as CP, Annonce.depart_dom, Annonce.arrivee_dom, LieuDepart.ville as ville_depart, LieuDepart.title as title_depart, LieuArrivee.ville as ville_arrivee, LieuArrivee.title as title_arrivee, 0 as garde, 0 as devoirs FROM km_besoins Besoin 
    INNER JOIN km_users User ON User.id = Besoin.user_id 
    INNER JOIN km_annoncesparents Annonce ON Annonce.besoin_id = Besoin.id 
    left join km_lieux LieuDepart on LieuDepart.id = Annonce.lieu_depart_id 
    left JOIN km_lieux LieuArrivee on LieuArrivee.id = Annonce.lieu_arrivee_id 
    where Besoin.etat="publie" and User.valide=1 and Besoin.valide=1 and (Annonce.date is null or Annonce.date >= '2014-11-24') and (Besoin.req_accomp = 1 or Besoin.do_accomp = 1) and Annonce.cp_arrivee ='75000' 
    GROUP BY B_id 
    ) 

UNION

    // Service (somebody takes children to school) request with exact city zipcode
    ( 
    SELECT distinct User.civilite, User.nom, User.prenom, User.adresse, User.ville as Ville, User.email_confirm, User.cni_verifie, User.diplome_verifie, User.permis_verifie, Besoin.id AS B_id, Besoin.do_accomp, Besoin.req_accomp, Besoin.infos, Besoin.reference, Annonce.type_trajet, Annonce.created, Annonce.perimetre as globalPerimetre, Perimetre.code_postal as CP, null, null, null, null, null, null, Annonce.garde, Annonce.devoirs FROM km_besoins Besoin INNER JOIN km_users User ON User.id = Besoin.user_id 
    INNER JOIN km_annoncesaccompas Annonce ON Annonce.besoin_id = Besoin.id 
    INNER JOIN km_perimetres Perimetre ON Perimetre.annoncesaccompa_id = Annonce.id AND Perimetre.code_postal ='75000' 
    inner join km_accompahoraires Service on Service.annoncesaccompa_id = Annonce.id 
    where Besoin.etat="publie" and User.valide=1 and Besoin.valide=1 and (Besoin.req_accomp = 0 and Besoin.do_accomp = 1) 
    GROUP BY B_id 
    )

UNION 

    // Parent request. Starting point is in an area close to the searched zipcode
    ( 
    SELECT distinct User.civilite, User.nom, User.prenom, User.adresse, User.ville as Ville, User.email_confirm, User.cni_verifie, User.diplome_verifie, User.permis_verifie, Besoin.id AS B_id, Besoin.do_accomp, Besoin.req_accomp, Besoin.infos, Besoin.reference, Annonce.type_transport, Annonce.created, null as globalPerimetre , Annonce.cp_depart as CP, Annonce.depart_dom, Annonce.arrivee_dom, LieuDepart.ville as ville_depart, LieuDepart.title as title_depart, LieuArrivee.ville as ville_arrivee, LieuArrivee.title as title_arrivee, 0 as garde, 0 as devoirs FROM km_besoins Besoin 
    INNER JOIN km_users User ON User.id = Besoin.user_id 
    INNER JOIN km_annoncesparents Annonce ON Annonce.besoin_id = Besoin.id 
    left JOIN km_lieux LieuDepart on LieuDepart.id = Annonce.lieu_depart_id 
    left join km_lieux LieuArrivee on LieuArrivee.id = Annonce.lieu_arrivee_id 
    where Besoin.etat="publie" and User.valide=1 and Besoin.valide=1 and (Annonce.date is null or Annonce.date >= '2014-11-24') and (Besoin.req_accomp = 1 or Besoin.do_accomp = 1) and Annonce.cp_depart in (75001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,9430075001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,94300)
    GROUP BY B_id 
    ) 

UNION

    // Parent request. Arrival point is in an area close to the searched zipcode
    ( 
    SELECT distinct User.civilite, User.nom, User.prenom, User.adresse, User.ville as Ville, User.email_confirm, User.cni_verifie, User.diplome_verifie, User.permis_verifie, Besoin.id AS B_id, Besoin.do_accomp, Besoin.req_accomp, Besoin.infos, Besoin.reference, Annonce.type_transport, Annonce.created, null as globalPerimetre , Annonce.cp_arrivee as CP, Annonce.depart_dom, Annonce.arrivee_dom, LieuDepart.ville as ville_depart, LieuDepart.title as title_depart, LieuArrivee.ville as ville_arrivee, LieuArrivee.title as title_arrivee, 0 as garde, 0 as devoirs FROM km_besoins Besoin 
    INNER JOIN km_users User ON User.id = Besoin.user_id 
    INNER JOIN km_annoncesparents Annonce ON Annonce.besoin_id = Besoin.id 
    left join km_lieux LieuDepart on LieuDepart.id = Annonce.lieu_depart_id 
    left JOIN km_lieux LieuArrivee on LieuArrivee.id = Annonce.lieu_arrivee_id 
    where Besoin.etat="publie" and User.valide=1 and Besoin.valide=1 and (Annonce.date is null or Annonce.date >= '2014-11-24') and (Besoin.req_accomp = 1 or Besoin.do_accomp = 1) and Annonce.cp_arrivee in (75001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,9430075001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,94300) 
    GROUP BY B_id 
    ) 

UNION

    // Service request with city zipcode in an area close to the searched zipcode
    (
    SELECT distinct User.civilite, User.nom, User.prenom, User.adresse, User.ville as Ville, User.email_confirm, User.cni_verifie, User.diplome_verifie, User.permis_verifie, Besoin.id AS B_id, Besoin.do_accomp, Besoin.req_accomp, Besoin.infos, Besoin.reference, Annonce.type_trajet, Annonce.created, Annonce.perimetre as globalPerimetre, Perimetre.code_postal as CP, null, null, null, null, null, null, Annonce.garde, Annonce.devoirs FROM km_besoins Besoin 
    INNER JOIN km_users User ON User.id = Besoin.user_id INNER JOIN km_annoncesaccompas Annonce ON Annonce.besoin_id = Besoin.id 
    INNER JOIN km_perimetres Perimetre ON Perimetre.annoncesaccompa_id = Annonce.id AND Perimetre.code_postal in (75001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,9430075001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,94300) 
    inner join km_accompahoraires Service on Service.annoncesaccompa_id = Annonce.id where Besoin.etat="publie" and User.valide=1 and Besoin.valide=1 and (Besoin.req_accomp = 0 and Besoin.do_accomp = 1) 
    GROUP BY B_id 

    ) 


) 
as T1 

group by T1.B_id ORDER BY field(T1.CP, 75000,75001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,9430075001,75002,75003,75004,75005,75006,75007,75008,75009,75010,75011,75012,75013,75014,75015,75016,75017,75018,75019,75020,75116,93400,93310,94250,92170,92240,94270,94200,92110,93170,92120,94800,94160,93230,93500,92200,93100,94110,92300,93260,93120,93300,94300), 
T1.B_id desc

0 个答案:

没有答案