1. distinct對某個列進行去重
select distinct(cno) from score;
mysql> select distinct(cno) from score;
+------+
| cno |
+------+
| 1001 |
| 1002 |
| 1003 |
+------+
3 rows in set (0.00 sec)
mysql> select count(*) from world.city;
+----------+
| count(*) |
+----------+
| 4079 |
+----------+
1 row in set (0.05 sec)
mysql> select count(distinct(name)) from world.city;
+-----------------------+
| count(distinct(name)) |
+-----------------------+
| 3998 |
+-----------------------+
1 row in set (0.01 sec)
2. 別名
表別名
SELECT stu.sname,co.cname
FROM student as stu
JOIN score as sc
ON stu.sno = sc.sno
JOIN course as co
ON sc.cno = co.cno
WHERE stu.sname = 'zhang3';
列別名
mysql> select count(distinct(name)) as 個數(shù) from world.city;
+--------+
| 個數(shù) |
+--------+
| 3998 |
+--------+
1 row in set (0.01 sec)
3. 外連接(優(yōu)化)
左外鏈接(左邊是小數(shù)據(jù)右邊是大數(shù)據(jù))
select a.name,b.name,b.surfacearea
from city as a
left join country as b
on a.countrycode=b.code
where a.population<100 ;
4. information_schema
1. 視圖
--- 8. 查詢zhang3,學(xué)習(xí)的課程名稱有哪些?
create view zs_v as
SELECT stu.sname,co.cname
FROM student as stu
JOIN score as sc
ON stusno = sc.sno
JOIN course as co
ON sc.cno = co.cno
WHERE stu.sname = 'zhang3';
select * from zs_v;
information_schema視圖庫(虛擬庫)
use information_schema
show tables;
表:元數(shù)據(jù)+數(shù)據(jù)行
------->元數(shù)據(jù)存儲在“基表“,無法直接查詢和修改
------->DDL搞旭、DCL對元數(shù)據(jù)進行修改
------->information_schema(全局類的統(tǒng)計和)和show的語句查詢元數(shù)據(jù)
mysql> mysql> desc tables;
TABLE_SCHEMA 表所在的庫
TABLE_NAME 表名
ENGINE 表的存儲引擎
TABLE_ROWS 表的行數(shù)
AVG_ROW_LENGTH 平均行長度
INDEX_LENGTH 索引長度
-----查詢整個數(shù)據(jù)庫中所有庫對應(yīng)的表名
select table_schema,table_name
from information_schema.`TABLES`;
----查詢world和school庫對應(yīng)的所有表名
select table_schema,table_name
from information_schema.`TABLES`
where table_schema='world';
union all
select table_schema,table_name
from information_schema.`TABLES`
where table_schema='school';
----統(tǒng)計一下每個庫下的表的個數(shù)
select table_schema,count(table_name)
from information_schema.`TABLES`
group by table_schema;
--- 查詢整個數(shù)據(jù)庫中所有的庫對應(yīng)的表名散怖,每個庫顯示成一行
select table_schema,group_concat(table_name)
from information_schema.`TABLES`
group by table_schema;
---統(tǒng)計一下每個庫的真實數(shù)據(jù)量
每張表數(shù)量=AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH
select
table_schema,
count(table_name),
sum(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 as table_mb
from information_schema.`TABLES`
group by table_schema;
----模仿以上命令,對整個數(shù)據(jù)庫下的1000張表進行單獨備份肄渗,排除sys,performance,information_schema
select concat("mysqlump -uroot -p123 ",table_schema," ",table_name,">/tmp/",table_schema,"_",table_name,".sql")
from information_schema.tables
where table_schema not in('sys','performance','information_schema')
into outfile '/tmp/bak.sh';
5. show
show databases; 查看所有庫名
show tables; 查看當前庫下的表名
show tables from world; 查看world數(shù)據(jù)庫下所有表名
show create database; 查看建庫語句
show create table; 查看建表語句
show grants for root@'localhost' 查看用戶權(quán)限信息
show charset; 查看所有字符集
show collation 查看校對規(guī)則
show full processlist 查看數(shù)據(jù)庫連接情況
show status 查看數(shù)據(jù)庫的整體狀態(tài)
show status like '%lock%' 模糊查看數(shù)據(jù)庫的整體狀態(tài)
show variables 查看數(shù)據(jù)庫所有變量情況
show variables like '%innodb%' 查看數(shù)據(jù)庫所有變量情況
show engines; 查看所有支持存儲引擎
show engines innodb status 查看所有innodb存儲引擎狀態(tài)
show binary logs 查看二進制日志情況
show binlog events in 查看二進制日志事件
show relaylog events in 查看relay日志事件
show slave status 查看從庫狀態(tài)
show master status 查看數(shù)據(jù)庫binlog位置信息
show index from world.city 查看表的索引情況
6杭抠、索引
6.0、學(xué)習(xí)環(huán)境準備
mysqlslap --defaults-file=/etc/my.cnf \
--concurrency=100 --iterations=1 --create-schema='oldboy' \
--query="select * from oldboy.t100w where k2='pqde'" engine=innodb \
--number-of-queries=2000 -uroot -p123 -verbose
6.1恳啥、索引作用:
提供類似書中目錄的作用,優(yōu)化查詢
6.2丹诀、索引的種類(算法):
B樹索引
hash索引
R樹
Full test
GIS
6.3钝的、B樹基于不同的查找算法分類介紹
取下一層的最小值組成B樹結(jié)構(gòu)
葉子節(jié)點---------------->枝節(jié)點------------>根節(jié)點
B+tree是優(yōu)化范圍查詢的效率,必須是相鄰節(jié)點
B-tree
B*tree(現(xiàn)在使用)
三者統(tǒng)稱B樹索引
6.4铆遭、在功能上的分類*****
6.4.1硝桩、輔助索引(s)怎么構(gòu)建B樹結(jié)構(gòu)的?
(1)輔助索引基于表的列進行生成
(2)取出索引列的所有值(取出所有鍵值)
(3)進行所有鍵值的排序
(4)將所有的鍵值按順序落到BTree索引的葉子節(jié)點上
(5)進而生成枝節(jié)點和根節(jié)點
(6)葉子節(jié)點除了存儲鍵值外枚荣,還存儲了相鄰葉子節(jié)點的指針碗脊,另外還會保存原表數(shù)據(jù)的指針。
6.4.2橄妆、聚集索引(c)怎么構(gòu)建B樹結(jié)構(gòu)的衙伶?
規(guī)劃和存儲數(shù)據(jù)
(1)建表時設(shè)置主鍵列(ID)
(2)表中進行數(shù)據(jù)存儲祈坠,會按照ID列的順序,有序的存儲一行一行的數(shù)據(jù)到數(shù)據(jù)頁上(這個動作叫做聚集索引組織表)
(3)表中的數(shù)據(jù)頁被作為聚集索引的葉子節(jié)點
(4)把葉子節(jié)點的主鍵值生成上層的枝節(jié)點和根節(jié)點矢劲。
6.4.3赦拘、聚集索引和輔助索引構(gòu)成區(qū)別總結(jié)?
(1)聚集索引只能有一個芬沉,非空唯一躺同,一般是主鍵
(2)輔助索引可以有多個,是配合聚集索引使用
(3)聚集索引葉子節(jié)點就是磁盤的數(shù)據(jù)行存儲的數(shù)據(jù)頁(整行數(shù)據(jù))
(4)mysql是根據(jù)聚集索引組織存儲數(shù)據(jù)丸逸,存儲數(shù)據(jù)時就是按照聚集索引的順序進行存儲數(shù)據(jù)
輔助索引只會提取索引鍵值進行自動排序生成B樹結(jié)構(gòu)
6.5蹋艺、輔助索引細分
單列的輔助索引
聯(lián)合多列輔助索引(覆蓋索引)
唯一索引
6.6、關(guān)于索引樹的高度受什么影響黄刚?(解決方法僅限于大表)
高度最多3層或4層
(1)數(shù)據(jù)行捎谨,分表
(2)索引列字符長度,前綴索引
(3)char varchar(char比varchar高)隘击,表設(shè)計
(4)enum大大減少將來作為索引列的高度侍芝,優(yōu)化索引高度,能用則用