I've been told that if you know you will be frequently using a field for joins, it may be good to create an index on it.
I generally understand the concept of indexing a table (much like an index in a paper book allows you to look up a particular term without having to search page by page). But I'm less clear about when to use them.
Let's say I have 3 tables: a USERS, COMMENTS, and a VOTES table. And I want to make a Stackoverflow-like commenting thread where the query returns comments as well as the numbers of up/down votes on those comments.
USERS table
user_id user_name
1 tim
2 sue
3 bill
4 karen
5 ed
COMMENTS table
comment_id topic_id comment commenter_id
1 1 good job! 1
2 2 nice work 2
3 1 bad job :) 3
VOTES table
vote_id vote comment_id voter_id
1 -1 1 5
2 1 1 4
3 1 3 1
4 -1 2 5
5 1 2 4
Here's the query and SQLFiddle to return the votes on topic_id=1:
select u.user_id, u.user_name,
c.comment_id, c.topic_id, c.comment,
count(v.vote) as totals, sum(v.vote > 0) as yes, sum(v.vote < 0) as no,
my_votes.vote as did_i_vote
from comments c
join users u on u.user_id = c.commenter_id
left join votes v on v.comment_id = c.comment_id
left join votes my_votes on my_votes.comment_id = c.comment_id
and my_votes.voter_id = 1
where c.topic_id = 1
group by c.comment_id, u.user_name, c.comment_id, c.topic_id, did_i_vote;
Let's assume the number of comments and votes goes in to the millions. To speed up the query, my question is should I put an index on comments.commenter_id
, votes.voter_id
and votes.comment_id
?