0

I have following query where I am fetching Question count for exams created by CreatorID 1001

SELECT count(Questions.question_id) QuestionCount 
    FROM Exams 
        LEFT JOIN Questions  
            ON Exams.exam_id = Questions.exam_id_fk 
    WHERE Exams.CreatorId='1001'

I know this is really stupid question but at some point its possible that there can be 10,000 users executing same query I just want to make sure about performace. So is there any othso er better way than this to do it ?

Thanks.

3 Answers 3

3

Provided you do not need the Exams where no questions have been defined, the following should be faster:

SELECT
  count(Questions.question_id) QuestionCount 
FROM Exams 
INNER JOIN Questions  
        ON Exams.exam_id = Questions.exam_id_fk 
WHERE Exams.CreatorId='1001'

Also make sure you have an index on the appropriate fields. Try using EXPLAIN on the query.

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

Comments

1

Any reason why your LEFT JOIN couldn't be an INNER JOIN instead?

Also, make sure you have indexes on:

  • Exams.CreatorId
  • Exams.exam_id
  • Questions.exam_id_fk

2 Comments

What is the advantage between using a LEFT and INNER JOIN? I admit to using INNER all the time but can't justify it.
INNER JOIN is faster, but omits the results where no record to join could be found.
0

It looks good to me. Just make sure that your join columns match and are indexes. I wouldn't preoptimize it.

If you do find that this query has a performance issue, by the context of the table and column names, you could store and update the number of questions on the exam. Assuming that adding and removing questions isn't occurring a lot.

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.