Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

MySQL Index

Hi Experts,

I have question about index in database. I've read some website that I can have composite indices which MySQL will know when to use it. I've also noticed that some page said if I have a composite index, if my query doesn't use the first index column from composite index, it will not be used.

My question will be:

1. If I declared index like this: INDEX(col1, col2, col3), does it mean that I don't need to create additional index for col2 and col3 (INDEX(col2), INDEX(col3)) separately? From what I've read that if my query doesn't use the col1 but instead used col2, INDEX(col1, col2, col3) will not be used. If I don't have INDEX(col2), then I will have a slow query because there is no index for col2. Is this true?

2. Lately I tried to run mysqlindexcheck as one of the utility command from MySQL workbench. After I ran this command, I found some possible redundant indices. For example I declared INDEX(col1, col2) and I create INDEX(col1), INDEX(col2) again, it showed that I have redundant indices. For this case should I simply remove the INDEX(col1), not INDEX(col2)?
0
kisegi
Asked:
kisegi
1 Solution
 
momi_sabagCommented:
1. Yes. Think of an index like the phone book. The phone book is organized by last name, first name. So if you search for someone by the full name, you will find it very quickly but if you search for someone only by the first name you will have to read the entire phone book (if for example you know the first name and phone number and you are trying to find the last name).
2. yes. Since COL1 is the first column in the composite index, you don't need to have another index that only has COL1.
Same goes for more than one column. If you have an index on C1,C2,C3, an index on C1,C2 will be redundant (but an index on C1,C3 will not)
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now