This article describes how to create, view, and delete MySQL table indexes with SQL statements.
1. See which tables add indexed SQL statements
The code is as follows |
Copy Code |
Mysql> Show index from Tblname; Mysql> show the keys from Tblname; |
For example, to execute the "show index from V9_danji" SQL statement to see what indexes the V9_danji table builds, it comes out as follows, you can see the name of the index and the type of index.
view table Index SQL
2. add an indexed SQL statement to a field in a table
The code is as follows |
Copy Code |
ALTER TABLE table_name ADD INDEX index_name (column_list) ALTER TABLE table_name ADD UNIQUE (column_list) ALTER TABLE table_name ADD PRIMARY KEY (column_list) |
This is the use of ALTER TABLE to create a normal index, a unique index, or a primary key index.
You can also use the following SQL statement:
Create index adds either a normal index or a unique index to a table.
The code is as follows |
Copy Code |
CREATE INDEX index_name on table_name (column_list) CREATE UNIQUE INDEX index_name on table_name (column_list) |
3. Delete Table Index
The code is as follows |
Copy Code |
DROP INDEX index_name on Talbe_name ALTER TABLE table_name DROP INDEX index_name ALTER TABLE table_name DROP PRIMARY KEY |
The benefits and costs of indexing the MySQL database
Speaking of improving database performance, index is the most inexpensive things. No need to add memory, do not change the program, do not need to adjust the SQL, as long as the implementation of the correct ' create index ', query speed may be increased by hundreds of thousands of times, this can really be tempting. But the world does not have a free lunch, query speed is increased by inserting, updating, delete the speed of the cost, these write operations, adding a lot of I/O. Because the storage structure of an index is different from that of a table, a table's index takes up more space than the data occupies.
This means that we do a lot of extra work while writing the database, and this job is just to improve the efficiency of reading. Therefore, we establish an index that must ensure that the index is not "at a loss".
It is generally necessary to observe such a rule:
The fields of the index must be fields that are often used as query criteria;
If you index more than one field, the first field is frequently used as a query condition. This index does not work if only the second field is a query condition;
The fields of the index must be sufficiently distinguishable;
Mysql supports prefix indexing for long fields;