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
No, Mysql is creating index on only those fields that you told it to. When you create a column with
UNIQUEat the end it means you are telling mysql to addUNIQUEindex 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.
userpasscolumn has no index. Because you didn’t tell it to.If you need index and you know why you need
UNIQUEindex then just add it. Dont think about the performance now. After deploying if you experience any performance problem then think about optimization.