Discussion
Indexes allow quick access to data sets in tables, and are vital to the proper functioning of most databases. SQL Server 2000 provides two basic types of indexes, non-clustered and clustered, and we will examine how to implement these indexes in MySQL. Because MySQL’s index features are dependent on the underlying storage engine that a table is using, we will specify the relevant storage engines being used during the index migration.
The non-clustered index is a typical style BTREE index and maps easily to MySQL’s BTREE index, supported both in MyISAM and InnoDB storage engines. To create such an index, one can use the same syntax as in SQL Server:
CREATE [UNIQUE] INDEX emp_phone on employee(phone)
or the alternate MySQL specific syntax:
ALTER TABLE employee ADD [UNIQUE] INDEX (phone)
The clustered index in SQL Server saves the table data in the leaves of the index itself, allowing very fast access to the full row data in the table based on the indexed columns of the table. MySQL’s InnoDB storage engine requires that each table store its data in this clustered way based on a PRIMARY KEY or UNIQUE NOT NULL column specified for a table, so if there was a clustered index like this in SQL Server:
CREATE UNIQUE CLUSTERED INDEX emp_clustered ON employee(phone)
then with MySQL, the same could be accomplished with a table defined like:
CREATE TABLE employee (phone varchar(20) PRIMARY KEY, ... ) ENGINE=INNODB
A non-unique clustered index in SQL Server does not map to MySQL so readily, since InnoDB will only cluster a table based on a PRIMARY KEY or UNIQUE column, but to achieve the similar effect, one might have a PRIMARY KEY based on a natural key starting with the column that one is interested in indexing for clustering purposes.
Note when managing indexes with MySQL, that multiple index changes should be made in one ALTER TABLE statement because MySQL will rebuild all of the data and indexes associated with a table whenever the indexes on that table are modified. Such a statement would look like:
ALTER TABLE employee drop index phone, add index (phone,dept)
There are some options that one can use when creating an index in SQL Server 2000, and will suggest here comparable options for enabling the desired functionality where possible:
| INDEX CREATION OPTIONS |
| SQL Server 2000 |
MySQL |
| ASC | DESC |
Supports these index creation options syntax but ignores them currently |
| ON Filegroup |
Use INDEX DIRECTORY table option |
| PAD_INDEX |
There is no equivalent functionality |
| FILLFACTOR |
There is no equivalent functionality |
| IGNORE_DUP_KEY |
Use special insert syntax, INSERT IGNORE INTO tbl_name … |
| DROP_EXISTING |
Create index or alter table commands already rebuild a table from scratch |
| STATISTICS_NO_RECOMPUTE |
Statistics are not usually an issue for MySQL indexes, however if statistics get out of date, one can issue an ANALYZE TABLE command on a table. This is not done by default. |
| SORT_IN_TEMPDB |
There is no equivalent functionality |
Related Issues
Other index related issues include MySQL having additional FULLTEXT and SPATIAL indexes implemented with its MyISAM storage engine for special types of searching that BTREE indexes do not well support.
Also MySQL does not currently support indexed views, and views themselves are only available starting in the 5.0.1 release of MySQL.