1、select * from table\G 返回結果垂直顯示
2、查詢表信息: show table status like 'testtable'\G
*************************** 1. row ***************************
Name: supplier
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 1
Avg_row_length: 16384
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 8388608
Auto_increment: NULL
Create_time: 2013-05-23 21:19:24
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
3. show create table test_table\G 查看建表信息
*************************** 1. row ***************************
Table: supplier
Create Table: CREATE TABLE `supplier` (
`name` varchar(32) DEFAULT NULL,
`person` varchar(32) DEFAULT NULL,
`tel` varchar(32) DEFAULT NULL,
`phone` varchar(32) DEFAULT NULL,
`location` varchar(64) DEFAULT NULL,
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
4、B-數適合直接查找,而散列適合一定範圍的查找
5、alter table supplier add index index_name(name,location);
創建多列索引增加查詢效率
6、show full processlist;查看當前進程
*************************** 1. row ***************************
Id: 31
User: root
Host: localhost:4665
db: bookshop
Command: Query
Time: 0
State: NULL
Info: show full processlist
*************************** 2. row ***************************
Id: 32
User: root
Host: localhost:30657
db: neil
Command: Sleep
Time: 78
State:
Info: NULL
2 rows in set (0.00 sec)
7、索引性能優化:
(1)整合ddl語句,也就是講多條對同一表的alter 合併到一條語句中
(2)去掉重複的索引
(3)刪除不用的索引
8、數據類型
(1)除非確定一個列可能包含NULL(未知的值),否則最好定義爲NOT NULL
(2)採用mysql提供的ENUM 比如('Male','Female') 優點:緊採用1個字節來存儲255個不同的值、可讀性強、隱含了檢查限制
(3)刪除重複的語句:
select name from supplier where id=1;
select name from supplier where id=2;
select name from supplier where id=3;
改爲
select name from supplier where id in(1,2,3);
開啓分析功能 set profiling=1;
select ...
show profiles;
9、explain 用於SQL查詢計劃(QEP),它的結果能讓我們瞭解SQL優化器如何執行SQL語句,幫助做出最優決策
mysql> explain select * from supplier where num=2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: swid
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
Extra:
1 row in set (0.00 sec)
其中rows表示的試圖分析的累積結果集,因爲通過possilbe_keys的索引來查詢,所以效率很高,如果是非索引結果就要遍歷表中全部結果(全表掃描)