2

The following query below executes in 17 seconds in a view. There are 450,000 rows. I have an index on the two columns being joined and they are FK. The join columns are BIGINTS. Is there anyway to speed this guy up?

SELECT c.id, sce.user_id
FROM sims_classroom c
JOIN sims_class_enrollment sce ON c.id = sce.classroom_id 

EXPLAIN

'1', 'SIMPLE', 'c', 'index', 'PRIMARY', 'PRIMARY', '8', NULL, '211213', 'Using index'
'1', 'SIMPLE', 'sce', 'ref', 'fk_class_enrollment_classroom_id', 'fk_class_enrollment_classroom_id', '9', 'ngsp.c.id', '1', 'Using where'

ROWS

sims_classroom = 200100
sims_class_enrollment = 476396
3
  • 3
    Use EXPLAIN on your query, and give us the output. Commented Jun 29, 2012 at 23:47
  • How many rows in sims_classroom and sims_class_enrollment too Commented Jun 29, 2012 at 23:49
  • 2
    Could you also try profiling your query? SET PROFILING = 1; SELECT ...; SHOW PROFILE FOR QUERY 1; SET PROFILING = 0; Commented Jun 30, 2012 at 0:20

1 Answer 1

2

It will slow down writes a little but since you're only one column short of having everything you need in your index, I would do a two column index for sce:

classroom_id, user_id

This would result in mysql not even needing to go to the actual table (both would be 'Using index' in the explain).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.