mysql> create database test;
Query OK, 1 row affected (0.57 sec)
mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table t1(
-> id int unsigned not null auto_increment primary key,
-> name varchar(30)
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.02 sec)
mysql> desc t1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.36 sec)
mysql> insert into t1(name) values ("user1");
Query OK, 1 row affected (0.11 sec)
mysql> insert into t1(name) values ("user2");
Query OK, 1 row affected (0.00 sec)
mysql> create table t2 like t1;
Query OK, 0 rows affected (0.08 sec)
mysql> describe t2;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> insert into t2 select * from t1;
Query OK, 2 rows affected (0.18 sec)
mysql> select * from t2;
+----+-------+
| id | name |
+----+-------+
| 1 | user1 |
| 2 | user2 |
+----+-------+
2 rows in set (0.00 sec)
mysql> create index in_name on t1(name);
Query OK, 0 rows affected (1.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t1 | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| t1 | 1 | in_name | 1 | name | A | 2 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> drop index in_name on t1;
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table t1 add index in_name(name);
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table t1 drop index in_name;
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show session status;
mysql> show global status like "com_insert%";
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Com_insert | 2 |
| Com_insert_select | 2 |
+-------------------+-------+
2 rows in set (0.00 sec)
mysql> show global status like "com_select%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select | 135 |
+---------------+-------+
1 row in set (0.01 sec)
mysql> show global status like "com_update%";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Com_update | 0 |
| Com_update_multi | 0 |
+------------------+-------+
2 rows in set (0.00 sec)
mysql> show global status like "com_delete%";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Com_delete | 0 |
| Com_delete_multi | 0 |
+------------------+-------+
2 rows in set (0.00 sec)
mysql> delete from t2 where id=1;
Query OK, 1 row affected (0.10 sec)
mysql> show global status like "com_delete%";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Com_delete | 1 |
| Com_delete_multi | 0 |
+------------------+-------+
2 rows in set (0.00 sec)
mysql> show status like "innodb_rows%";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Innodb_rows_deleted | 1 |
| Innodb_rows_inserted | 10 |
| Innodb_rows_read | 5 |
| Innodb_rows_updated | 0 |
+----------------------+-------+
4 rows in set (0.00 sec)
mysql> show status like "connections%"
-> ;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections | 78 |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show status like "slow_queries";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 0 |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show variables like "%slow%";
+---------------------+--------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------+
| log_slow_queries | OFF |
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/ubuntu-slow.log |
+---------------------+--------------------------------+
4 rows in set (0.00 sec)
mysql> show variables like "%long%";
+---------------------------------------------------+-----------+
| Variable_name | Value |
+---------------------------------------------------+-----------+
| long_query_time | 10.000000 |
| max_long_data_size | 16777216 |
| performance_schema_events_waits_history_long_size | 10000 |
+---------------------------------------------------+-----------+
3 rows in set (0.00 sec)
mysql> desc select * from t1 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 2
Extra:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> explain select * from t1 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 2
Extra:
1 row in set (0.00 sec)
mysql> desc select * from t1 where name="user1" \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 6
Extra: Using where
1 row in set (0.00 sec)
mysql> desc select * from t1 where id="1" \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
Extra:
1 row in set (0.30 sec)
mysql> show index from t1 \G;
*************************** 1. row ***************************
Table: t1
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
mysql> alter table t1 add index nameindex(name);
Query OK, 0 rows affected (0.21 sec)
mysql> desc select * from t1 where name="user1" \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ref
possible_keys: nameindex
key: nameindex
key_len: 33
ref: const
rows: 1
Extra: Using where; Using index
1 row in set (0.00 sec)
mysql> explain select * from t1 where name like "%ser1" \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: index
possible_keys: NULL
key: nameindex
key_len: 33
ref: NULL
rows: 6
Extra: Using where; Using index
1 row in set (0.00 sec)
mysql> explain select * from t1 where name is not null \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: index
possible_keys: nameindex
key: nameindex
key_len: 33
ref: NULL
rows: 6
Extra: Using where; Using index
1 row in set (0.00 sec)
mysql> explain select * from t1 where name="user1" or id=2 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: index
possible_keys: PRIMARY,nameindex
key: nameindex
key_len: 33
ref: NULL
rows: 6
Extra: Using where; Using index
1 row in set (0.00 sec)
mysql> alter table t1 drop index nameindex;
mysql> explain select * from t1 where name="user1" or id=2 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 6
Extra: Using where
1 row in set (0.00 sec)
and or条件必须全建立了索引才能用到索引,另外必须使用数据库中的类型进行匹配。例如字符型的就不能用整数型的作为查询条件。
mysql> show status like "handler_read%";
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 2 |
| Handler_read_key | 11 |
| Handler_read_last | 0 |
| Handler_read_next | 0 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 1160 |
+-----------------------+-------+
7 rows in set (0.00 sec)