SlideShare a Scribd company logo
1 of 63
Hệ quản trị cơ sở dữ liệu

Indexing
Dư Phương Hạnh
Bộ 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
hanhdp@vnu.edu.vn
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/optimizatio
2

Hệ quản trị CSDL @ BM HTTT
Problem
 Relation: Employee (ID, Name, Dept, …)
 10 M tuples
 (Filter) Query:
SELECT *
FROM
Employee
WHERE Name = “Bob”

3

Hệ quản trị CSDL @ BM HTTT
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

4

Hệ quản trị CSDL @ BM HTTT
Solution #2
 Storage:
– Employee relation sorted on Name attribute

 Query plan:
– Binary search

5

Hệ quản trị CSDL @ BM HTTT
Solution #2
 Cost:
–
–
–
–
–

6

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 HTTT
Solution #2: Issues
 Filters on different attributes:
SELECT *
FROM Employee
WHERE Dept = “Sales”
 Inserts and Deletes

7

Hệ quản trị CSDL @ BM HTTT
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

8

Hệ quản trị CSDL @ BM HTTT
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
9

Hệ quản trị CSDL @ BM HTTT
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
•

10

Hệ quản trị CSDL @ BM HTTT
Single Attribute Index: General
Construction
A

B

a1

a1

b1

a2

a2

b2

ai

ai

bi

an

an

bn

A = val
A > low
A < high

11

Hệ quản trị CSDL @ BM HTTT
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.

12

Hệ quản trị CSDL @ BM HTTT
Primary index on the ordering key
field

13

Hệ quản trị CSDL @ BM HTTT
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.

14

Hệ quản trị CSDL @ BM HTTT
A Clustering Index Example
 A clustering index on
the DEPTNUMBER
ordering non-key field
of an EMPLOYEE file.

15

Hệ quản trị CSDL @ BM HTTT
Another Clustering Index Example

16

Hệ quản trị CSDL @ BM HTTT
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
17

Hệ quản trị CSDL @ BM HTTT
Example of a Dense Secondary
Index

18

Hệ quản trị CSDL @ BM HTTT
An Example of a Secondary Index

19

Hệ quản trị CSDL @ BM HTTT
Properties of Index Types

20

Hệ quản trị CSDL @ BM HTTT
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 firstlevel index consists of more than one disk block

21

Hệ quản trị CSDL @ BM HTTT
A Two-level Primary Index

22

Hệ quản trị CSDL @ BM HTTT
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

23

Hệ quản trị CSDL @ BM HTTT
A search tree of order p = 3.

24

Hệ quản trị CSDL @ BM HTTT
Dynamic Multilevel Indexes Using BTrees 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

25

Hệ quản trị CSDL @ BM HTTT
Dynamic Multilevel Indexes Using BTrees 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

26

Hệ quản trị CSDL @ BM HTTT
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

27

Hệ quản trị CSDL @ BM HTTT
B-tree Structures

28

Hệ quản trị CSDL @ BM HTTT
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.

29

Hệ quản trị CSDL @ BM HTTT
Insert: các bước thực hiện





30

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 HTTT
Chèn vào nút lá

58
54

31

57

60

62

Hệ quản trị CSDL @ BM HTTT
Chèn vào nút lá

58
54

32

57

60

62

Hệ quản trị CSDL @ BM HTTT
Chèn vào nút lá

58
54

33

57

58

60

62

Hệ quản trị CSDL @ BM HTTT
Chia tách nút lá
61

54

54

34

57

58

60

66

62

Hệ quản trị CSDL @ BM HTTT
Chia tách nút lá

61

54

54

35

57

58

60

66

62

Hệ quản trị CSDL @ BM HTTT
Chia tách nút lá

61

54

54

36

57

58

66

60

61

62

Hệ quản trị CSDL @ BM HTTT
Chia tách nút lá
59
61

54

54

37

57

58

66

60

61

62

Hệ quản trị CSDL @ BM HTTT
Chia tách nút lá

61

54

54

38

57

58

59

66

60

61

62

Hệ quản trị CSDL @ BM HTTT
Chia tách nút con
…

21

99

…

59
40

39

54

[54, 59)

66

74

[ 59, 66)

84

[66,74)
Hệ quản trị CSDL @ BM HTTT
Chia tách nút con
…

21

99

…

59
40

40

54

[54, 59)

66

74

[ 59, 66)

84

[66,74)
Hệ quản trị CSDL @ BM HTTT
Chia tách nút con
66
…

