1. 程式人生 > >Python學習—數據庫篇之索引

Python學習—數據庫篇之索引

radi 主鍵索引 取數據 其他 union span 條件查詢 長度 result

一、索引簡介

索引,是數據庫中專門用於幫助用戶快速查詢數據的一種數據結構。類似於字典中的目錄,查找字典內容時可以根據目錄查找到數據的存放位置,然後直接獲取即可,對於索引,會保存在額外的文件中。在mysql數據庫中,索引是按照B樹的結構來進行存儲的。

                            30

                  10 40                       5 15 35 66                         1
6 11 19 21 39 55 100 在mysql中,索引主要分為以下幾種:   a) 普通索引:僅加速查詢   b) 唯一索引:加速查詢 + 列值唯一(可以有null)   c) 主鍵索引:加速查詢 + 列值唯一 + 表中只有一個(不可以有null)   d) 組合索引:多列值組成一個索引,專門用於組合搜索,其效率大於索引合並   e) 全文索引:對文本的內容進行分詞,進行搜索   f) 索引合並:使用多個單列索引組合搜索   g) 覆蓋索引:select的數據列只用從索引中就能夠取得,不必讀取數據行,換句話說查詢列要被所建的索引覆蓋

二、普通索引

1、創建表示創建索引

1 create table in1(
2     nid int not null auto_increment primary key,
3     name varchar(32) not null,
4     email varchar(64) not null,
5     extra text,
6     index ix_name (name)
7 )

2、添加索引

create index index_name on table_name(column_name)

3、刪除索引

drop index_name on
table_name;

4、查看索引

show index from table_name;

註意:對於創建索引時如果是BLOB 和 TEXT 類型,必須指定length。

create index ix_extra on in1(extra(32));

三、唯一索引

1、創建表時創建索引

create table in1(
    nid int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text,
    unique ix_name (name)
)

2、添加索引

create unique index 索引名 on 表名(列名)

3、刪除索引

drop unique index 索引名 on 表名

四、主鍵索引

其應用場景為:頻繁的同時使用n列來進行查詢

1、創建表時創建索引

create table in3(
    nid int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text
)

2、添加索引

create index ix_name_email on in3(name,email);

3、最左原則

如上創建組合索引之後,必須按照name,email的順序查詢,如:

  • name and email -- 使用索引
  • name -- 使用索引
  • email -- 不使用索引

五、補充

1、執行計劃

explain + 查詢SQL - 用於顯示SQL執行信息參數,根據參考信息可以進行SQL優化

mysql> explain select * from tb2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | tb2   | ALL  | NULL          | NULL | NULL    | NULL |    2 | NULL  |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

主要查看type和rows(rows只能作為參考,並不準確),從range以及其往下的結果是比較理想的,往上的結果是要對表結構進一步優化的。

