2016-07-04 182 views
0

我有一个sql查询的问题,它给了我想要的输出,而且在我的电脑上工作正常,但由于我必须在学校的个人电脑上运行我的数据库,所以我进入了问题。查询需要34!要执行秒,而在我的电脑,它需要像6SQL查询执行速度慢

这是我的DB:

enter image description here

查询的是:你必须找到了1分代表最大(MAX(中汽))对于已经参加健身会员超过5年的每个用户,“Panca Orizzontale”,“Squat”,“Estensioni Bilanciere”,“Squat”练习的练习。

这是我使用的查询:这是理想的结果

SELECT U.Nome 
    , U.Cognome 
    , MAX(P1.Carico) AS MaxPanca_Orizzontale 
    , MAX(P2.Carico) AS MaxSquat 
    , MAX(P3.Carico) AS MaxEstensioni_Bilanciere 
    , MAX(P4.Carico) AS MaxLento_Avanti 
    FROM utente AS U 
    left 
    join scheda AS S1 
    on U.CF=S1.ID_Utente 
    left 
    join programma AS P1 
    on S1.ID_Scheda = P1.ID_Scheda 
    AND P1.nRipetizioni = 1 
    AND P1.Esercizio = "Panca Orizzontale" 
    left 
    join scheda AS S2 
    on U.CF=S2.ID_Utente 
    left 
    join programma AS P2 
    on S2.ID_Scheda = P2.ID_Scheda 
    AND P2.nRipetizioni = 1 
    AND P2.Esercizio = "Squat" 
    left 
    join scheda AS S3 
    on U.CF = S3.ID_Utente 
    left 
    join programma AS P3 
    on S3.ID_Scheda = P3.ID_Scheda 
    AND P3.nRipetizioni = 1 
    AND P3.Esercizio = "Estensioni Bilanciere" 
    left 
    join scheda AS S4 
    on U.CF = S4.ID_Utente 
    left 
    join programma AS P4 
    on S4.ID_Scheda = P4.ID_Scheda 
    AND P4.nRipetizioni = 1 
    AND P4.Esercizio = "Lento Avanti" 
WHERE U.CF IN(SELECT U.CF 
       FROM utente U 
       WHERE Data_Iscrizione < date_sub(curdate(), interval 5 year) 
      ) 
GROUP 
    BY U.Nome 
    , U.Cognome; 

enter image description here

也许所有这些加入的问题,是有办法使它更快执行?感谢您的时间

+1

可以提供tablestructures和一些示例数据? – Philipp

+0

...和期望的结果。 – Strawberry

+0

添加了所需的结果,现在我尝试添加创建表 – LucaPearl

回答

0

我想说,你最好添加programma.Esercizio组。 喜欢的东西:

SELECT U.Nome, U.Cognome, P.Esercizio, MAX(Carico) AS MaxCarico 
FROM utente AS U left join scheda AS S1 on U.CF=S1.ID_Utente 
left join programma AS P1 on S1.ID_Scheda=P1.ID_Scheda AND P1.nRipetizioni=1 
WHERE U.CF IN(SELECT U.CF FROM utente U WHERE Data_Iscrizione < date_sub(curdate(), interval 5 year)) 
GROUP BY U.Nome, U.Cognome, P.Esercizio; 

输出会有点不同(Extra列和4行,而不是NE),但它的好了很多,以程序为你的数据库服务器。你可以测试一下,看看它是否仍然适合你的需求?

0

我现在已经改变了我的答案,并将它写入新的数据。

首先你必须创建两个新indexe

ALTER TABLE programma 
ADD KEY `idx_nRipetizioni` (`nRipetizioni`,`ID_Scheda`,`Esercizio`); 

ALTER TABLE utente 
ADD KEY `idx_ata_Iscrizione` (`Data_Iscrizione`); 

然后你就可以运行这个。请测试它的结果是一样的。 如果你想拥有的,而不是0变化,0),NULL)在 NULL if语句

,看到了执行时间:-)

SELECT 
    U.Nome, 
    U.Cognome, 
    MAX(IF(P1.Esercizio="Panca Orizzontale",P1.Carico,0)) AS MaxPanca_Orizzontale, 
    MAX(IF(P1.Esercizio="Squat",P1.Carico,0)) AS MaxSquat, 
    MAX(IF(P1.Esercizio="Estensioni Bilanciere",P1.Carico,0)) AS MaxEstensioni_Bilanciere, 
    MAX(IF(P1.Esercizio="Lento Avanti",P1.Carico,0)) AS MaxLento_Avanti 