21

99

…

[21,66)
40

41

54

[54, 59)

[66, 99)

59

74

[ 59, 66)

84

[66,74)
Hệ quản trị CSDL @ BM HTTT
Chia tách nút gốc

59
40

42

54

[54, 59)

66

74

[ 59, 66)

84

[66,74)
Hệ quản trị CSDL @ BM HTTT
Chia tách nút gốc

59
40

43

54

[54, 59)

66

74

[ 59, 66)

84

[66,74)
Hệ quản trị CSDL @ BM HTTT
Chia tách nút gốc
66

40

44

54

[54, 59)

59

74

[ 59, 66)

84

[66,74)
Hệ quản trị CSDL @ BM HTTT
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

45

Hệ quản trị CSDL @ BM HTTT
Gộp các lá liền kề
72

…

54

46

58

64

67

85

68

72

75

Hệ quản trị CSDL @ BM HTTT
Gộp các lá liền kề
72

…

54

47

58

64

67

85

68

75

Hệ quản trị CSDL @ BM HTTT
Gộp các lá liền kề
72

…

54

48

58

64

68

67

85

75

Hệ quản trị CSDL @ BM HTTT
Gộp các lá liền kề
72

…

54

49

58

64

68

85

75

Hệ quản trị CSDL @ BM HTTT
Gộp các nút anh em
…

41

48

…

63

52

[52, 59)
50

59

74

[59,63)
Hệ quản trị CSDL @ BM HTTT
Gộp các nút anh em
…

41

48

52

59

…

63

[52, 59)
51

59

[59,63)
Hệ quản trị CSDL @ BM HTTT
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
52

Hệ quản trị CSDL @ BM HTTT
Hệ quản trị cơ sở dữ liệu

Using indexes in MySQL
Dư Phương Hạnh
Bộ 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
hanhdp@vnu.edu.vn
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.
54

Hệ quản trị CSDL @ BM HTTT
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;
55

Hệ quản trị CSDL @ BM HTTT
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:
56

Hệ quản trị CSDL @ BM HTTT
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.

57

Hệ quản trị CSDL @ BM HTTT
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).
58

Hệ quản trị CSDL @ BM HTTT
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).
59

Hệ quản trị CSDL @ BM HTTT
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
60

Hệ quản trị CSDL @ BM HTTT
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';

61

Hệ quản trị CSDL @ BM HTTT
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';

62

Hệ quản trị CSDL @ BM HTTT
Verifying Index Usage
 Next lecture: Optimizing queries.

63

Hệ quản trị CSDL @ BM HTTT

More Related Content

What's hot

Csc4320 chapter 8 2
Csc4320 chapter 8 2Csc4320 chapter 8 2
Csc4320 chapter 8 2bshikhar13
 
23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMS23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMSkoolkampus
 
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMARIntroduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMARBHARATH KUMAR
 
Programming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesProgramming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesFellowBuddy.com
 
Implementation of page table
Implementation of page tableImplementation of page table
Implementation of page tableguestff64339
 
Structure of the page table
Structure of the page tableStructure of the page table
Structure of the page tableduvvuru madhuri
 
Algorithms for External Memory Sorting
Algorithms for External Memory SortingAlgorithms for External Memory Sorting
Algorithms for External Memory SortingMilind Gokhale
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)Tech_MX
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.Muhammad SiRaj Munir
 
Data structure
Data structureData structure
Data structureMohd Arif
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLEDrkhanchanaR
 
Distributed design alternatives
Distributed design alternativesDistributed design alternatives
Distributed design alternativesPooja Dixit
 
Furnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree StructuresFurnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree Structuresijceronline
 

What's hot (19)

Csc4320 chapter 8 2
Csc4320 chapter 8 2Csc4320 chapter 8 2
Csc4320 chapter 8 2
 
23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMS23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMS
 
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMARIntroduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
 
Linked List
Linked ListLinked List
Linked List
 
Programming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesProgramming & Data Structure Lecture Notes
Programming & Data Structure Lecture Notes
 
Implementation of page table
Implementation of page tableImplementation of page table
Implementation of page table
 
Structure of the page table
Structure of the page tableStructure of the page table
Structure of the page table
 
1816 1819
1816 18191816 1819
1816 1819
 
Algorithms for External Memory Sorting
Algorithms for External Memory SortingAlgorithms for External Memory Sorting
Algorithms for External Memory Sorting
 
