MySQL一周從入門到精通Day4

2021-10-05 09:18:24 字數 4105 閱讀 5904

可以通過命令臨時設定,也可以通過修改配置永久設定

#檢視是否開啟慢日誌

show variables like

'slow%'

;#臨時開啟慢日誌

set slow_query_log =

'on'

;#設定閾值

set long_query_time =1;

#慢日誌檔案路徑

show variables like

'%datadir%'

;

實操
#登入

mysql -h localhost -u root -p

#檢視是否開啟慢日誌

show variables like

'%slow%'

;#臨時開啟慢日誌

set slow_query_log =

'on'

;//發現報錯,原因是這是個全域性變數

setglobal slow_query_log =

'on'

;#設定閾值

show varibles like

'%long%'

;set long_query_time =1;

#慢日誌輸出到檔案

show variables like

'%log_output%'

;#慢日誌檔案路徑

show variables like

'%datadir%'

;#慢日誌輸出到表

use mysql;

show

tables

;#模擬乙個超過閾值的sql語句

select sleep(11)

;//在c:\programadata\sqlserver8.0\data\mysql 根據自己的安裝目錄,裡面的slow_log開啟,會看到記錄

實操
#登入

mysql -h localhost -u root -p

#選擇資料庫

use mydb;

#檢視表

show

tables

;select

*from employee;

#查詢字段

select

*from employee where name =

'張三'

;select

*from employee where id =5;

#我們加上explain後看一下具體過程

explain

select

*from employee where name =

'張三'

;explain

select

*from employee where id =5;

//可以看出 從連線型別type 索引key 掃瞄行數rows 百分比filters

//都可以看出,索引查詢效率更高

建立索引

刪除索引drop index [indexname] on tablename;

檢視索引show index from tablename;

實操

#登入

mysql -h localhost -u root -p

#選擇資料庫

use mydb;

#檢視表

show

tables

;select

*from employee;

#檢視索引

show

index

from employee\g

//發現對主鍵預設建了乙個索引

#檢視詳細資訊

explain

select

*from employee where name =

'張三' \g

//發現type為all,就是全掃瞄

#建立索引

create

index idx_name on employee(name)

;explain

select

*from employee where name =

'張三' \g

//效率提公升

#刪除索引

drop

index idx_name on employee;

實操

#登入

mysql -h localhost -u root -p

#選擇資料庫

use mydb;

#檢視表

show

tables

;select

*from employee;

#檢視索引

show

index

from employee\g;

//發現對主鍵預設建了乙個索引

#檢視詳細資訊

explain

select

*from employee where name =

'張三' \g

//發現type為all,就是全掃瞄

#建立復合索引

create

index idx_name_salary_dept on employee(name,salary,dept)

;#查詢

explain

select

*from employee where name =

'張三' \g

//效率提公升,只掃瞄一行

explain

select

*from employee where name =

'張三'

and salary =

'5000' \g

explain

select

*from employee where name =

'張三'

and salary =

'5000'

and dept =

'部門a'\g

#部門a

explain

select

*from employee where dept =

'部門a' \g

#薪水explain

select

*from employee where salary =

'5000' \g

- 如何判斷使用了覆蓋索引?

實操

#登入

mysql -h localhost -u root -p

#選擇資料庫

use mydb;

#檢視表

show

tables

;select

*from employee;

#檢視索引

show

index

from employee\g

//發現復合索引,就是我們上一節建立的。name salary dept ,相當於有4個索引

#檢視表資訊

select

*from employee;

#查詢select

*from employee where name =

'張三'

;explain

select

*from employee where name =

'張三' \g

//發現extra為null,沒有覆蓋索引

#查詢id

explain

select name from employee where name =

'張三' \g

//發現extra為using index

explain

select name from employee \g

//發現extra為using index

explain

select name,*** from employee \g

//發現extra為null

MySQL一周從入門到精通Day1

實戰操作 日期 時間 字串 包括 建立,修改,刪除 建立 表名,欄位名,字段型別 mysql語法 create temporary table if not exists table name create definition,table options 可選 示例 create table co...

MySQL一周從入門到精通Day7

2 表 3 字段 實戰 字符集之gb2312裝不下 屌絲 create table t1 name varchar 30 ult charset gb2312 insert into t1 value2 張三 ok insert into t1 values 屌絲 失敗了!gb2312不支援繁體字 ...

mysql從入門到精通

在sql語言中,用join實現表與表的關聯,用on指定聯合表的查詢條件,如 實現三表聯查,可以用table1.table2.來排列 顯示資訊的先後順序 select from user left join address on user id address.user id left join us...