12. Create an index for the column of the table

Like the book catalog database indexes like, you can speed up the query data query the database when you create an index on the field, then to the index as a query, the query by the primary key is the fastest, each table can have only one primary key column, but there may be more common index column, the primary key column requires that all contents of the column must be unique, while the average index column does not require the content must be unique primary keys on a similar school number when we learn in school, the class is unique, whole key for each record in the table is the only table that uniquely identifies a record.

12.1 create a primary key index for the table method

mysql> use oldboy
Database changed
mysql> create table student(
 -> id int(4) not null AUTO_INCREMENT,
 -> name char(20) not null,
 -> age tinyint(2) not null default '0',
-> dept varchar(16) default null,
->primary key(id),
->KEY index_name(name)
->);​

 

Tip:
primary key (ID) <- primary key
KEY index_name (name) <-name ordinary index field
only for the primary key of type int and can use auto_increment

12.2 View student table structure

mysql> show create table student\G;
*************************** 1. row ***************************
 Table: student
Create Table: CREATE TABLE `student` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
 `name` char(20) NOT NULL,
 `age` tinyint(2) NOT NULL DEFAULT '0',
 `dept` varchar(16) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `index_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> describe student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)​

 

12.3 how to delete a table's primary key

mysql> alter table student drop primary key;​

 

Tip: If the primary key in a table set AUTO_INCREMENT (automatic increase), then it is deleted can not afford

12.4 using the alter command to modify the auto-increment id as the primary key column

mysql> alter table student change id id int primary key auto_increment;

 

After the construction of the table using the alter 12.5 increase in the general index

index_name indexes created when construction of the table to delete
mysql> select database();
+------------+
| database()
+------------+
| oldboy |
+------------+
1 row in set (0.00 sec)
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------+
| Table | Create Table
|
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------+
| student | CREATE TABLE `student` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
 `name` char(20) NOT NULL,
 `age` tinyint(2) NOT NULL DEFAULT '0',
 `dept` varchar(16) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `index_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------+
1 row in set (0.00 sec)
Delete general index
mysql> alter table student drop index index_name;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
---+
| Table | Create Table
|
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
---+
| student | CREATE TABLE `student` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
 `name` char(20) NOT NULL,
 `age` tinyint(2) NOT NULL DEFAULT '0',
 `dept` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