Question answer
Question answerQuestion answer
Question answer
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
 
Data structure
Data structureData structure
Data structure
 
Ch1- Introduction to dbms
Ch1- Introduction to dbmsCh1- Introduction to dbms
Ch1- Introduction to dbms
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLE
 
Distributed design alternatives
Distributed design alternativesDistributed design alternatives
Distributed design alternatives
 
Normalization
NormalizationNormalization
Normalization
 
Normalization
NormalizationNormalization
Normalization
 
Furnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree StructuresFurnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree Structures
 

Viewers also liked

Viewers also liked (9)

6.1 query optimization overview
6.1 query optimization overview6.1 query optimization overview
6.1 query optimization overview
 
2.2 cac chuong trinh my sql
2.2 cac chuong trinh my sql2.2 cac chuong trinh my sql
2.2 cac chuong trinh my sql
 
2.1 view
2.1 view2.1 view
2.1 view
 
01 gioithieu
01 gioithieu01 gioithieu
01 gioithieu
 
9. partitioning
9. partitioning9. partitioning
9. partitioning
 
7. backup & restore data
7. backup & restore data7. backup & restore data
7. backup & restore data
 
8.replication
8.replication8.replication
8.replication
 
C3 2
C3 2C3 2
C3 2
 
2.3 quan ly truy cap
2.3 quan ly truy cap2.3 quan ly truy cap
2.3 quan ly truy cap
 

Similar to Hệ quản trị CSDL

indexingstructureforfiles-160728120658.pdf
indexingstructureforfiles-160728120658.pdfindexingstructureforfiles-160728120658.pdf
indexingstructureforfiles-160728120658.pdfFraolUmeta
 
Indexing structure for files
Indexing structure for filesIndexing structure for files
Indexing structure for filesZainab Almugbel
 
Adbms 22 dynamic multi level index using b and b+ tree
Adbms 22 dynamic multi level index using b  and b+ treeAdbms 22 dynamic multi level index using b  and b+ tree
Adbms 22 dynamic multi level index using b and b+ treeVaibhav Khanna
 
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)Beat Signer
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMSkoolkampus
 
Index Structures.pptx
Index Structures.pptxIndex Structures.pptx
Index Structures.pptxMBablu1
 
Indexing and Hashing
Indexing and HashingIndexing and Hashing
Indexing and Hashingsathish sak
 
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...Beat Signer
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashingJeet Poria
 
chapter09-120827115409-phpapp01.pdf
chapter09-120827115409-phpapp01.pdfchapter09-120827115409-phpapp01.pdf
chapter09-120827115409-phpapp01.pdfAxmedMaxamuud6
 
Database Performance
Database PerformanceDatabase Performance
Database PerformanceBoris Hristov
 

Similar to Hệ quản trị CSDL (20)

indexingstructureforfiles-160728120658.pdf
indexingstructureforfiles-160728120658.pdfindexingstructureforfiles-160728120658.pdf
indexingstructureforfiles-160728120658.pdf
 
Indexing structure for files
Indexing structure for filesIndexing structure for files
Indexing structure for files
 
A41001011
A41001011A41001011
A41001011
 
Adbms 22 dynamic multi level index using b and b+ tree
Adbms 22 dynamic multi level index using b  and b+ treeAdbms 22 dynamic multi level index using b  and b+ tree
Adbms 22 dynamic multi level index using b and b+ tree
 
ch12
ch12ch12
ch12
 
11885558.ppt
11885558.ppt11885558.ppt
11885558.ppt
 
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
 
Ch12
Ch12Ch12
Ch12
 
Storage struct
Storage structStorage struct
Storage struct
 
Unit 08 dbms
Unit 08 dbmsUnit 08 dbms
Unit 08 dbms
 
Indexing
IndexingIndexing
Indexing
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Index Structures.pptx
Index Structures.pptxIndex Structures.pptx
Index Structures.pptx
 
Indexing and Hashing
Indexing and HashingIndexing and Hashing
Indexing and Hashing
 
Unit08 dbms
Unit08 dbmsUnit08 dbms
Unit08 dbms
 
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
 
Ardbms
ArdbmsArdbms
Ardbms
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashing
 
chapter09-120827115409-phpapp01.pdf
chapter09-120827115409-phpapp01.pdfchapter09-120827115409-phpapp01.pdf
chapter09-120827115409-phpapp01.pdf
 
