MySQL基礎學習筆記 索引

2021-09-26 07:12:34 字數 1830 閱讀 9622

show

index

from 表名;

# 語法

alter

table 表名 add

index

[索引名]

(列名,..

.)# 給name欄位新增索引,命名方式習慣:i_欄位名(i表示index)

alter

table gongfu add

index i_name(name)

;

# 語法

alter

table 表名 drop

index 索引名;

# 如果不知道說明可以先檢視索引

show

create

table 表名;

# 或者

shwo index

from 表名;

# 刪除name的索引

show

index form hero;

alter

table hero drop

index i_name;

# 建立測試表:testindex表

create

table

(title varchar(10

));

# 利用資料庫程式設計想資料庫中插入50萬條資料

import pymysql

conn = pymysql.connect(host=

"localhost"

, port=

3306

, user=

"root"

, password=

"1237894560"

, database=

"heima"

,charset=

"utf8"

)cursor = conn.cursor(

)sql =

"insert into testindex value(%s)"

try:

for i in

range

(500000):

cursor.execute(sql,

['ha-'

+str

(i)]

) conn.commit(

)except exception as err:

print

("sql操作失敗,開始回滾"

, err)

conn.rollback(

)finally

: cursor.close(

) conn.close(

)

# 開啟執行時間檢測

set profiling =1;

# 查詢第50萬條資料ha-499999

select

*from testindex where title=

"ha-499999"

;# 檢視執行時間

show profiles;

# 給title新增索引

alter

table testindex add

index i_title(title)

;# 再次查詢第50萬條資料ha-499999

select

*from testindex where title=

"ha-499999"

;# 再次檢視執行的時間

show profiles;

mysql基礎操作學習筆記(2) 索引

為什麼要建立索引?在此本人也帶著相同的疑問,能夠解釋的僅僅是 為了減少資料庫查詢時所需要的速度。如果正常查詢和索引查詢所需時間相差很多倍時我們自然是需要索引的了。想要知道結果,只能等我學得更加深入一點咯。建立索引 建立索引有三種方法 1 在建立表時建立索引 2 使用alter table 語句建立索...

MYSQL索引 學習筆記

索引分類 索引失效 索引帶來的弊端 幫助mysql進行高效查詢的資料結構 有序 在資料之外,資料庫系統還維護著滿足特定查詢演算法的資料結構,這些資料結構以某種方式引用 指向 資料,這樣就可以在這些資料結構上實現高階查詢演算法,這種資料結構就是索引 換言之,索引就是某種資料結構 如下圖所示 左邊是資料...

mysql索引學習筆記

mysql索引學習筆記 1.索引的優劣 優 加快查詢速率 劣 影響對錶的添刪改操作的速率,增大檔案大小 可能索引檔案比資料檔案還大 所以,在往資料庫匯入大量資料之前,應該先暫時刪除索引,資料匯入完成後再統一建立索引。www.2cto.com 2.建立索引的原則 1 不過度索引 2 索引應該建在需要頻...