2017-09-15 58 views
0

SQLFiddle here:http://sqlfiddle.com/#!9/b88d1a/1/0 ---注意由於大約8000個字符的限制,並且沒有查詢無法運行的最小數據量,我無法在SQLFiddle中加載足夠的插入。因此,SQL插入位於此pastebin文件中:https://pastebin.com/MfhJ0Svc - 我無法將320插入行粘貼到Fiddle中,或者抱歉。如何簡化多選查詢以加快執行速度?

我有一個表格,然後是一個名爲Review的視圖,它包含一個只有唯一用戶名的列。該表稱爲RebasedQuestions,目前包含大約40000條記錄。

該表用於計算人們對人們進行的一系列評論。

查詢需要產生一個最終表格,它以百分比的形式給出了主管,自己,同行和下屬的值,然後是6以外的值(評論問題選項的範圍從1到6)。而每評審類型的權重分別是:

Supervisor: 30% 
Own: 0% 
Peer: 40% 
Subordinate: 30% 

enter image description here

這裏是DDL的觀點:

CREATE VIEW Reviewed AS 
    SELECT DISTINCT `t1`.`Reviewed` AS `Reviewed` 
    FROM `edsdb`.`RebasedQuestions` `t1` 
    ORDER BY `t1`.`Reviewed`; 

而且DDL主表:

create table RebasedQuestions 
(
    Reviewed varchar(50) null, 
    ReviewType varchar(20) null, 
    BU int null, 
    RebasedValue double null 
) 
; 

這是我的查詢,但我認爲它還遠沒有優化,所以請賜教如何加快這一點起來,尤其是在主表與每一個新的審查,提交時間爲20線增長:

select DISTINCT t1.Reviewed, t2.BU, 
    (SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Supervisor') AS Supervisor, 
    (((SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Supervisor')/100) * 6) as Sup6, 
    (SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Own') AS Own, 
    (((SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Own')/100) * 6) as Own6, 
    (SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Peer') AS Peer, 
    (((SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Peer')/100) * 6) as Peer6, 
    (SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Subordinate') AS Subordinate, 
    (((SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Subordinate')/100) * 6) as Sub6, 
    (ifnull((((SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Supervisor')/100) * 6),6) * 0.3 + 
    ifnull((((SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Peer')/100) * 6),6) * 0.4 + 
    ifnull((((SELECT ((sum(t2.RebasedValue)/(count(t2.RebasedValue)/20))) from RebasedQuestions t2 WHERE t2.Reviewed = t1.Reviewed and t2.ReviewType = 'Subordinate')/100) * 6),6) * 0.3) as Totaled 
from Reviewed t1 JOIN RebasedQuestions t2 on t1.Reviewed = t2.Reviewed 

我甚至不能粘貼在這裏插入體內。

他們可以在這個引擎收錄鏈接上找到:https://pastebin.com/MfhJ0Svc

+1

您還可以提供一些樣本和所需的數據 –

+1

請提供您期望的樣本輸入和輸出。即使沒有任何調整,如果沒有比您寫的更有效的查詢,我會感到震驚。 –

+1

作爲@MKhalidJunaid說,請它會很好,如果使用這個工具 sqlfiddle.com – ARr0w

回答

1

下面是你應該能夠適應你的需要的建議。

SELECT 
innerQuery.Reviewed, 
innerQuery.BU, 
(CASE WHEN (innerQuery.supcount=0) then null else 
innerQuery.suptot/innerQuery.supcount end) as sup, 
(CASE WHEN (innerQuery.supcount=0) then null else 
(6*innerQuery.suptot)/(100*innerQuery.supcount) end) as sup6 
... 
(CASE WHEN (innerQuery.supcount=0) then 1.8 else sup6*0.3 end)+ ... as totaled 
from 
(SELECT 
Reviewed, 
BU, 
(SUM(ReviewType='Supervisor') then RebasedValue else 0 end) as 
suptot 
(COUNT(ReviewType='Supervisor') then RebasedValue else 0 end) as 
supcount, 
... 
FROM RebasedQuestions GROUP BY Reviewed, BU) innerQuery 
+0

在手機atm上,謝謝你的回答。將盡快測試並回復;) – georgelappies