Database Performance
Database PerformanceDatabase Performance
Database Performance
 

More from Trần Thanh (11)

07 trigger view
07 trigger view07 trigger view
07 trigger view
 
4 trigger
4  trigger4  trigger
4 trigger
 
Chuan
ChuanChuan
Chuan
 
C4 1 tuan 14
C4 1 tuan 14C4 1 tuan 14
C4 1 tuan 14
 
C3 2 (tuan6,7)
C3 2 (tuan6,7)C3 2 (tuan6,7)
C3 2 (tuan6,7)
 
C3 1
C3 1C3 1
C3 1
 
C2 2
C2 2C2 2
C2 2
 
C2 1
C2 1C2 1
C2 1
 
C1
C1C1
C1
 
C4 1
C4 1C4 1
C4 1
 
VoIP with Opensips
VoIP with OpensipsVoIP with Opensips
VoIP with Opensips
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 

Hệ quản trị CSDL

  • 1. Hệ quản trị cơ sở dữ liệu Indexing Dư Phương Hạnh Bộ 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 hanhdp@vnu.edu.vn
  • 2. 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/optimizatio 2 Hệ quản trị CSDL @ BM HTTT
  • 3. Problem  Relation: Employee (ID, Name, Dept, …)  10 M tuples  (Filter) Query: SELECT * FROM Employee WHERE Name = “Bob” 3 Hệ quản trị CSDL @ BM HTTT
  • 4. 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 4 Hệ quản trị CSDL @ BM HTTT
  • 5. Solution #2  Storage: – Employee relation sorted on Name attribute  Query plan: – Binary search 5 Hệ quản trị CSDL @ BM HTTT
  • 6. Solution #2  Cost: – – – – – 6 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 HTTT
  • 7. Solution #2: Issues  Filters on different attributes: SELECT * FROM Employee WHERE Dept = “Sales”  Inserts and Deletes 7 Hệ quản trị CSDL @ BM HTTT
  • 8. 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 8 Hệ quản trị CSDL @ BM HTTT
  • 9. 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 9 Hệ quản trị CSDL @ BM HTTT
  • 10. 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 • 10 Hệ quản trị CSDL @ BM HTTT
  • 11. Single Attribute Index: General Construction A B a1 a1 b1 a2 a2 b2 ai ai bi an an bn A = val A > low A < high 11 Hệ quản trị CSDL @ BM HTTT
  • 12. 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. 12 Hệ quản trị CSDL @ BM HTTT
  • 13. Primary index on the ordering key field 13 Hệ quản trị CSDL @ BM HTTT
  • 14. 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. 14 Hệ quản trị CSDL @ BM HTTT
  • 15. A Clustering Index Example  A clustering index on the DEPTNUMBER ordering non-key field of an EMPLOYEE file. 15 Hệ quản trị CSDL @ BM HTTT
  • 16. Another Clustering Index Example 16 Hệ quản trị CSDL @ BM HTTT
  • 17. 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 17 Hệ quản trị CSDL @ BM HTTT
  • 18. Example of a Dense Secondary Index 18 Hệ quản trị CSDL @ BM HTTT
  • 19. An Example of a Secondary Index 19 Hệ quản trị CSDL @ BM HTTT
  • 20. Properties of Index Types 20 Hệ quản trị CSDL @ BM HTTT
  • 21. 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 firstlevel index consists of more than one disk block 21 Hệ quản trị CSDL @ BM HTTT
  • 22. A Two-level Primary Index 22 Hệ quản trị CSDL @ BM HTTT
  • 23. 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 23 Hệ quản trị CSDL @ BM HTTT
  • 24. A search tree of order p = 3. 24 Hệ quản trị CSDL @ BM HTTT
  • 25. Dynamic Multilevel Indexes Using BTrees 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 25 Hệ quản trị CSDL @ BM HTTT
  • 26. Dynamic Multilevel Indexes Using BTrees 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 26 Hệ quản trị CSDL @ BM HTTT
  • 27. 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 27 Hệ quản trị CSDL @ BM HTTT
  • 28. B-tree Structures 28 Hệ quản trị CSDL @ BM HTTT
  • 29. 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. 29 Hệ quản trị CSDL @ BM HTTT
  • 30. Insert: các bước thực hiện     30 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 HTTT
  • 31. Chèn vào nút lá 58 54 31 57 60 62 Hệ quản trị CSDL @ BM HTTT
  • 32. Chèn vào nút lá 58 54 32 57 60 62 Hệ quản trị CSDL @ BM HTTT
  • 33. Chèn vào nút lá 58 54 33 57 58 60 62 Hệ quản trị CSDL @ BM HTTT
  • 34. Chia tách nút lá 61 54 54 34 57 58 60 66 62 Hệ quản trị CSDL @ BM HTTT
  • 35. Chia tách nút lá 61 54 54 35 57 58 60 66 62 Hệ quản trị CSDL @ BM HTTT
  • 36. Chia tách nút lá 61 54 54 36 57 58 66 60 61 62 Hệ quản trị CSDL @ BM HTTT
  • 37. Chia tách nút lá 59 61 54 54 37 57 58 66 60 61 62 Hệ quản trị CSDL @ BM HTTT
  • 38. Chia tách nút lá 61 54 54 38 57 58 59 66 60 61 62 Hệ quản trị CSDL @ BM HTTT
  • 39. Chia tách nút con … 21 99 … 59 40 39 54 [54, 59) 66 74 [ 59, 66) 84 [66,74) Hệ quản trị CSDL @ BM HTTT
  • 40. Chia tách nút con … 21 99 … 59 40 40 54 [54, 59) 66 74 [ 59, 66) 84 [66,74) Hệ quản trị CSDL @ BM HTTT
  • 41. Chia tách nút con 66 … 21 99 … [21,66) 40 41 54 [54, 59) [66, 99) 59 74 [ 59, 66) 84 [66,74) Hệ quản trị CSDL @ BM HTTT
  • 42. Chia tách nút gốc 59 40 42 54 [54, 59) 66 74 [ 59, 66) 84 [66,74) Hệ quản trị CSDL @ BM HTTT
  • 43. Chia tách nút gốc 59 40 43 54 [54, 59) 66 74 [ 59, 66) 84 [66,74) Hệ quản trị CSDL @ BM HTTT
  • 44. Chia tách nút gốc 66 40 44 54 [54, 59) 59 74 [ 59, 66) 84 [66,74) Hệ quản trị CSDL @ BM HTTT
  • 45. 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 45 Hệ quản trị CSDL @ BM HTTT
  • 46. Gộp các lá liền kề 72 … 54 46 58 64 67 85 68 72 75 Hệ quản trị CSDL @ BM HTTT
  • 47. Gộp các lá liền kề 72 … 54 47 58 64 67 85 68 75 Hệ quản trị CSDL @ BM HTTT
  • 48. Gộp các lá liền kề 72 … 54 48 58 64 68 67 85 75 Hệ quản trị CSDL @ BM HTTT
  • 49. Gộp các lá liền kề 72 … 54 49 58 64 68 85 75 Hệ quản trị CSDL @ BM HTTT
  • 50. Gộp các nút anh em … 41 48 … 63 52 [52, 59) 50 59 74 [59,63) Hệ quản trị CSDL @ BM HTTT
  • 51. Gộp các nút anh em … 41 48 52 59 … 63 [52, 59) 51 59 [59,63) Hệ quản trị CSDL @ BM HTTT
  • 52. 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 52 Hệ quản trị CSDL @ BM HTTT
  • 53. Hệ quản trị cơ sở dữ liệu Using indexes in MySQL Dư Phương Hạnh Bộ 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 hanhdp@vnu.edu.vn
  • 54. 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. 54 Hệ quản trị CSDL @ BM HTTT
  • 55. 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; 55 Hệ quản trị CSDL @ BM HTTT
  • 56. 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: 56 Hệ quản trị CSDL @ BM HTTT
  • 57. 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. 57 Hệ quản trị CSDL @ BM HTTT
  • 58. 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). 58 Hệ quản trị CSDL @ BM HTTT
  • 59. 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). 59 Hệ quản trị CSDL @ BM HTTT
  • 60. 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 60 Hệ quản trị CSDL @ BM HTTT
  • 61. 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'; 61 Hệ quản trị CSDL @ BM HTTT
  • 62. 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'; 62 Hệ quản trị CSDL @ BM HTTT
  • 63. Verifying Index Usage  Next lecture: Optimizing queries. 63 Hệ quản trị CSDL @ BM HTTT

Editor's Notes

  1. 1. A data entry k is an actual data record (with search key value k). 2. A data entry is a hk, rid i pair, where rid is the record id of a data record with search key value k. 3. A data entry is a hk, rid-list i pair, where rid-list is a list of record ids of data records with search key value k.