mysql學習(二)
mysql> drop table t2;
Query OK, 0 rows affected (0.05 sec)
Query OK, 0 rows affected (0.05 sec)
mysql> create table t2(a int primary key ,b varchar(13000));
Query OK, 0 rows affected (0.09 sec)
Query OK, 0 rows affected (0.09 sec)
mysql> insert into t2 select 1,lpad('a',12000,'a');
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t2 select 2,lpad('b',12000,'b');
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> create index idx_t2 on t2(b);
Query OK, 0 rows affected, 2 warnings (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 2
Query OK, 0 rows affected, 2 warnings (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 2
mysql> insert into t2 select 3,lpad('c',12000,'c');
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t2 select 4,lpad('d',12000,'d');
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t2 select 5,lpad('e',12000,'e');
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> explain select * from t2 where a='3';
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref |
rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| 1 | SIMPLE | t2 | const | PRIMARY | PRIMARY | 4 | const |
1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
1 row in set (0.00 sec)
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref |
rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| 1 | SIMPLE | t2 | const | PRIMARY | PRIMARY | 4 | const |
1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
1 row in set (0.00 sec)
mysql> delete from t2 where b<>1;
Query OK, 5 rows affected, 5 warnings (0.05 sec)
Query OK, 5 rows affected, 5 warnings (0.05 sec)
mysql> select count(1) from t2;
+----------+
| count(1) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
+----------+
| count(1) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql> insert into t2 select 5,lpad('e',12000,'e');
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> explain select * from t2 where a='3';
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows
| Extra |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-----------------------------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL
| Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-----------------------------------------------------+
1 row in set (0.00 sec)
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows
| Extra |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-----------------------------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL
| Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> explain select * from t2 where a='5';
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref |
rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| 1 | SIMPLE | t2 | const | PRIMARY | PRIMARY | 4 | const |
1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
1 row in set (0.00 sec)
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref |
rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
| 1 | SIMPLE | t2 | const | PRIMARY | PRIMARY | 4 | const |
1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+
------+-------+
1 row in set (0.00 sec)
mysql> explain select * from t2;
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows
| Extra |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-------+
| 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 1
| |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-------+
1 row in set (0.00 sec)
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows
| Extra |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-------+
| 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 1
| |
+----+-------------+-------+------+---------------+------+---------+------+-----
-+-------+
1 row in set (0.00 sec)
mysql> explain select a from t2;
+----+-------------+-------+-------+---------------+---------+---------+------+-
-----+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref |
rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+-
-----+-------------+
| 1 | SIMPLE | t2 | index | NULL | PRIMARY | 4 | NULL |
1 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+-
-----+-------------+
1 row in set (0.00 sec)
+----+-------------+-------+-------+---------------+---------+---------+------+-
-----+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref |
rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+-
-----+-------------+
| 1 | SIMPLE | t2 | index | NULL | PRIMARY | 4 | NULL |
1 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+-
-----+-------------+
1 row in set (0.00 sec)
綜上可以看到,表中有主鍵等索引時不一定使用。如果mysql發現根據主鍵值沒找到對應的值,Extra列顯示“ Impossible WHERE noticed after reading const tables”
根據查詢的不同,type會顯示是隻查詢索引(index),全表掃描方式查詢(ALL),還是根據索引查詢多列(const),或者沒有匹配結果(NULL)
根據查詢的不同,type會顯示是隻查詢索引(index),全表掃描方式查詢(ALL),還是根據索引查詢多列(const),或者沒有匹配結果(NULL)
我在t2表上的b欄位建立了普通索引,其建表語句為:
mysql> show create table t2;
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
| Table | Create Table
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
| t2 | CREATE TABLE `t2` (
`a` int(11) NOT NULL,
`b` varchar(13000) DEFAULT NULL,
PRIMARY KEY (`a`),
KEY `idx_t2` (`b`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
1 row in set (0.00 sec)
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
| t2 | CREATE TABLE `t2` (
`a` int(11) NOT NULL,
`b` varchar(13000) DEFAULT NULL,
PRIMARY KEY (`a`),
KEY `idx_t2` (`b`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------+
1 row in set (0.00 sec)
mysql允許的最大列長度是65535,超過時報錯。可以改為text或blobs等大欄位方式儲存。
mysql> alter table t2 add t3 varchar(13000);
ERROR 1118 (42000): Row size too large. The maximum row size for the used table
type, not counting BLOBs, is 65535. You have to change some columns to TEXT or B
LOBs
接下來嘗試新建一列,用於建立全文索引的實驗
mysql> alter table t2 add column t3 varchar(1300);
Query OK, 1 row affected (0.27 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> alter table t2 add column t3 varchar(1300);
Query OK, 1 row affected (0.27 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> create fulltext index idx_t2b on t2(t3);
ERROR 1214 (HY000): The used table type doesn't support FULLTEXT indexes
ERROR 1214 (HY000): The used table type doesn't support FULLTEXT indexes
注意到上面的show create table語句中,t2表示innodb型別表,不能建立全文索引。只有myisam引擎的表才可以。
mysql> create table tab_a as select * from t2 engine=MYISAM;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '=MYIS
AM' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '=MYIS
AM' at line 1
mysql> create table tab_a (a int(10),b varchar(12000)) engine=MYISAM;
Query OK, 0 rows affected (0.08 sec)
Query OK, 0 rows affected (0.08 sec)
mysql> insert into tab_a select 5,lpad('e',12000,'e');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> create fulltext index idx_taba on tab_a(b);
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> show create table tab_a;
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----+
| Table | Create Table
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----+
| tab_a | CREATE TABLE `tab_a` (
`a` int(10) DEFAULT NULL,
`b` varchar(12000) DEFAULT NULL,
FULLTEXT KEY `idx_taba` (`b`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----+
1 row in set (0.00 sec)
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----+
| tab_a | CREATE TABLE `tab_a` (
`a` int(10) DEFAULT NULL,
`b` varchar(12000) DEFAULT NULL,
FULLTEXT KEY `idx_taba` (`b`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----+
1 row in set (0.00 sec)
注意建表語句中對列長度不再限制。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26451536/viewspace-766650/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql學習二MySql
- MYSQL學習(二) --MYSQL框架MySql框架
- MySql 學習筆記二:索引MySql筆記索引
- MySQL高階學習筆記(二)MySql筆記
- 【Mysql 學習】MyISAM儲存引擎(二)。MySql儲存引擎
- MySQL 學習筆記(二)MVCC 機制MySql筆記MVC
- Mysql學習積累之二[網摘收藏個人學習參考]MySql
- Mysql基礎學習第二天MySql
- MYSQL練習二MySql
- MySQL事務學習筆記(二) 相識篇MySql筆記
- 【Mysql 學習】 MERGE表方面的問題(二)MySql
- 學習MySQLMySql
- MySQL 學習MySql
- Mysql學習MySql
- Golang 學習——interface 介面學習(二)Golang
- Go學習【二】學習資料Go
- hdfs學習(二)
- TypeScript 學習(二)TypeScript
- docker學習(二)Docker
- MyBatis學習(二)MyBatis
- Kaldi學習(二)
- 學習webpack(二)Web
- CSS學習(二)CSS
- Bootstrap學習(二)boot
- RMAN 學習(二)
- Drools學習(二)
- DWR學習(二)
- MySQL學習 - 索引MySql索引
- MySQL深度學習MySql深度學習
- Mysql學習教程MySql
- mysql學習(一)MySql
- 【Mysql 學習】字串MySql字串
- Mysql 5.7 Gtid內部學習(十) 實際案例(二)MySql
- MySQL學習(二)圖形介面管理工具Navicat for MySQL安裝和使用MySql
- 【Mysql 學習】Mysql 日誌(一)MySql
- 【Mysql 學習】mysql 字符集MySql
- 【Mysql 學習】Mysql 儲存引擎MySql儲存引擎
- 函式學習二函式