IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Requêtes MySQL Discussion :

Optimisation requête avec GROUP BY (avec opérations) et ORDER BY


Sujet :

Requêtes MySQL

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mars 2011
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Optimisation requête avec GROUP BY (avec opérations) et ORDER BY
    Bonjour,

    Je bloque depuis pas mal de temps sur la création d'un index pour une requête.

    La table sur laquelle je veux créer un index contient 15 millions d'enregistrements (50000 enregistrements sont créés chaque jour).

    La structure de ma table :

    stats_property_history_property_id int(11) (PK)
    stats_property_history_date date (PK)
    stats_property_history_visits int(11)
    stats_property_history_phones int(11)
    stats_property_history_emails int(11)
    stats_property_history_offers int(11)
    stats_property_history_leads int(11)
    stats_property_history_impressions int(11)
    stats_property_history_impressions_homepage int(11)
    stats_property_history_impressions_search int(11)
    stats_property_history_alerts int(11)
    stats_property_history_agent_id int(11)
    stats_property_history_property_type_id int(11)
    stats_property_history_property_type_sale_id int(11)
    stats_property_history_property_housing_id int(11)
    stats_property_history_bedroom_id int(11)
    stats_property_history_loc_city_id int(11)
    stats_property_history_loc_community_id int(11)
    stats_property_history_loc_subcommunity_id int(11)
    stats_property_history_loc_tower_id int(11)
    stats_property_history_price double
    stats_property_history_unified_price double
    stats_property_history_sqft double
    stats_property_history_images int(11)
    stats_property_history_property_showstyle_id int(11)
    stats_property_history_property_showstyle_level int(11)
    stats_property_history_property_ranking_total int(11)
    La requête en question :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    SELECT SQL_CALC_FOUND_ROWS 
    property_id, property_reference, property_property_type_id, property_property_type_sale_id,
    property_property_housing_id, property_price_type_id, property_price, property_unified_price,
    property_sqft, property_agent_id, property_bedroom_id, property_loc_community_id,
    property_loc_subcommunity_id, property_loc_tower_id, property_nbimages, property_published, property_date_creation,
    property_deleted, agent_name, property_type_name, property_type_sale_name, property_housing_name,
    bedroom_name, loc_city_name, loc_community_name, loc_subcommunity_name,
    loc_tower_name, price_type_name, property_property_showstyle_id, property_showstyle_name,
    IFNULL(SUM(stats_property_history_visits),0) as visits, IFNULL(SUM(stats_property_history_phones),0) as phones, 
    IFNULL(SUM(stats_property_history_emails),0) as emails, IFNULL(SUM(stats_property_history_offers),0) as offers, 
    IFNULL(SUM(stats_property_history_leads),0) as leads, IFNULL(SUM(stats_property_history_impressions),0) as impressions, 
    IFNULL(SUM(stats_property_history_visits)/SUM(stats_property_history_impressions),0) as ctr1, 
    IFNULL(SUM(stats_property_history_leads)/SUM(stats_property_history_visits),0) as ctr2, 
    IFNULL(AVG(stats_property_history_property_ranking_total),0) as ranking_total, 
    IFNULL(MAX(stats_property_history_property_showstyle_level),1) as property_showstyle_level
    FROM stats_property_history 
    LEFT JOIN property p on stats_property_history_property_id = property_id 
    LEFT JOIN property_type ON property_type_id = property_property_type_id 
    LEFT JOIN property_type_sale ON property_type_sale_id = property_property_type_sale_id 
    LEFT JOIN property_housing ON property_housing_id = property_property_housing_id 
    LEFT JOIN price_type ON price_type_id = property_price_type_id 
    LEFT JOIN agent ON agent_id = property_agent_id 
    LEFT JOIN bedroom ON bedroom_id = property_bedroom_id 
    LEFT JOIN loc_community ON loc_community_id = property_loc_community_id 
    LEFT JOIN loc_city ON loc_city_id = loc_community_loc_city_id 
    LEFT JOIN loc_subcommunity ON loc_subcommunity_id = property_loc_subcommunity_id 
    LEFT JOIN loc_tower ON loc_tower_id = property_loc_tower_id 
    LEFT JOIN property_showstyle ON property_showstyle_id = property_property_showstyle_id 
    WHERE stats_property_history_date BETWEEN '2011-03-22' AND '2011-04-22' 
    AND stats_property_history_property_type_id = '1' 
    AND stats_property_history_property_type_sale_id = '1' 
    AND (stats_property_history_property_housing_id = '1'
    AND (stats_property_history_agent_id = '1'
    AND stats_property_history_loc_city_id = '1' 
    AND (stats_property_history_loc_community_id = '1'
    AND (stats_property_history_loc_subcommunity_id = '1'
    AND (stats_property_history_loc_tower_id = '1'
    GROUP BY stats_property_history_property_id 
    ORDER BY leads DESC 
    LIMIT 0, 100;

    J'ai créé un index sur les colonnes

    stats_property_history_date
    stats_property_history_property_type_id
    stats_property_history_property_type_sale_id
    stats_property_history_property_housing_id
    stats_property_history_agent_id
    stats_property_history_loc_city_id
    stats_property_history_loc_community_id
    stats_property_history_loc_subcommunity_id
    stats_property_history_loc_tower_id
    stats_property_history_property_id
    stats_property_history_visits
    stats_property_history_phones
    stats_property_history_emails
    stats_property_history_offers
    stats_property_history_leads
    stats_property_history_impressions
    Pour infos, je me suis arrêté à stats_property_history_impressions du fait de la limite des 16 champs par index imposé par Mysql.

    Toutes les colonnes xxx_id sont des clés étrangères.

    Avec cet index, ma requête met 30 secondes à s'exécuter, et un explain me donne l'horrible "Using where; Using temporary; Using filesort"

    J'ai cherché pas mal d'infos sur Internet sur l'optimisation des requêtes avec Group By et opérations afin qu'elles utilisent un index, mais j'ai rien trouvé de vraiment concret.

    J'espère que quelqu'un saura m'aider.

  2. #2
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 772
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 21 772
    Points : 52 737
    Points
    52 737
    Billets dans le blog
    5
    Par défaut
    Il est normal que :
    • votre requête ne soit pas optimisable
    • vous n'ayez rien trouvez sur le net

    parce que votre table n'est pas une table, c'est un fichier COBOL !

    Je me marre toujours à votre des horreur pareilles. Connaissez vous la modélisation des données et notamment les formes normales ?

    Si tel était le cas, vous n'auriez pas fait une telle table fourre tout et vos performances serait optimale (quelques ms).

    A lire : http://blog.developpez.com/sqlpro/p1...ances-petites/

    A +
    Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

Discussions similaires

  1. Requête SUM / GROUP BY avec des DATE (et retour étrange)
    Par Kyle_Katarn dans le forum Langage SQL
    Réponses: 1
    Dernier message: 18/03/2012, 01h27
  2. Optimisation requête avec Group BY sur 600 000 lignes
    Par kimaidou dans le forum Requêtes
    Réponses: 3
    Dernier message: 05/03/2011, 13h01
  3. requête avec group by...
    Par JerBi dans le forum Langage SQL
    Réponses: 3
    Dernier message: 29/03/2006, 03h17
  4. Problème requête avec group by et distinct
    Par tomca dans le forum Langage SQL
    Réponses: 2
    Dernier message: 20/07/2005, 16h10
  5. requête avec group by
    Par Staron dans le forum Langage SQL
    Réponses: 4
    Dernier message: 29/11/2004, 16h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo