MySql Using filesort when i using a group by

48 views Asked by At

I have a little problem with optimizing a query, I have 2 tables, one which records the participation (participation) in a quiz, and the other which records the answer to each question (participation_rep), participation is linked to the campaign table.

SELECT count(DISTINCT p.id) as number_of_participation
       FROM participation_rep prep          
           INNER JOIN participation p           
                ON p.id = prep.id_participation
                AND p.trash <> 1
  WHERE prep.id_question IN (780,787,794,801,809)           
  AND prep.trash <> 1   
  GROUP BY pp.id_campaign

Explain of the query

And the problem is that this request is very heavy to execute when there is a lot of data which is concerned by the request and I do not know how to optimize it.

This query take 30-50ms to execute.

Structure of table participation :

CREATE TABLE IF NOT EXISTS `participation` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_campagne` int(11) NOT NULL,
  `id_identifiant` int(11) DEFAULT NULL,
  `firstname` varchar(255) DEFAULT NULL,
  `surname` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `date_p` date NOT NULL,
  `hour_p` time NOT NULL,
  `comment` text,
  `trash` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Structure of table participation_rep :

CREATE TABLE IF NOT EXISTS `participation_rep` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_participation` int(11) NOT NULL,
  `id_question` int(11) NOT NULL,
  `id_rep` int(11) NOT NULL,
  `trash` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_participation` (`id_participation`,`id_question`,`id_reponse`),
  KEY `id_question` (`id_question`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
0

There are 0 answers