MyISAM结构下面的data有3种文件(.frm,MYD,MYI)分别代表表结构,数据和索引文件。
【连接】
cd /usr/local/webdev/mysql/bin/mysql -uroot -proot
也可以像windows下那样设置环境变量然后不用输入完整的mysql路径了,方法如下:
vi /etc/profile
OK,重启机器生效。(这是对所有用户都生效的)
【操作】
a)创建视图
mysql> create view v_t1 as select * from t1 where id=3;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
| v_t1 |
+----------------+
2 rows in set (0.00 sec)
b)复制表
mysql> create table t2 like t1;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
| t2 |
| v_t1 |
+----------------+
3 rows in set (0.00 sec)
mysql> insert into t2 select * from t1;
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
c)重命名表
mysql> rename table t2 to t1;
Query OK, 0 rows affected (0.00 sec)
【索引】
mysql> desc t1;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(30) | NO | MUL | | |
+----------+------------------+------+-----+---------+----------------+
mysql> show index from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | 0 | PRIMARY | 1 | id | A | 7 | NULL | NULL | | BTREE | |
| t1 | 1 | username | 1 | username | A | NULL | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
了解上面的表结构和索引我们开始下面的几种测试。
影响查询速度的原因是查询影响的行数。
【a】去除索引
先说一个使用的小技巧,如果不记得命令的使用语法,可以通过下面的方式来查询。
mysql> ? drop index
Name: 'DROP INDEX'
Description:
Syntax:
DROP INDEX index_name ON tbl_name /*这里就是语法,如果不记得可以这样进行查询*/
DROP INDEX drops the index named index_name from the table tbl_name.
This statement is mapped to an ALTER TABLE statement to drop the index.
See [HELP ALTER TABLE].
URL: http://dev.mysql.com/doc/refman/5.0/en/drop-index.html
OK,现在我们去除t1表的索引username
mysql> drop index username on t1;
Query OK, 7 rows affected (0.09 sec)
Records: 7 Duplicates: 0 Warnings: 0
首先查询下带主键索引的id列
mysql> desc select * from t1 where id=4\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: const
possible_keys: PRIMARY /*可能用到的索引*/
key: PRIMARY /*实际用到的索引*/
key_len: 4
ref: const
rows: 1 /*存在索引所以影响的行数是1行*/
Extra:
1 row in set (0.00 sec)
现在来查询下不带索引的username列
mysql> desc select * from t1 where username="user4"\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 7 /*这里是关键,没有索引,mysql则会进行全表扫描,如果数据量非常大的情况下,效率肯定是很低的*/
Extra: Using where
1 row in set (0.00 sec)
【like】
like模糊匹配开头不可以使用%,如果使用了则用不到索引。
【and,or】
or左边和右边必须全部加上索引,否则查询时候不走索引,我们来看下
mysql> show index from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | 0 | PRIMARY | 1 | id | A | 7 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
mysql> explain select * from t1 where username="user5" or id=4\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ALL
possible_keys: PRIMARY
key: NULL /*实际上并没有走索引*/
key_len: NULL
ref: NULL
rows: 7 /*所以是全表扫描的*/
Extra: Using where
1 row in set (0.00 sec)
mysql> alter table t1 add index (username);
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> explain select * from t1 where username="user5" or id=4\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: index_merge
possible_keys: PRIMARY,username
key: username,PRIMARY /*走索引了*/
key_len: 32,4
ref: NULL
rows: 2 /*所以影响2行*/
Extra: Using union(username,PRIMARY); Using where
1 row in set (0.00 sec)
还有一点就是如果查询的类型和原本字段类型不相同时也是不走索引的。
【handler_read_rnd_next】
mysql> show status like "Handler_read%";
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 30 |
| Handler_read_next | 14 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 268 | /*如果这个值越高则说明很多查询语句需要用到索引,可以结合满查询,explain进行分析*/
+-----------------------+-------+
6 rows in set (0.00 sec)