2016-07-04 176 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