天天看點

覆寫索引覆寫索引

覆寫索引

1、當發起一個被索引覆寫的查詢時,在explain的extra列可以看到using index的資訊,此時就使用了覆寫索引

mysql> explain select store_id,film_id from inventory\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: inventory
   partitions: NULL
         type: index
possible_keys: NULL
          key: idx_store_id_film_id
      key_len: 3
          ref: NULL
         rows: 4581
     filtered: 100.00
        Extra: Using index
1 row in set, 1 warning (0.01 sec)

           

2、在大多數存儲引擎中,覆寫索引隻能覆寫那些隻通路索引中部分列的查詢。不過,可以進一步的進行優化,可以使用innodb的二級索引來覆寫查詢。

例如:actor使用innodb存儲引擎,并在last_name字段又二級索引,雖然該索引的列不包括主鍵actor_id,但也能夠用于對actor_id做覆寫查詢

mysql> explain select actor_id,last_name from actor where last_name='HOPPER'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: actor
   partitions: NULL
         type: ref
possible_keys: idx_actor_last_name
          key: idx_actor_last_name
      key_len: 137
          ref: const
         rows: 2
     filtered: 100.00
        Extra: Using index
1 row in set, 1 warning (0.00 sec)

           

繼續閱讀