Advertisement

01.23.2008 at 10:57AM PST, ID: 23105429
[x]
Attachment Details

MySQL Query optimization with a join and group by

Asked by MacAnthony in MySQL

Tags: SQL

I have a relatively simple query that uses a join and a group by but the query takes a while to run. The first table has 45k rows and the joined table has 5k rows and without query caching, it takes about 2 seconds to run on our development server. Without the group by, it takes about 11ms. There are individual indexes applied to all the references columns but no multicolumn indexes (I'm not real good at figuring out what the indexes should be).

I've attached the query and the explain statement.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
QUERY:
SELECT SQL_NO_CACHE `t1`.`tasklistCompletionId` AS `tasklistCompletionId` , max( `t2`.`lastModified` ) AS `revUsedDate` , `t2`.`tasklistItemId` AS `tasklistItemId`
FROM (
`tasklistCompletion` `t1`
JOIN `tasklistItem` `t2` ON (
`t1`.`tasklistItemId` = `t2`.`tasklistItemId`
AND `t2`.`lastModified` <= `t1`.`completionDate`
)
)
GROUP BY `t1`.`tasklistCompletionId`
 
 
 
EXPLAIN:
id 	select_type 	table 	type 	possible_keys 	key 	key_len 	ref 	rows 	Extra
1 	SIMPLE 	t2 	ALL 	lastModified,tasklistItemId 	NULL 	NULL 	NULL 	4769 	Using temporary; Using filesort
1 	SIMPLE 	t1 	ref 	checklistItemId,completionDate 	checklistItemId 	4 	projects.t2.tasklistItemId 	3 	Using where
 
Loading Advertisement...
 
[+][-]01.23.2008 at 11:33PM PST, ID: 20731396

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.24.2008 at 05:36AM PST, ID: 20732794

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MySQL
Tags: SQL
Sign Up Now!
Solution Provided By: AdrianSRU
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.24.2008 at 05:44AM PST, ID: 20732868

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 06:48AM PST, ID: 20733412

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 07:13AM PST, ID: 20733689

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 07:31AM PST, ID: 20733871

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.24.2008 at 07:32AM PST, ID: 20733883

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628