5. indexing
DESCRIPTION
TRANSCRIPT
Hệ quản trị cơ sở dữ liệuHệ quản trị cơ sở dữ liệu
Dư Phương HạnhBộ môn Hệ thống thông tin
Khoa CNTT, trường Đại học Công
nghệ
Đại học Quốc gia Hanoi
IndexingIndexing
Hệ quản trị CSDL @ BM HTTT2
Outline
Introduce Types of Indexes
– Single-level Ordered Indexes– Multilevel Indexes– Dynamic Multilevel Indexes Using B-Trees and B+-Trees
Using indexes on MySQL
Reading: [1]chap. 5+6(13+14)
http://dev.mysql.com/doc/refman/5.5/en/optimization-indexes.html
Hệ quản trị CSDL @ BM HTTT3
Problem
Relation: Employee (ID, Name, Dept, …) 10 M tuples (Filter) Query:
SELECT * FROM Employee WHERE Name = “Bob”
Hệ quản trị CSDL @ BM HTTT4
Solution #1: Full Table Scan
Storage:– Employee relation stored in contiguous blocks
Query plan:– Scan the entire relation, output tuples with
Name = “Bob” Cost:
– Size of each record = 100 bytes– Size of relation = 10 M x 100 = 1 GB– Time @ 20 MB/s ≈ 1 Minute
Hệ quản trị CSDL @ BM HTTT5
Solution #2
Storage:– Employee relation sorted on Name attribute
Query plan:– Binary search
Hệ quản trị CSDL @ BM HTTT6
Solution #2
Cost:– Size of a block: 1024 bytes– Number of records per block: 1024 / 100 = 10– Total number of blocks: 10 M / 10 = 1 M– Blocks accessed by binary search: 20– Total time: 20 ms x 20 = 400 ms
Hệ quản trị CSDL @ BM HTTT7
Solution #2: Issues
Filters on different attributes:
SELECT * FROM EmployeeWHERE Dept = “Sales”
Inserts and Deletes
Hệ quản trị CSDL @ BM HTTT8
Indexes Definition: a database index is an auxiliary data structure
which allows for faster retrieval of data stored in the database
(Usually) independent of physical storage of relation:– Multiple indexes per relation
Disk resident– Large to fit in memory– Persistent
Updated when indexed relation updated– Relation updates costlier– Query cheaper
Hệ quản trị CSDL @ BM HTTT9
Indexes as Access Paths One form of an index: file of entries <field value, pointer to
record>, which is ordered by field value A single-level index is an auxiliary file that makes it more
efficient to search for a record in the data file. The index is called an access path on the field. The index file usually occupies considerably less disk blocks
than the data file because its entries are much smaller A binary search on the index yields a pointer to the file
record Indexes can also be characterized as dense or sparse
– A dense index has an index entry for every search key value (and hence every record) in the data file.
– A sparse (or nondense) index, on the other hand, has index entries for only some of the search values
Hệ quản trị CSDL @ BM HTTT10
Ví dụ Xét quan hệ EMPLOYEE(NAME, SSN, ADDRESS, JOB, SAL, ... ) Biết rằng:
– record size R=150 bytes block size B=512 bytes r=30000 records
Như vậy:– blocking factor Bfr= B div R= 512 div 150= 3 records/block– số lượng block b= (r/Bfr)= (30000/3)= 10000 blocks
Để index trên cột SSN với kích thước VSSN=9 bytes, giả thiết kích thước con trỏ dữ liệu là PR=7 bytes, ta có:– k.thước index entry RI=(VSSN+ PR)=(9+7)=16 bytes– index blocking factor BfrI= B div RI= 512 div 16= 32 entries/block– số lượng index block bl= (r/ BfrI)= (30000/32)= 938 blocks– số lần truy cập index block sử dụng binary search log2bI= log2938=
10 lần– So sánh:
• chi phí trung bình của việc tìm kiếm tuyến tính:(b/2)= 10000/2= 5000 block accesses
• Nếu file dữ liệu được sắp xếp thì chi phí binary search là:
log2r= log230000= 15 block accesses
Hệ quản trị CSDL @ BM HTTT11
b1
2b
ib
nb
a1
2a
ia
na
A B
a1
2a
ia
na
A = val
A > lowA < high
Single Attribute Index: General Single Attribute Index: General ConstructionConstruction
Hệ quản trị CSDL @ BM HTTT12
Types of Single-Level Indexes
Primary Index– Defined on an ordered data file– The data file is ordered on a key field– Includes one index entry for each block in the data file;
the index entry has the key field value for the first record in the block, which is called the block anchor
– A similar scheme can use the last record in a block.– A primary index is a nondense (sparse) index, since it
includes an entry for each disk block of the data file and the keys of its anchor record rather than for every search value.
Hệ quản trị CSDL @ BM HTTT13
Primary index on the ordering key field
Hệ quản trị CSDL @ BM HTTT14
Types of Single-Level Indexes…
Clustering Index– Defined on an ordered data file– The data file is ordered on a non-key field unlike primary
index, which requires that the ordering field of the data file have a distinct value for each record.
– Includes one index entry for each distinct value of the field; the index entry points to the first data block that contains records with that field value.
– It is another example of nondense index where Insertion and Deletion is relatively straightforward with a clustering index.
Hệ quản trị CSDL @ BM HTTT15
A Clustering Index Example
A clustering index on the DEPTNUMBER ordering non-key field of an EMPLOYEE file.
Hệ quản trị CSDL @ BM HTTT16
Another Clustering Index Example
Hệ quản trị CSDL @ BM HTTT17
Types of Single-Level Indexes Secondary Index
– A secondary index provides a secondary means of accessing a file for which some primary access already exists.
– The secondary index may be on a field which is a candidate key and has a unique value in every record, or a non-key with duplicate values.
– The index is an ordered file with two fields.• The first field is of the same data type as some non-ordering
field of the data file that is an indexing field. • The second field is either a block pointer or a record pointer.• There can be many secondary indexes (and hence, indexing
fields) for the same file.– Includes one entry for each record in the data file; hence,
it is a dense index
Hệ quản trị CSDL @ BM HTTT18
Example of a Dense Secondary Index
Hệ quản trị CSDL @ BM HTTT19
An Example of a Secondary Index
Hệ quản trị CSDL @ BM HTTT20
Properties of Index Types
Hệ quản trị CSDL @ BM HTTT21
Multi-Level Indexes
Because a single-level index is an ordered file, we can create a primary index to the index itself;– In this case, the original index file is called the first-level index and the
index to the index is called the second-level index.
We can repeat the process, creating a third, fourth, ..., top level until all entries of the top level fit in one disk block
A multi-level index can be created for any type of first-level index (primary, secondary, clustering) as long as the first-level index consists of more than one disk block
Hệ quản trị CSDL @ BM HTTT22
A Two-level Primary Index
Hệ quản trị CSDL @ BM HTTT23
Multi-Level Indexes
Such a multi-level index is a form of search tree– However, insertion and deletion of new index entries is a
severe problem because every level of the index is an ordered file.
A node in a Search Tree with pointers to subtrees below it
Hệ quản trị CSDL @ BM HTTT24
A search tree of order p = 3.
Hệ quản trị CSDL @ BM HTTT25
Dynamic Multilevel Indexes Using B-Trees and B+-Trees
Most multi-level indexes use B-tree or B+-tree data structures
These data structures are variations of search trees that allow efficient insertion and deletion of new search values.
In B-Tree and B+-Tree data structures, each node corresponds to a disk block
Each node is kept between half-full and completely full
Hệ quản trị CSDL @ BM HTTT26
Dynamic Multilevel Indexes Using B-Trees and B+-Trees (contd.)
An insertion into a node that is not full is quite efficient– If a node is full the insertion causes a split into two nodes
Splitting may propagate to other tree levels A deletion is quite efficient if a node does not
become less than half full If a deletion causes a node to become less than half
full, it must be merged with neighboring nodes
Hệ quản trị CSDL @ BM HTTT27
Difference between B-tree and B+-tree In a B-tree, pointers to data records exist at all
levels of the tree
In a B+-tree, all pointers to data records exists at the leaf-level nodes
A B+-tree can have less levels (or higher capacity of search values) than the corresponding B-tree
Hệ quản trị CSDL @ BM HTTT28
B-tree Structures
Hệ quản trị CSDL @ BM HTTT29
The Nodes of a B+-tree The nodes of a B+-tree
– (a) Internal node of a B+-tree with q –1 search values.– (b) Leaf node of a B+-tree with q – 1 search values and q – 1 data pointers.
Hệ quản trị CSDL @ BM HTTT30
Insert: các bước thực hiện
Chèn vào nút lá Chia tách nút lá Chia tách nút con Chia tách nút gốc
Hệ quản trị CSDL @ BM HTTT31
Chèn vào nút lá
54 57 60 62
58
Hệ quản trị CSDL @ BM HTTT32
54 57 60 62
58
Chèn vào nút lá
Hệ quản trị CSDL @ BM HTTT33
Chèn vào nút lá
54 57 60 62
58
58
Hệ quản trị CSDL @ BM HTTT34
61
54 57 60 6258
54 66
Chia tách nút lá
Hệ quản trị CSDL @ BM HTTT35
61
54 57 60 6258
54 66
Chia tách nút lá
Hệ quản trị CSDL @ BM HTTT36
61
54 57 61 6258
54 66
60
Chia tách nút lá
Hệ quản trị CSDL @ BM HTTT37
61
54 57 61 6258
54 66
60
59
Chia tách nút lá
Hệ quản trị CSDL @ BM HTTT38
61
54 57 61 6258
54 66
60
59
Chia tách nút lá
Hệ quản trị CSDL @ BM HTTT39
59
54 6640
[ 59, 66)[54, 59)
74 84
9921 ……
[66,74)
Chia tách nút con
Hệ quản trị CSDL @ BM HTTT40
59
54 6640 74 84
9921 ……
[ 59, 66)[54, 59) [66,74)
Chia tách nút con
Hệ quản trị CSDL @ BM HTTT41
5954
66
40 74 84
9921 ……
[66, 99)
[ 59, 66)[54, 59)
[21,66)
[66,74)
Chia tách nút con
Hệ quản trị CSDL @ BM HTTT42
54 6640 74 84
59
[ 59, 66)[54, 59) [66,74)
Chia tách nút gốc
Hệ quản trị CSDL @ BM HTTT43
54 6640 74 84
59
[ 59, 66)[54, 59) [66,74)
Chia tách nút gốc
Hệ quản trị CSDL @ BM HTTT44
54
66
40 74 8459
[ 59, 66)[54, 59) [66,74)
Chia tách nút gốc
Hệ quản trị CSDL @ BM HTTT45
Delete: Các bước thực hiện
Xoá key trong nút lá
Phân bố lại key giữa các lá liền kề
Gộp các lá liền kề
Phân bố lại các key giữa hai nút con anh em
Gộp các nút con anh em
Hệ quản trị CSDL @ BM HTTT46
Gộp các lá liền kề
54 58 64 68 72 75
67 85…72
Hệ quản trị CSDL @ BM HTTT47
Gộp các lá liền kề
54 58 64 68 75
67…72 85
Hệ quản trị CSDL @ BM HTTT48
Gộp các lá liền kề
54 58 64 68 75
67…72 85
Hệ quản trị CSDL @ BM HTTT49
Gộp các lá liền kề
54 58 64 68 75
…72 85
Hệ quản trị CSDL @ BM HTTT50
Gộp các nút anh em
41 48 52 63 74
59
[52, 59) [59,63)
……
Hệ quản trị CSDL @ BM HTTT51
Gộp các nút anh em
41 48 52 63
59
[52, 59) [59,63)
59
……
Hệ quản trị CSDL @ BM HTTT52
Exercise
Insert a data entry with key 9 Insert a data entry with key 3 Delete a data entry with key 8, assuming that the left
sibling is checked for possible redistribution Delete a data entry with key 8, assuming that the
right sibling is checked for possible redistribution
Hệ quản trị cơ sở dữ liệuHệ quản trị cơ sở dữ liệu
Dư Phương HạnhBộ môn Hệ thống thông tin
Khoa CNTT, trường Đại học Công
nghệ
Đại học Quốc gia Hanoi
Using indexes in MySQLUsing indexes in MySQL
Hệ quản trị CSDL @ BM HTTT54
How MySQL using indexes
Most MySQL indexes (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT) are stored in B-trees.
MySQL uses indexes for these operations:– To find the rows matching a WHERE clause quickly.– To eliminate rows from consideration. If there is a choice
between multiple indexes, MySQL normally uses the index that finds the smallest number of rows.
– To retrieve rows from other tables when performing joins. MySQL can use indexes on columns more efficiently if they are declared as the same type and size.
Hệ quản trị CSDL @ BM HTTT55
How MySQL using indexes
MySQL uses indexes for these operations (cont):– To find the MIN() or MAX() value for a specific indexed
column key_col. This is optimized by a preprocessor that checks whether you are using WHERE Key_part_N = constant on all key parts that occur before key_col in the index. In this case, MySQL does a single key lookup for each MIN() or MAX() expression and replaces it with a constant. If all expressions are replaced with constants, the query returns at once:
SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE key_part1=10;
Hệ quản trị CSDL @ BM HTTT56
How MySQL using indexes
MySQL uses indexes for these operations (cont):– To sort or group a table if the sorting or grouping is done
on a leftmost prefix of a usable key (for example, ORDER BY key_part1, key_part2). If all key parts are followed by DESC, the key is read in reverse order.
– In some cases, a query can be optimized to retrieve values without consulting the data rows. (An index that provides all the necessary results for a query is called a covering index.) If a query uses only columns from a table that are numeric and that form a leftmost prefix for some key, the selected values can be retrieved from the index tree for greater speed:
Hệ quản trị CSDL @ BM HTTT57
Column Indexes
A single column, storing copies of the values from that column in a data structure, allowing fast lookups for the rows with the corresponding column values.
The B-tree data structure lets the index quickly find a specific value, a set of values, or a range of values, corresponding to operators such as =, >, ≤, BETWEEN, IN, and so on, in a WHERE clause.
The maximum number of indexes per table and the maximum index length is defined per storage engine. All storage engines support at least 16 indexes per table and a total index length of at least 256 bytes. Most storage engines have higher limits.
Hệ quản trị CSDL @ BM HTTT58
Prefix Indexes
With col_name(N) syntax in an index specification, you can create an index that uses only the first N characters of a string column.
Indexing only a prefix of column values in this way can make the index file much smaller.
When you index a BLOB or TEXT column, you must specify a prefix length for the index. For example:
CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
Prefixes can be up to 1000 bytes long (767 bytes for InnoDB tables).
Hệ quản trị CSDL @ BM HTTT59
Multiple-Column Indexes
MySQL can create multiple columns index consist of up to 16 columns
MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on.
If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to find rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on(col1), (col1, col2), and (col1, col2, col3).
Hệ quản trị CSDL @ BM HTTT60
Multiple-Column Index Example
CREATE TABLE test
( id INT NOT NULL,
last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
INDEX name (last_name,first_name));
The name index can be used for lookups in queries that
specify values in a known range for combinations
of last_name and first_name values. It can also be used for
queries that specify just a last_name value because that
column is a leftmost prefix of the index
Hệ quản trị CSDL @ BM HTTT61
Multiple-Column Index Example
The name index is used for lookups in the following queries:
– SELECT * FROM test WHERE last_name='Widenius';
– SELECT * FROM test WHERE last_name='Widenius'
AND first_name='Michael';
– SELECT * FROM test WHERE last_name='Widenius'
AND (first_name='Michael' OR first_name='Monty');
– SELECT * FROM test WHERE last_name='Widenius'
AND first_name >='M' AND first_name < 'N';
Hệ quản trị CSDL @ BM HTTT62
Multiple-Column Index Example
The name index is not used for lookups in the following queries:
– SELECT * FROM test WHERE first_name='Michael';
– SELECT * FROM test WHERE last_name='Widenius' OR
first_name='Michael';
Hệ quản trị CSDL @ BM HTTT63
Verifying Index Usage
Next lecture: Optimizing queries.