1

How does INDEX work with MYSQL?

Suppose I got 2 tables like this

 //customerTable
 id auto_increment,
 username char(30),
 password char(40),
 phone int(10)

 //profileTable
 id auto_increment,
 username char(30),
 description text

And I created an INDEX on username on both tables, like this

 create index username on `customerTable` ( username, password )
 create index username on `profileTable` ( username )

Then I run these queries:

 select * from `customerTable` where username='abc' limit 1

 select * from `customerTable` where username='abc' and password='xyzzzzz' limit 1

 select customerTable.*, profileTable.* from
 customerTable, profileTable where
 customerTable.username='abc' 
 and customerTable.password='xyzzzzzzz' 
 and customerTable.username = profileTable.username
 limit 1

Which indexes will these 3 queries use? Because name of both indexes is same...

Sumit Kumar
  • 761
  • 1
  • 6
  • 17
  • Use [EXPLAIN EXTENDED SELECT ...](https://dev.mysql.com/doc/refman/5.7/en/explain.html) to see which indexes are being used. – rkosegi Oct 29 '19 at 19:01
  • 1
    You have asked three different questions here. It's really unclear what you're looking for, so [here's a link to the MySQL docs](https://www.tutorialspoint.com/mysql/mysql-indexes.htm) about indexes. – Andrew Oct 29 '19 at 19:04
  • Thankyou for the links. But I was not able to figure out from those docs, which is why I asked. All I want to know is, whether this is correct way of creating indexes, or should I name my indexes differently for each table, even if column names are same. Thanks – Sumit Kumar Oct 29 '19 at 19:32

1 Answers1

1

Index names must be unique within the same table. That is, you can't have two indexes in the same table and name both indexes username.

You can reuse an index name on a different table, like you have shown. Index names don't have to be unique over multiple tables. In this way, they are like column names. You can use the same column name in more than one table.

Some people like to define a naming convention for their index names, but it doesn't really affect anything as far as the database is concerned.

I'm especially puzzled when I see developers who think they have to use "idx_" as a prefix for every index name. It's not necessary, it's just four extra characters you have to type.

The SQL query optimizer knows which index belongs to each table, even if they have the same name. It will not get confused.

You might like my presentation How to Design Indexes, Really, or the video of me presenting it: https://www.youtube.com/watch?v=ELR7-RdU9XU


P.S.: I have a couple of comments that are not directly related to your question, but I have to caution you:

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Many thanks for this clarification. I'm also reading your other points. I don't store passwords in plain text, never. Its always a md5 hashed at least, with a constant string added to the password before running md5 on it. – Sumit Kumar Oct 30 '19 at 04:56
  • That video is wonderful, learned a lot from it! Many thanks for sharing it with me :) – Sumit Kumar Oct 30 '19 at 06:34
  • I'd like to show you a page from my website. The database this page is accessing has 2 tables, one has about 2 M records, and other has 4M records. Second table is read twice, once for "CE", once for "PE". No join queries though... I am using all INDEX rules you explained in your video on this website. https://MunafaSutra.com/nse/optionAnalysis/INFY My confusion was about index names when we use join queries, which you clarified beautifully, and I also got the opportunity to learn so many other things from you :) Many many thanks :) – Sumit Kumar Oct 30 '19 at 06:47
  • Looks like the page is loading very fast now! Congrats! – Bill Karwin Oct 30 '19 at 15:58