0

I read somewhere that one should not create many indexes on the columns of a table for it reduces the performance of operations.

But when I create a table with UNIQUE NOT NULL fields, the MySQL is creating indexes on all the fields by itself. Doesn't that reduce the performance?? If yes, what flags I need to change the default behaviour? If not, then where I am wrong?

My Table:

CREATE TABLE Users(
Id_usr INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(Id_usr),
Email_id varchar(45) UNIQUE NOT NULL,
username varchar(30) UNIQUE NOT NULL,
userpass varchar(30) NOT NULL
);

When I see the table on phpmyadmin:

Indexes: Documentation
Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
Edit Edit   Drop Drop   PRIMARY BTREE   Yes No  Id_usr  0   A       
Edit Edit   Drop Drop   Email_id    BTREE   Yes No  Email_id    0   A       
Edit Edit   Drop Drop   username    BTREE   Yes No  username    0   A       
2
  • 1
    Indexes are a trade-off. They slow table write operations (i.e. inserts and updates) whilst speeding up read operations (i.e. selects, duplicate checking etc). If you want MySQL to enforce a uniqueness constraint on a column, then having an index on that column is the right approach. Don't prematurely micro-optimise the cost of additional indexes unless/until you're aware that it's becoming an issue.
    – eggyal
    Nov 11, 2012 at 20:34
  • 1
    The general rule of thumb for indexes is to slap one onto any field(s) which are used in a where, a join, or an order by. Indexes are there to help speed up searches, at the cost of slowing down insert/updates. If you never use a field as anything but a storage location, then don't index it.
    – Marc B
    Nov 11, 2012 at 20:44

2 Answers 2

1

But when I create a table with UNIQUE NOT NULL fields, the MySQL is creating indexes on all the fields by itself.

No, Mysql is creating index on only those fields that you told it to. When you create a column with UNIQUE at the end it means you are telling mysql to add UNIQUE index on that column.

So for two UNIQUE columns there are two indexes. And for the primary key there is one index. In total 3 indexes. userpass column has no index. Because you didn't tell it to.

If you need index and you know why you need UNIQUE index then just add it. Dont think about the performance now. After deploying if you experience any performance problem then think about optimization.

1

The reduced performance is due to the need to update indices while writing. Indices are created to speed up selecting, on the other hand.

MySQL has no way to enforce uniqueness without creating an index or scanning your whole table on each insert or update which would be much worse performance impact. Luckily, you can't change that behaviour.

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.