不需要访问表减少了I/O的次数。
索引通常比表小很多。
由于索引是按照键值顺序存储的(至少在一个页内是这样),对于按照键值进行范围查询时使用的是顺序I/O,相对于离散I/O性能大大提高。
mysql>explain select customer_id,inventory_id,rental_date from rental\G***************************1. row ***************************id: 1select_type: SIMPLEtable: rentalpartitions: NULLtype: indexpossible_keys: NULLkey: rental_datekey_len: 10ref: NULLrows: 16008filtered: 100.00Extra: Using index1row in set, 1 warning (0.01 sec)
mysql> select column_name from information_schema.statistics where index_name='rental_date';+--------------+| column_name |+--------------+| rental_date || inventory_id || customer_id |+--------------+3 rows in set (0.01 sec)
mysql>explain select rental_id,customer_id,inventory_id,rental_date from rental\G***************************1. row ***************************id: 1select_type: SIMPLEtable: rentalpartitions: NULLtype: indexpossible_keys: NULLkey: rental_datekey_len: 10ref: NULLrows: 16008filtered: 100.00Extra: Using index1row in set, 1 warning (0.00 sec)
mysql>explain select * from rental where rental_date='2005-05-24 22:53:30' and inventory_id=367\G***************************1. row ***************************id: 1select_type: SIMPLEtable: rentalpartitions: NULLtype: refpossible_keys: rental_date,idx_fk_inventory_idkey: rental_datekey_len: 8ref: const,constrows: 1filtered: 100.00Extra: NULL1row in set, 1 warning (0.00 sec)
mysql>explain select * from rental where rental_id in (select rental_id from rental where rental_date='2005-05-24 22:53:30' and inventory_id=367)\G***************************1. row ***************************id: 1select_type: SIMPLEtable: rentalpartitions: NULLtype: refpossible_keys: PRIMARY,rental_date,idx_fk_inventory_idkey: rental_datekey_len: 8ref: const,constrows: 1filtered: 100.00Extra: Using index***************************2. row ***************************id: 1select_type: SIMPLEtable: rentalpartitions: NULLtype: eq_refpossible_keys: PRIMARYkey: PRIMARYkey_len: 4ref: sakila.rental.rental_idrows: 1filtered: 100.00Extra: NULL2rows in set, 1 warning (0.00 sec)
mysql> explain analyze select * from rental order by rental_date limit 1000,5\G*************************** 1. row ***************************EXPLAIN: -> Limit/Offset: 5/1000 row(s) (cost=1625.05 rows=5) (actual time=17.487..17.488 rows=5 loops=1)-> Sort: rental.rental_date, limit input to 1005 row(s) per chunk (cost=1625.05 rows=16008) (actual time=17.254..17.435 rows=1005 loops=1)-> Table scan on rental (cost=1625.05 rows=16008) (actual time=0.332..11.348 rows=16044 loops=1)1 row in set (0.02 sec)
mysql> explain analyze select * from rental r1 inner join (select rental_id from rental order by rental_date limit 1000,5) r2 on r1.rental_id=r2.rental_id\G*************************** 1. row ***************************EXPLAIN: -> Nested loop inner join (cost=259.58 rows=5) (actual time=7.015..7.067 rows=5 loops=1)-> Table scan onr2 (cost=115.56 rows=1005) (actual time=0.002..0.003 rows=5 loops=1)-> Materialize (cost=7.83 rows=5) (actual time=6.836..6.838 rows=5 loops=1)-> Limit/Offset: 5/1000 row(s) (cost=7.83 rows=5) (actual time=6.641..6.644 rows=5 loops=1)-> Index scan on rental usingrental_date (cost=7.83 rows=1005) (actual time=3.762..6.568 rows=1005 loops=1)-> Single-row index lookup on r1 usingPRIMARY (rental_id=r2.rental_id) (cost=0.25 rows=1) (actual time=0.044..0.044 rows=1 loops=5)1 row inset (0.02 sec)
欢迎关注我的公众号

文章转载自oracleace,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。





