1

I'm trying to understand how MySQL uses indexes for the join criteria as well as the where criteria. If I do a join such as the following:

SELECT * from Table1
 inner join table2 on Table1.colA = Table2.colB
 where Table2.colC > val1
   and Table2.colC < val2;

How do I index Table1 and Table2 so that it is executing this query efficiently?

Do I need to do multiple single indexes on Table1.colA and Table2.colB and Table2.colC? Or is there another way to do this?

4
  • You should index JOIN columns and columns used in the WHERE,so yes.
    – Mihai
    Feb 10, 2015 at 22:01
  • Try creating the compound index (colB,colC) on Table2. Then test performance and run EXPLAIN. Then do the same with the index (colC, ColB). Then, show us the results, please.
    – O. Jones
    Feb 10, 2015 at 22:36
  • Unfortunately, I simplified an example on production. The table I'm trying to make more efficient is a 73M row table that is 27GB big, and I don't have permissions to add indexes on the fly. I would need to request our DBA to do this, and I need to prove beforehand that the indexes would help. So experimenting wouldn't be an option unfortunately.
    – Steve
    Feb 10, 2015 at 23:01
  • Here's the deal. This kind of query is often helped by compound covering indexes (look that up). It's important to know the full query and the table layouts to suggest a proper index. But one thing is for sure. You're doing a range scan on Table2.ColC. Can you give more detail?
    – O. Jones
    Feb 10, 2015 at 23:28

1 Answer 1

0

How do I index Table1 and Table2 so that it is executing this query efficiently?

Set your first column in each table as PK/AI (Primary Key/Auto-increment).

Then, when you need to join them together you can name the second column in tableB.colB linkToA or simple idA. That sounds a bit confusing, so here's a practical example:

You have a table of Students and Teachers.

students table

┌──┬────────────┬──────────┐
│id│    name    │teacher_id│
├──┼────────────┼──────────┼
│1 │Billy       │3         │
├──┼────────────┼──────────┼
│2 │Jeffrey     │2         │
├──┼────────────┼──────────┼
│3 │Sally       │3         │
├──┼────────────┼──────────┼
│4 │Frankie     │1         │
└──┴────────────┴──────────┘

teachers table

┌──┬────────────┬───┐
│id│    name    │age│
├──┼────────────┼───┤
│1 │Jan         │34 │
├──┼────────────┼───┤
│2 │Alexander   │26 │
├──┼────────────┼───┤
│3 │Steve       │23 │
├──┼────────────┼───┤
│4 │Joan        │56 │
└──┴────────────┴───┘

Now suppose you need to find students whose teacher is in between 25 and 35 years old.

SELECT s.name AS student_name, t.name AS teacher_name
FROM students s
INNER JOIN teachers t ON s.teacher_id = t.id
WHERE t.age > 25 AND t.age < 35;

Result set

┌────────────┬────────────┐
│student_name│teacher_name│
├────────────┼────────────┤
│Jeffrey     │Alexander   │
├────────────┼────────────┤
│Frankie     │Jan         │
└────────────┴────────────┘
4
  • The query in the question calls for a range scan on colC. Not sure this answer takes that into account.
    – O. Jones
    Feb 10, 2015 at 22:38
  • Thanks @rick6, but I was asking about indexing on tables where there is a join as well as where clause. What would the indexes that I would use to help with your example, especially on the t.age column?
    – Steve
    Feb 10, 2015 at 23:01
  • @Steve No problem; I thought I addressed that by asking you to set the id on both tables as PK/AI? Primary Key is another word for "index".
    – rick6
    Feb 10, 2015 at 23:07
  • Range scans benefit from indexes. You didn't suggest one on the column being range scanned.
    – O. Jones
    Feb 10, 2015 at 23:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.