Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimizing query for 3 JOINed tables

Status
Not open for further replies.

wanttoscript

IS-IT--Management
Oct 25, 2002
9
US
I am new to MySQL, and am in the process of migrating an ACCESS database to MySQL/PHP. I am having some big performance issues!

I have many queries that rely on three tables:
i. employee_info (describes a hierarchy of users)
ii. call_info (data describing phone calls)
iii. answers (answers to questions on each call)

My queries in ACCESS look something like:

SELECT Count(answers.answer) AS num_answers
FROM (call_info LEFT JOIN answers ON call_info.call_id = answers.call_id) INNER JOIN employee_info ON call_info.employee_number = employee_info.employee_number
WHERE (((employee_info.level3)=2) AND ((call_info.call_date)>=#3/1/2003# And ((call_info.call_date)<=#4/1/2003#));

This will typically run on 200,000 calls in less than 2 seconds!

When I convert directly to MySQL I try
SELECT Count(answers.answer) AS num_answers
FROM (call_info LEFT JOIN answers ON call_info.call_id = answers.call_id) INNER JOIN employee_info ON call_info.employee_number = employee_info.employee_number
WHERE (((employee_info.level3)=2) AND ((call_info.call_date)>=&quot;2003-03-01&quot; And ((call_info.call_date)<=&quot;2003-04-01&quot;));

THIS TAKES OVER 2 minutes to run .... I am sure this has to do with how I JOIN the tables, but I don't know how to speed this up!

Any help/ guidance would be most appreciated.
 
Code:
SELECT Count(a.answer) AS num_answers
  FROM call_info ci INNER JOIN employee_info ei
    ON ci.employee_number = ei.employee_number
 INNER join answers a 
    ON ci.call_id = a.call_id
WHERE ei.level3 = 2 
  AND ci.call_date >= '2003-03-01' 
  And ci.call_date <= '2003-04-01'

How are the tables indexed?
Count(columnName) will discard any null values so there is no need for a left join.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top