FROM utente AS U 
LEFT JOIN scheda AS S1 ON U.CF=S1.ID_Utente 
LEFT JOIN programma AS P1 ON S1.ID_Scheda=P1.ID_Scheda 
      AND P1.nRipetizioni=1 
WHERE Data_Iscrizione < date_sub(curdate(), INTERVAL 5 YEAR) 
GROUP BY U.Nome, 
     U.Cognome; 

样品

创建索引

MariaDB [yourschema]> ALTER TABLE programma 
    -> ADD KEY `idx_nRipetizioni` (`nRipetizioni`,`ID_Scheda`,`Esercizio`); 

Query OK, 0 rows affected (0.27 sec) 
Records: 0 Duplicates: 0 Warnings: 0 

创建索引

MariaDB [yourschema]> ALTER TABLE utente 
    -> ADD KEY `idx_ata_Iscrizione` (`Data_Iscrizione`); 
Query OK, 0 rows affected, 1 warning (0.08 sec) 
Records: 0 Duplicates: 0 Warnings: 1 

运行查询

MariaDB [yourschema]> SELECT 
    -> U.Nome, 
    -> U.Cognome, 
    -> MAX(IF(P1.Esercizio="Panca Orizzontale",P1.Carico,0)) AS MaxPanca_Orizzontale, 
    -> MAX(IF(P1.Esercizio="Squat",P1.Carico,0)) AS MaxSquat, 
    -> MAX(IF(P1.Esercizio="Estensioni Bilanciere",P1.Carico,0)) AS MaxEstensioni_Bilanciere, 
    -> MAX(IF(P1.Esercizio="Lento Avanti",P1.Carico,0)) AS MaxLento_Avanti 
    -> FROM utente AS U 
    -> LEFT JOIN scheda AS S1 ON U.CF=S1.ID_Utente 
    -> LEFT JOIN programma AS P1 ON S1.ID_Scheda=P1.ID_Scheda 
    ->   AND P1.nRipetizioni=1 
    -> WHERE Data_Iscrizione < date_sub(curdate(), INTERVAL 5 YEAR) 
    -> GROUP BY U.Nome, 
    ->   U.Cognome; 
+------------+--------------+----------------------+----------+--------------------------+-----------------+ 
| Nome  | Cognome  | MaxPanca_Orizzontale | MaxSquat | MaxEstensioni_Bilanciere | MaxLento_Avanti | 
+------------+--------------+----------------------+----------+--------------------------+-----------------+ 
| Ajeje  | Brazov  |     0.0 | 100.0 |      0.0 |   35.0 | 
| Aldo  | Baglio  |     80.0 | 120.0 |      32.5 |   50.0 | 
| Fernando | Torres  |     0.0 | 150.0 |      0.0 |   35.0 | 
| Francesco | Toldo  |     90.0 |  0.0 |      40.0 |    0.0 | 
| Giovanni | Storti  |     65.0 |  0.0 |      0.0 |   30.0 | 
| Guendalina | Porte  |     0.0 |  50.0 |      20.0 |   25.0 | 
| Harry  | Potter  |    150.0 | 180.0 |      80.0 |   122.5 | 
| John  | Cena   |    135.0 | 240.0 |      60.0 |   75.5 | 
| Kevin  | Velociraptor |     0.0 |  0.0 |      20.0 |   95.0 | 
| Luciano | Spalletti |     60.0 | 280.0 |      95.0 |   100.0 | 
| Marcella | Mandria  |     0.0 |  50.0 |      0.0 |   27.5 | 
| Marcelo | Zalayeta  |    140.0 | 200.0 |      55.0 |   60.0 | 
| Radja  | Nainggolan |     90.0 | 120.0 |      0.0 |   40.0 | 
| Romina  | Power  |     0.0 |  0.0 |     140.0 |   20.0 | 
+------------+--------------+----------------------+----------+--------------------------+-----------------+ 
14 rows in set (0.00 sec) 

MariaDB [yourschema]> 
+0

我试过了,但它显示我一个错误:#1248 - 每个派生表必须有它自己的别名 – LucaPearl

+0

@LucaPearl - 现在在我的答案中更正了错误。请再检查一次。我没有数据 –

+0

nope它不起作用,这是它显示https://i.gyazo.com/43316678aac5c90bd302f2d5f83d0c7c.png – LucaPearl