Voilà confronté à un problème de lenteur dans une application PHP-Mysql, je cherche à optimiser en premier lieu ma requête SQL. Le problème c'est que je n'arrive pas à pondre la requête sql adéquate.
Contexte simplifié :
J’ai 2 tables.
Une table demande et une table Contact.
Un contact peut effectuer 0 ou plusieurs demandes.
Un contact se caractérise par de nombreux attributs dont une adresse email.
Mes deux tables sont reliées via l’id_contact.
Exemple :
id_contact...nom..............email1
-------------------------------------------------------
1..............TOTO............toto_AT_free.fr
2..............LEON............leon_AT_wanadoo.fr
3..............TOTO...............toto_AT_free.fr
4..............PAUL1.............paul_AT_neuf.fr
5..............NOEMIE..........nono_AT_club.fr
6..............PAUL2.............paul_AT_neuf.fr
id_demande....date_demande......contact_id
---------------------------------------------------------
1...................25/01/2006.................1
2...................12/04/2006.................2
3...................13/05/2006.................3
4...................13/06/2006.................4
5...................03/09/2006.................4
6...................04/09/2006.................4
7...................02/01/2007.................6
Exemple concret :
CODE
SELECT c.id_contact, d.id_demande, d.date_demande, c.nom, c.email1
FROM demande d LEFT JOIN contact c ON d.contact_id = c.id_contact
FROM demande d LEFT JOIN contact c ON d.contact_id = c.id_contact
id_contact....id_demande..date_demande...nom.....email1
-----------------------------------------------------------------------------------------------
1.................1......................25/01/2006.....TOTO....toto_AT_free.fr
2.................2......................12/04/2006.....LEON....leon_AT_wanadoo.fr
3.................3......................13/05/2006.....TOTO....toto_AT_free.fr
4.................4......................13/06/2006.....PAUL1...paul_AT_neuf.fr
4.................5......................03/09/2006.....PAUL1...paul_AT_neuf.fr
4.................6.......................04/09/2006.....PAUL1...paul_AT_neuf.fr
4.................7......................02/01/2007.....PAUL2...paul_AT_neuf.fr
Voila ce que je souhaiterais obtenir :
id_contact..id_demande...date_demande.....nom.....email1.................nb
---------------------------------------------------------------------------------
1..................1.................25/01/2006.......TOTO.....toto_AT_free.fr..........2
2..................2.................12/04/2006.......LEON.....leon_AT_wanadoo.fr...1
3..................3.................13/05/2006.......TOTO.....toto_AT_free.fr..........2
4..................4.................13/06/2006.......PAUL1....paul_AT_neuf.fr.........4
4..................5.................03/09/2006.......PAUL1....paul_AT_neuf.fr.........4
4..................6.................04/09/2006.......PAUL1....paul_AT_neuf.fr.........4
4..................7.................02/01/2007.......PAUL2....paul_AT_neuf.fr.........4
Code pour créer les 2 tables avec les datas
CODE
--
-- Structure de la table `contact`
--
CREATE TABLE `contact` (
`id_contact` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(100) collate latin1_general_ci NOT NULL DEFAULT '',
`email1` varchar(250) collate latin1_general_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id_contact`),
KEY `email1` (`email1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=27376;
--
-- Contenu de la table `contact`
--
INSERT INTO `contact` (`id_contact`, `nom`, `email1`) VALUES
(1, 'TOTO', 'toto_AT_free.fr'),
(2, 'LEON', 'leon_AT_wanadoo.fr'),
(3, 'TOTO', 'toto_AT_free.fr'),
(4, 'PAUL1', 'paul_AT_neuf.fr'),
(5, 'NOEMIE', 'nono_AT_club.fr'),
(6, 'PAUL2', 'paul_AT_neuf.fr');
-- --------------------------------------------------------
--
-- Structure de la table `demande`
--
CREATE TABLE `demande` (
`id_demande` int(11) NOT NULL AUTO_INCREMENT,
`date_demande` date NOT NULL DEFAULT '0000-00-00',
`contact_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_demande`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci PACK_KEYS=0 AUTO_INCREMENT=23411;
--
-- Contenu de la table `demande`
--
INSERT INTO `demande` (`id_demande`, `date_demande`, `contact_id`) VALUES
(1, '2006-01-25', 1),
(2, '2006-04-12', 2),
(3, '2006-05-13', 3),
(4, '2006-06-13', 4),
(5, '2006-09-03', 4),
(6, '2006-09-04', 4),
(7, '2007-01-02', 6);
-- Structure de la table `contact`
--
CREATE TABLE `contact` (
`id_contact` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(100) collate latin1_general_ci NOT NULL DEFAULT '',
`email1` varchar(250) collate latin1_general_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id_contact`),
KEY `email1` (`email1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=27376;
--
-- Contenu de la table `contact`
--
INSERT INTO `contact` (`id_contact`, `nom`, `email1`) VALUES
(1, 'TOTO', 'toto_AT_free.fr'),
(2, 'LEON', 'leon_AT_wanadoo.fr'),
(3, 'TOTO', 'toto_AT_free.fr'),
(4, 'PAUL1', 'paul_AT_neuf.fr'),
(5, 'NOEMIE', 'nono_AT_club.fr'),
(6, 'PAUL2', 'paul_AT_neuf.fr');
-- --------------------------------------------------------
--
-- Structure de la table `demande`
--
CREATE TABLE `demande` (
`id_demande` int(11) NOT NULL AUTO_INCREMENT,
`date_demande` date NOT NULL DEFAULT '0000-00-00',
`contact_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_demande`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci PACK_KEYS=0 AUTO_INCREMENT=23411;
--
-- Contenu de la table `demande`
--
INSERT INTO `demande` (`id_demande`, `date_demande`, `contact_id`) VALUES
(1, '2006-01-25', 1),
(2, '2006-04-12', 2),
(3, '2006-05-13', 3),
(4, '2006-06-13', 4),
(5, '2006-09-03', 4),
(6, '2006-09-04', 4),
(7, '2007-01-02', 6);
Espérant avoir exposé ma problématique le plus clairement possible, je vous remercie par avance des conseils que vous pourrez m’apporter.
