How to create parent and children by giving two or more columns as a grouping in any SELECT efficiently?

Asked

Viewed 11 times

1

Let’s assume I have similar data below, but the complete table has: auto-increment, measurement values, dates, foreign keys, descriptive texts. In the example I created, it has only auto-increment, a value, a date and two foreign keys representing an equipment and a customer. The same equipment may have more than one customer and one customer may have more than one equipment.

DROP TABLE IF EXISTS `teste_stackoverflow`;
CREATE TABLE `teste_stackoverflow`  (
  `auto` INT NOT NULL AUTO_INCREMENT,
  `descricao` VARCHAR(255),
  `chave_estrangeira_1` INT,
  `chave_estrangeira_2` INT,
  `valor` DOUBLE,
  `data` DATETIME,
  PRIMARY KEY (`auto`) USING BTREE
);
INSERT INTO `teste_stackoverflow` VALUES (1, 'Material 1', 1, 1, 1.300, '2019-04-02 12:04:40');
INSERT INTO `teste_stackoverflow` VALUES (2, 'Material 2', 1, 2, 1.500, '2019-04-02 12:05:59');
INSERT INTO `teste_stackoverflow` VALUES (3, 'Material 3', 2, 1, 1.400, '2020-04-02 12:06:34');
INSERT INTO `teste_stackoverflow` VALUES (4, 'Material 4', 1, 1, 1.700, '2020-04-02 12:06:52');
INSERT INTO `teste_stackoverflow` VALUES (5, 'Material 5', 1, 2, 1.800, '2020-04-02 12:07:22');

Having this scenario, I want to create a 'history' based on these two foreign keys. For each line, point to the id or auto of previous as father. I got something with the consultation

    WITH CTE AS (
    SELECT      
        auto, auto as historico_auto, descricao, chave_estrangeira_1, chave_estrangeira_2, valor, data
    FROM teste_stackoverflow
    ORDER BY data DESC
)
    SELECT
        T1.auto, CTE.historico_auto, T1.descricao,T1.chave_estrangeira_1,T1.chave_estrangeira_2, T1.valor, T1.data
    FROM teste_stackoverflow AS T1
        INNER JOIN CTE ON CTE.chave_estrangeira_1 = T1.chave_estrangeira_1 AND CTE.chave_estrangeira_2 = T1.chave_estrangeira_2
    WHERE T1.Auto <= CTE.historico_auto
    ORDER BY historico_auto, auto;

Which I brought, the way I wanted:

+------+----------------+------------+---------------------+---------------------+-------+---------------------+
| auto | historico_auto | descricao  | chave_estrangeira_1 | chave_estrangeira_2 | valor | data                |
+------+----------------+------------+---------------------+---------------------+-------+---------------------+
|    1 |              1 | Material 1 |                   1 |                   1 | 1.300 | 2019-04-02 12:04:40 |
|    2 |              2 | Material 2 |                   1 |                   2 | 1.500 | 2019-04-02 12:05:59 |
|    3 |              3 | Material 3 |                   2 |                   1 | 1.400 | 2020-04-02 12:06:34 |
|    1 |              4 | Material 1 |                   1 |                   1 | 1.300 | 2019-04-02 12:04:40 |
|    4 |              4 | Material 4 |                   1 |                   1 | 1.700 | 2020-04-02 12:06:52 |
|    2 |              5 | Material 2 |                   1 |                   2 | 1.500 | 2019-04-02 12:05:59 |
|    5 |              5 | Material 5 |                   1 |                   2 | 1.800 | 2020-04-02 12:07:22 |
+------+----------------+------------+---------------------+---------------------+-------+---------------------+

With the real table, with the various 'values', 'dates', texts and other information, in some 86 thousand records, this query does not conclude. It is several minutes running without bringing any results, even when I use only the columns necessary for logic, as in the example created and cited. Is there any more efficient way to address this problem?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.