So without index MySQL start searching from first row and then to the entire table to find relevant rows. So bigger the table higher the cost. If indexes were introduced for the seeking columns query can locate the relevant rows without going through the entire table. If table as 1000 records so this is at least 100 times fast (as per the MySQL Doc). If you need to access much of the data then reading the entire table sequentially is won’t be big problem. Because it minimize the data seeks. If “WHERE” clause has more than on indexed columns it uses the index which gives the smallest number of rows.
If you use JOIN in your query it will use index on different tables. Here having same type of index columns coming in handy. If comparison cannot be done without conversion on dissimilar data types MySQL will prevent use of index. So having similar types on different columns on different table will definitely increase the performance when comes to JOIN. VARCHAR and CHAR is taken as same type if the size also same.
All MySQL data types can be use as indexes. But maximum number of indexes per table and maximum index length depend on the storage engine. Most storage engines have higher values but at least 16 indexes per table and a total index length of at least 256 bytes supported.
CREATE TABLE emp (emp_id INT, INDEX(emp_id));
You can create an index using first N characters also.
CREATE TABLE employee (e_b_col BLOB, INDEX(e_b_col (10)));
You can use up to 1000 bytes of prefixes (prefix is calculated by bytes). FULL-TEXT indexing only possible with MyISAM which will use for full text searches. And data types CHAR, VARCHAR and TEXT can be used.
Spatial data type indexing can be done except NDBCLUSTER and ARCHIVE, which use RTREE. MEMORY storage engine uses HASH as default but BTREE also supports.
If the query uses only columns that are numeric and left most prefix of key, then result can be obtain from index tree with higher speed.
Index Merge optimization
MySQL do several range scans and merge their results into one. It’s only applied to single table not multiple tables across.
CREATE TABLE `t2` (`id` INT NOT NULL, `age` INT NOT NULL, `marks` INT NOT NULL,
KEY `age` (`age`),
KEY `marks` (`marks`));
mysql> explain select * from t2 where age=1 or marks=1 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t2
type: index_merge
possible_keys: age,marks
key: age,marks
key_len: 4,4
ref: NULL
rows: 2
Extra: Using union(age,marks); Using where
1 row in set (0.00 sec)
In ‘type’ column you can see it is using index_merge.
mysql> explain select * from t2 ignore index (age,marks) where age=1 or marks=1
\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t2
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1347
Extra: Using where
Use “EXPLAIN” is a good way to proceed with the query optimization
Indexes are something extra that you can enable on your MySQL table to increase performance, but they do have some downsides. Upon creating a new index MySQL builds a separate clock of information that needs to be updated every time there are changes made to the table. This means that if you are constantly executed UPDATE, INSERT and DELETE queries in your table this could have negative impact on performance.
So when introducing index you need to be careful with,
The SELECT queries you going to execute.
What would be your WHERE clause.
How often the table gets updated with data.