0

I have a database with +1 million rows and the structure looks like:

CREATE TABLE IF NOT EXISTS `Performance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `CIDs` varchar(100) DEFAULT NULL,
  `COLOR` varchar(100) DEFAULT NULL,
  `Name` varchar(255) DEFAULT NULL,
  `XT` bigint(16) DEFAULT NULL,
  `MP` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `CIDs` (`CIDs`),
  KEY `COLOR` (`COLOR`),
  KEY `Name` (`Name`),
  KEY `XT` (`XT`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;

INSERT into `Performance` (`id`, `CIDs`, `COLOR`, `Name`, `XT`, `MP`) 
VALUES
(1, '1253374160', 'test test test test test', 'Load1', '89421331221', ''),
(2, '1271672029', NULL, 'Load1', '19421331221', NULL),
(3, '1188959688', NULL, 'Load2', '39421331221', NULL),
(4, '1271672029', NULL, 'Load3', '49421341221', 'Description'),
(5, '1271888888', NULL, 'Load4', '59421331221', 'Description');

The Output should look like:

+----+------------+--------------------------+-------------+-------------+-------+-----------+---------+
| id | CIDs       | COLOR                    | XT          | MP          | Name  | PIDs      | unqName |
+----+------------+--------------------------+-------------+-------------+-------+-----------+---------+
|  1 | 1253374160 | test test test test test | 89421331221 |             | Load1 | 1,2       | Load1   |
|  3 | 1188959688 | NULL                     | 39421331221 | NULL        | Load2 | 3         | Load2   |
|  4 | 1271672029 | NULL                     | 49421341221 | Description | Load3 | 4,5       | Load3   |
+----+------------+--------------------------+-------------+-------------+-------+-----------+---------+

How could I do this as fast as possible? I have tried with some group by, but it takes some Minutes :/

//edit: for the solution with the group by, I needed 4 subquery :/

//edit2: as requested:

    SELECT id, 
       cids, 
       color, 
       xt, 
       mp, 
       name, 
       Concat(pids, ",", Group_concat(DISTINCT id)) AS PIDs, 
       Ifnull(name, id)                             AS unqName 
FROM   (SELECT id, 
               cids, 
               color, 
               xt, 
               mp, 
               name, 
               Concat(pids, ",", Group_concat(DISTINCT id)) AS PIDs, 
               Ifnull(mp, id)                               AS unqMP 
        FROM   (SELECT id, 
                       cids, 
                       color, 
                       xt, 
                       mp, 
                       name, 
                       Concat(pids, ",", Group_concat(DISTINCT id)) AS PIDs, 
                       Ifnull(xt, id)                               AS unqXT 
                FROM   (SELECT id, 
                               cids, 
                               color, 
                               xt, 
                               mp, 
                               name, 
                               Group_concat(DISTINCT id) AS PIDs, 
                               Ifnull(color, id)         AS unqCOLOR 
                        FROM   performance 
                        GROUP  BY unqcolor) m 
                GROUP  BY unqxt) x 
        GROUP  BY unqmp) y 
GROUP  BY unqname
4

0 回答 0