简体   繁体   中英

How can I improve performance of a MySQL query without changing the underlying tables?

I have an application which queries data from a 3rd party product. As such, i'm keen not to change the table structure.

Is there a way I can improve efficiency purely on the query side?

My query is:

CallsClosed.Query = @"SELECT COALESCE(ti.FIRST_NAME,'Not Assigned') AS 'Technician', COUNT(*) 'Calls_Closed'
FROM WorkOrder_Threaded wot
INNER JOIN WorkOrder wo ON wot.WORKORDERID=wo.WORKORDERID
LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID
LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
WHERE (wo.COMPLETEDTIME != 0) AND (wo.COMPLETEDTIME != -1) AND (wo.COMPLETEDTIME IS NOT NULL)
AND wo.COMPLETEDTIME >= (UNIX_TIMESTAMP(TIMESTAMP('" + sdChartRange.From + @"')) * 1000)
AND wot.THD_WOID=wot.WORKORDERID
GROUP BY Technician ORDER BY 'Calls_Closed' DESC";

I've run JetProfiler on this, and it looks like the main offender is the size of the wot table. (c. 19k rows)

Any suggestions on where I should start to speed the query up? (Currently takes about 4s to run)

  1. Minimise the number of joins you have to do.
  2. Echoing the comment, add indices.
  3. Look at the EXPLAIN QUERY result for that query.
  4. If solved to satisfaction, post sample data in a fiddle and I'll take a look.
  • Make sure you have indexes on the fields used for selection and joins.
  • Check for foreign keys that are no longer valid ('dangling').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM