2017-04-07 54 views
0

我正在開發項目管理工具,同時通過管理端生成報告,數據庫中的數據加載時間非常多> 5分鐘。還有,我知道會幫助我提高性能,但現在我需要有幫助如何提高複雜的MySQL select查詢的性能?

SELECT timesheet_client.organisation as client_name, timesheet_project.title as project_name, 
    timesheet_task.name as task, CONCAT(timesheet_user.first_name, ' ', timesheet_user.last_name) as resource_name, 
    timesheet_user.bill_factor, timesheet_client.client_type, sum(spent) as spent, sum(delivered_hours) as delivered_hours, 
    sum(billable_hours) as billable_hours, comments, color, lock_color, updated_by, updated_by_date, timesheet_user_psdb.grp_id, 
    timesheet_user_psdb.client_id, timesheet_user_psdb.proj_id, timesheet_user_psdb.task_id, timesheet_user_psdb.uid, 
    timesheet_user_grp.grp_name 
FROM timesheet_user_psdb,timesheet_user, timesheet_client, 
    timesheet_project,timesheet_task,timesheet_user_grp 
WHERE timesheet_user.username=timesheet_user_psdb.uid and 
    timesheet_client.client_id=timesheet_user_psdb.client_id and timesheet_project.proj_id=timesheet_user_psdb.proj_id and 
    timesheet_task.task_id = timesheet_user_psdb.task_id and timesheet_user_grp.grp_id=timesheet_user_psdb.grp_id and month =3 
    AND year = 2017 and month!='' and timesheet_user_psdb.client_id=326 
GROUP BY timesheet_user_psdb.task_id,timesheet_user_psdb.uid 
ORDER BY timesheet_client.client_type desc,timesheet_client.organisation,timesheet_user_psdb.proj_id, 
    timesheet_user_psdb.task_id,timesheet_user.uid,timesheet_user_psdb.task_id; 

我已經使用上的所有主鍵索引SELECT查詢幾點。

explain輸出: EXPLAIN Results for above statement

幫助這將是非常可觀的。

+0

是強制性在這種情況下的順序?刪除訂單條款可能會有所幫助。 –

+2

在SELECT之前放置EXPLAIN並向我們顯示輸出。 – Adder

+0

您應該提供表定義,'EXPLAIN'的輸出,您正在使用的引擎,任何引擎特定的配置值以及您接收的記錄數。沒有人可以運行這個查詢並告訴你發生了什麼。我的假設是你正在I/O綁定,因爲你正在運行默認的MySQL設置。 – Mjh

回答

1

試試這個:

SELECT 
    TC.ORGANISATION AS CLIENT_NAME, 
    TP.TITLE AS PROJECT_NAME, 
    TT.NAME AS TASK, 
    CONCAT(TU.FIRST_NAME, ' ', TU.LAST_NAME) AS RESOURCE_NAME, 
    TU.BILL_FACTOR, 
    TC.CLIENT_TYPE, SUM(SPENT) AS SPENT, -- You should specify which table this comes from 
    SUM(DELIVERED_HOURS) AS DELIVERED_HOURS, -- You should specify which table this comes from 
    SUM(BILLABLE_HOURS) AS BILLABLE_HOURS, -- You should specify which table this comes from 
    COMMENTS, -- You should specify which table this comes from 
    COLOR, -- You should specify which table this comes from 
    LOCK_COLOR, -- You should specify which table this comes from 
    UPDATED_BY, -- You should specify which table this comes from 
    UPDATED_BY_DATE, -- You should specify which table this comes from 
    TUP.GRP_ID, 
    TUP.CLIENT_ID, 
    TUP.PROJ_ID, 
    TUP.TASK_ID, 
    TUP.UID, 
    TUG.GRP_NAME 
FROM  
    TIMESHEET_USER AS TU 
     LEFT OUTER JOIN 
    TIMESHEET_USER_PSDB AS TUP 
     ON TU.USERNAME = TUP.UID 
     LEFT OUTER JOIN 
    TIMESHEET_USER_GRP AS TUG 
     ON TUP.GRP_ID = TUG.GRP_ID 
     LEFT OUTER JOIN  
    TIMESHEET_CLIENT AS TC 
     ON TUP.CLIENT_ID = TC.CLIENT_ID 
     LEFT OUTER JOIN 
    TIMESHEET_PROJECT AS TP 
     ON TUP.PROJ_ID = TP.PROJ_ID 
     LEFT OUTER JOIN 
    TIMESHEET_TASK TT 
     ON TUP.TASK_ID = TT.TASK_ID 
WHERE 
    MONTH = 3 AND -- You should specify which table this comes from 
    YEAR = 2017 AND -- You should specify which table this comes from 
    MONTH != '' AND -- You should specify which table this comes from 
    TUP.CLIENT_ID = 326 
GROUP BY 
    TUP.TASK_ID, 
    TUP.UID 
ORDER BY 
    TC.CLIENT_TYPE DESC, 
    TC.ORGANISATION, 
    TUP.PROJ_ID, 
    TUP.TASK_ID, 
    TU.UID, 
    TUP.TASK_ID; 
+0

是的,這工作非常好。我已經執行了這個查詢,大約需要0.0309秒來顯示結果。 –

1

做一個解釋應該對此有所瞭解。

通過在FROM子句中顯式地執行表連接而不是在WHERE內(https://dev.mysql.com/doc/refman/5.7/en/join.html),您可能會看到性能提升。通過顯式連接(例如LEFT OUTER等),您不僅可以提高查詢的可讀性,還可以在需要時使用較便宜的連接。這也影響查詢的執行方式,因爲每個子句都按特定順序執行(MySQL query/clause execution order)。