2017-04-07 55 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)。