id
        查詢順序標識
            如:mysql> explain select * from (select nid,name from tb1 where nid < 10) as B;
            +----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+
            | id | select_type | table      | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
            +----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+
            |  1 | PRIMARY     | <derived2> | ALL   | NULL          | NULL    | NULL    | NULL |    9 | NULL        |
            |  2 | DERIVED     | tb1        | range | PRIMARY       | PRIMARY | 8       | NULL |    9 | Using where |
            +----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+
        特別的:如果使用union連接氣值可能為null


    select_type
        查詢類型
            SIMPLE          簡單查詢
            PRIMARY         最外層查詢
            SUBQUERY        映射為子查詢
            DERIVED         子查詢
            UNION           聯合
            UNION RESULT    使用聯合的結果
            ...
    table
        正在訪問的表名


    type
        查詢時的訪問方式,性能:all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const
            ALL             全表掃描,對於數據表從頭到尾找一遍
                            select * from tb1;
                            特別的:如果有limit限制,則找到之後就不在繼續向下掃描
                                   select * from tb1 where email = [email protected]
                                   select * from tb1 where email = [email protected] limit 1;
                                   雖然上述兩個語句都會進行全表掃描,第二句使用了limit,則找到一個後就不再繼續掃描。

            INDEX           全索引掃描,對索引從頭到尾找一遍
                            select nid from tb1;

            RANGE          對索引列進行範圍查找
                            select *  from tb1 where name < alex;
                            PS:
                                between and
                                in
                                >   >=  <   <=  操作
                                註意:!=> 符號


            INDEX_MERGE     合並索引,使用多個單列索引搜索
                            select *  from tb1 where name = alex or nid in (11,22,33);

            REF             根據索引查找一個或多個值
                            select *  from tb1 where name = seven;

            EQ_REF          連接時使用primary key 或 unique類型
                            select tb2.nid,tb1.name from tb2 left join tb1 on tb2.nid = tb1.nid;



            CONST           常量
                            表最多有一個匹配行,因為僅有一行,在這行的列值可被優化器剩余部分認為是常數,const表很快,因為它們只讀取一次。
                            select nid from tb1 where nid = 2 ;

            SYSTEM          系統
                            表僅有一行(=系統表)。這是const聯接類型的一個特例。
                            select * from (select nid from tb1 where nid = 1) as A;
    possible_keys
        可能使用的索引

    key
        真實使用的

    key_len
        MySQL中使用索引字節長度

    rows
        mysql估計為了找到所需的行而要讀取的行數 ------ 只是預估值

    extra
        該列包含MySQL解決查詢的詳細信息
        “Using index”
            此值表示mysql將使用覆蓋索引,以避免訪問表。不要把覆蓋索引和index訪問類型弄混了。
        “Using where”
            這意味著mysql服務器將在存儲引擎檢索行後再進行過濾,許多where條件裏涉及索引中的列,當(並且如果)它讀取索引時,就能被存儲引擎檢驗,因此不是所有帶where子句的查詢都會顯示“Using where”。有時“Using where”的出現就是一個暗示:查詢可受益於不同的索引。
        “Using temporary”
            這意味著mysql在對查詢結果排序時會使用一個臨時表。
        “Using filesort”
            這意味著mysql會對結果使用一個外部索引排序,而不是按索引次序從表裏讀取行。mysql有兩種文件排序算法,這兩種排序方式都可以在內存或者磁盤上完成,explain不會告訴你mysql將使用哪一種文件排序,也不會告訴你排序會在內存裏還是磁盤上完成。
        “Range checked for each record(index map: N)”
            這個意味著沒有好用的索引,新的索引將在聯接的每一行上重新估算,N是顯示在possible_keys列中索引的位圖,並且是冗余的。

詳細

2、以下情況使用索引時要註意

- like %xx
    select * from tb1 where name like %cn;
- 禁止對索引列使用函數,使用函數會改變原來的值為索引的屬性,即使用函數後的值不再是索引了
    select * from tb1 where reverse(name) = wupeiqi;
- or
    select * from tb1 where nid = 1 or email = [email protected];
    特別的:當or條件中有未建立索引的列才失效,以下會走索引
            select * from tb1 where nid = 1 or name = seven;
            select * from tb1 where nid = 1 or email = [email protected] and name = alex
- 類型不一致
    如果列是字符串類型,傳入條件是必須用引號引起來,不然...
    select * from tb1 where name = 999;
- !=
    select * from tb1 where name != alex
    特別的:如果是主鍵,則還是會走索引
        select * from tb1 where nid != 123
- >
    select * from tb1 where name > alex
    特別的:如果是主鍵或索引是整數類型,則還是會走索引
        select * from tb1 where nid > 123
        select * from tb1 where num > 123
- order by
    select email from tb1 order by name desc;
    當根據索引排序時候,選擇的映射如果不是索引,則不走索引
    特別的:如果對主鍵排序,則還是走索引:
        select * from tb1 order by nid desc;
 
- 組合索引最左前綴
    如果組合索引為:(name,email)
    name and email       -- 使用索引
    name                 -- 使用索引
    email                -- 不使用索引

3、其他註意事項

- 避免使用select *
- count(1)或count(列) 代替 count(*)
- 創建表時盡量時 char 代替 varchar
- 表的字段順序固定長度的字段優先
- 組合索引代替多個單列索引(經常使用多個條件查詢時)
- 盡量使用短索引
- 使用連接(JOIN)來代替子查詢(Sub-Queries)
- 連表時註意條件類型需一致
- 索引散列值(重復少)不適合建索引,例:性別不適合

完結撒花,掰掰 !!!

Python學習—數據庫篇之索引