---+
1 row in set (0.00 sec)
Create a regular index
mysql> use oldboy
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> alter table student add index index_name (name);
Query OK, 0 rows affected (0.47 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------+
| Table | Create Table
|
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------+
| student | CREATE TABLE `student` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
 `name` char(20) NOT NULL,
 `age` tinyint(2) NOT NULL DEFAULT '0',
 `dept` varchar(16) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `index_name` (` name`) Tip: when accessing large amounts of data, not suitable for the establishment of normal index, will affect
User access, indexing service underestimate when Try to choose
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------+
1 row in set (0.00 sec)​

 

 12.6 create a common index for the first n characters table fields

The first n characters when faced with large table columns, the first n characters of the column content is close to unique in all content, then you can build an index on the column, without having to build this index the entire column, so create a system can save space occupied by the index, as well as reading and updating maintenance system to reduce resource consumption index.
N characters created for the general index of the former field syntax:
the Create index index_name ON Student (name (8)); conditions are listed first N characters to create the index
below to combat demonstration:

mysql> select database();
+------------+
| database() |
+------------+
| oldboy |
+------------+
1 row in set (0.00 sec)
Create a regular index for the first eight characters of the column dept
mysql> describe student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> create index index_name_dept on student (dept(8));
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------+
| Table | Create Table
|
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------+
| student | CREATE TABLE `student` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
 `age` tinyint(2) NOT NULL DEFAULT '0',
 `dept` varchar(16) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `index_name` (`name`),
 KEY `index_name_dept` (`dept`(8))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------+
1 row in set (0.00 sec)​

 

In addition to create a common index of the latter to establish the methodology:

mysql> alter table student add index index_name_dept (dept(8));​

 

12.7 create a joint index for multiple fields of the table

When multiple columns, how we can create a data query multiple queries column joint index, even, you can create an index for the joint first n characters of multi-column column combat demonstration as follows:

mysql> create index index_name_and_dept on student (name,dept);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
 `name` char(20) NOT NULL,
 `age` tinyint(2) NOT NULL DEFAULT '0',
 `dept` varchar(16) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `index_name` (`name`),
 KEY `index_name_dept` (`dept`(8)),
 KEY `index_name_and_dept` (`name`,`dept`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)​

 

12.8 create a joint index for the first n characters of multiple fields tables

mysql> create index index_name_and_dept on student (name(10),dept(10));
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----+
| Table | Create Table
|
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----+
| student | CREATE TABLE `student` (
 `id` int(4) NOT NULL AUTO_INCREMENT,
 `name` char(20) NOT NULL,
 `age` tinyint(2) NOT NULL DEFAULT '0',
 `dept` varchar(16) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `index_name` (`name`),
 KEY `index_name_dept` (`dept`(8)),
 KEY `index_name_and_dept` (`name`(10),`dept`(10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----+
1 row in set (0.00 sec)​

 

Tip: Press the condition column of the query data, joint index is prefixed into force characteristic
index (a, b, c) only a, ab, abc three columns query can take the index, b, bc, ac, c, etc. can not be used indexed

Try to put the most commonly used as a query column, in the first place set

12.9 primary key can also do a joint multi-column indexes

*************************** 1. row ***************************
 Table: user
Create Table: CREATE TABLE `user` (
 `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
 `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
 `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
 `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
 `ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT
'',
 `ssl_cipher` blob NOT NULL,
 `x509_issuer` blob NOT NULL,
 `x509_subject` blob NOT NULL,
 `max_questions` int(11) unsigned NOT NULL DEFAULT '0',
 `max_updates` int(11) unsigned NOT NULL DEFAULT '0',
 `max_connections` int(11) unsigned NOT NULL DEFAULT '0',
 `max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
 `plugin` char(64) COLLATE utf8_bin DEFAULT '',
 `authentication_string` text COLLATE utf8_bin,
 PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and
global privileges'
1 row in set (0.00 sec)
ERROR:
No query specified​ 

 

12.10 Statistical field column number of unique values

  1. mysql> select user from mysql.user;
    +-----------+
    | user |
    +-----------+
    | root |
    | blog |
    | oldgirl |
    | wordpress |
    | oldgirl |
    | oldboy |
    | oldgirl |
    | root |
    +-----------+
    8 rows in set (0.00 sec)
    mysql> select count(distinct user) from mysql.user;
    +----------------------+
    | count(distinct user) |
    +----------------------+
    | 5 |
    +----------------------+
    1 row in set (0.06 sec)
    Tip: Try to build indexes on large tables and more unique values

     

12.11 Create a unique index (primary key)

mysql> show create table student ;
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------------+
| Table | Create Table
|
+---------+----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
--------------------------------------+
| student | CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
`age` tinyint(2) NOT NULL DEFAULT '0',
`dept` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_age` (`age`),
KEY `index_name` (`name`),
KEY `index_name_dept` (`dept`(8)),
KEY `index_name_and_dept` (`name`(10),`dept`(10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)​

 

And create conditions for the entry into force of 12.12 index column

One problem: Since the index can speed up queries, then give all the columns indexed, right?
Answer: because the index will not only occupy system space, the data will also be required to maintain and update the index data, therefore, the index is double-edged sword, is not possible, for example: no small table on the tens to hundreds of lines indexing, updating frequently, read less and less to the table indexing
problem: the need to create a column on which the index?
answer: select user, host from mysql.user where password = ..., index must be created on condition row after where, while data on the selected column after not select, in addition, we want to try to choose the column is indexed on the number of unique values in a large table, such as: gender column unique values, not suitable for indexing

Guess you like

Origin www.cnblogs.com/hackerlin/p/12539630.html