0% found this document useful (0 votes)
8 views

03-Indexing-Partitioning

Uploaded by

minhnh.22bi13291
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

03-Indexing-Partitioning

Uploaded by

minhnh.22bi13291
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

INDEXING

Lê Hồng Hải
UET-VNUH
Today’s Overview

1 Index

2 Compound index

3 Function Index

4 FTS

2
Indexing

 Using indexes to quickly find rows with


specific column values
 Without an index, MySQL must scan the
whole table to locate the relevant rows
The larger table, the slower it searches

3
Query Explain example

Explain SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
jobTitle = 'Sales Rep’;

 MySQL had to scan the whole table to find


the employees with the Sales Rep job title

4
Create Index

 Let’s create an index for the jobTitle column by using the


CREATE INDEX statement:
◼ CREATE INDEX jobTitle ON employees(jobTitle);

EXPLAIN SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
jobTitle = 'Sales Rep';

5
Indexing LIKE Filters

 There are search terms that can be indexed very


well, but others can not
 It is the position of the wild card characters that
make all the difference
 Can not be indexed
◼ SELECT * FROM tbl_name WHERE key_col LIKE
'%Patrick%’;
 Can be indexed
◼ SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%’
◼ SELECT * FROM tbl_name WHERE key_col LIKE
'Pat%_ck%';

6
Compound Index

A compound (composite) index is an index on


multiple columns.
CREATE TABLE Employee (
id INT NOT NULL,
lastname varchar(50) not null,
firstname varchar(50) not null,
PRIMARY KEY (id),
INDEX name (lastname, firstname)
);

7
Compound Index

 The query optimizer uses the composite


indexes for queries that:
◼ Test all columns in the index, or
◼ Test the first columns, the first two
columns, and so on

8
Compound Index

 The following queries use the name index:


SELECT * FROM Employee WHERE lastname='Shah';
SELECT * FROM Employee WHERE lastname ='Shah'
AND firstname ='Mona’
 There are some queries in which composite indexes
will not work:
SELECT * FROM Employee WHERE firstname='Mona';
SELECT * FROM Employee WHERE lastname='Shah'
OR firstname='Mona';

9
Primary Index

 When you create a table with a primary key or


unique key, MySQL automatically creates a
special index named PRIMARY. This index is
called the clustered index.

Why do we need to create ‘productCode’


index?

10
Descending Index

 A descending index is an index that stores


key values in descending order
EXPLAIN SELECT
*
FROM
t
ORDER BY a DESC , b DESC; -- use index a_desc_b_desc

11
Function Index

12
Function Index

 From MySQL 8.0.13, there is support for indexing


using functions
Alter table orders add index
((year(orderDate)), (month(orderDate)));

13
FULLTEXT Search (FTS)

 Full-text search is a technique to search


for documents that don’t perfectly match
the search criteria
 For example, you can search for Wood
and Metal, FTS can return results that
contain the searched words separately

14
LIKE Filter

 MySQL has to scan the whole table to find


the exact text based on a pattern in the
LIKE statement or pattern in the regular
expressions
 It is difficult to have a flexible search
query

15
Create FULLTEXT index

CREATE TABLE table_name(


column_list,
...,
FULLTEXT (column1,column2,..)
);

16
Create FULLTEXT Search

 Create a full-text search in the


productLine column of the products table
using the ALTER TABLE ADD FULLTEXT
statement:
ALTER TABLE products
ADD FULLTEXT(productline);

17
Search using FTS

 You can search for products whose


product lines contain the term Classic .
You use the MATCH() and AGAINST()
functions as the following query:
SELECT
productName,
productLine
FROM products
WHERE
MATCH(productLine)
AGAINST('Classic');

18
Search using FTS

 To search for a product whose product line


contains Classic or Vintage term, you can
use the following query:
SELECT
productName,
productLine
FROM products
WHERE
MATCH(productline)
AGAINST('Classic,Vintage')
ORDER BY productName;

19
Boolean Full-Text Searches

 In the Boolean mode, MySQL searches for


words instead of the concept like in the
natural language search
SELECT productName, productline
FROM products
WHERE MATCH(productName) AGAINST('Truck -
Pickup' IN BOOLEAN MODE )

20
Boolean Full-Text Searches

 To search for rows that contain at least one of the


two words: mysql or tutorial
‘mysql tutorial’
 To search for rows that contain both words:
mysql and tutorial
‘+mysql +tutorial’
 To search for rows that contain the word “mysql”,
but put the higher rank for the rows that contain
“tutorial”:
‘+mysql tutorial’

21
ngramFull-Text Parser

 When it comes to ideographic languages such as


Chinese, Japanese, and Korean, the full-text parser
has a limitation in that these ideographic
languages do not use word delimiters
 MySQL provided the ngram full-text parser. Since
version 5.7.6, MySQL included ngram full-text parser
as a built-in server plugin delimiters

22
PARTITIONING
PARTITIONING

24
PARTITIONING

 Parts of the table are saved as separate


tables in different locations
 Allows distribution of table parts across the
file system according to established rules
(partitioning function)

25
Advantages

 Some queries may be optimal if the data


that satisfies the WHERE clause is
determined to be stored in one or more
partitions
 You can also use partitioning to distribute
the data across different disks
 Partitions are updateable, data can be
reorganized to enhance frequent queries
 Data that is no longer useful can often be
easily removed by deleting the partition

26
Partitioning types

 RANGE: assigns rows to partitions based


on column values within a range
 LIST: similar to RANGE, but the list is a
collection of discrete values
 HASH: based on the value returned by a
user-defined expression (produces an
integer, non-negative value)
 KEY: similar to hash partitioning, except
that the hash function is provided by the
MySQL server

27
Key Partitioning

CREATE TABLE members (


firstname VARCHAR(25) NOT NULL,
lastname VARCHAR(25) NOT NULL,
username VARCHAR(16) NOT NULL,
email VARCHAR(35),
joined DATE NOT NULL
)
PARTITION BY KEY(joined) PARTITIONS 6;

28
Range Partitioning

CREATE TABLE members (


firstname VARCHAR(25) NOT NULL,
lastname VARCHAR(25) NOT NULL,
username VARCHAR(16) NOT NULL,
email VARCHAR(35),
joined DATE NOT NULL
)
PARTITION BY RANGE( YEAR(joined) )(
PARTITION p0 VALUES LESS THAN (1960),
PARTITION p1 VALUES LESS THAN (1970),
PARTITION p2 VALUES LESS THAN (1980),
PARTITION p3 VALUES LESS THAN (1990),
PARTITION p4 VALUES LESS THAN MAXVALUE
);

29
List Partitioning

CREATE TABLE employees (


id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT, store_id INT
) PARTITION BY LIST(store_id) (
PARTITION pNorth VALUES IN (3,5,6,9,17),
PARTITION pEast VALUES IN (1,2,10,11,19,20),
PARTITION pWest VALUES IN (4,12,13,14,18),
PARTITION pCentral VALUES IN (7,8,15,16)
);

30
Partition Management

 ALTER TABLE
 PARTITION BY, ADD PARTITION, DROP PARTITION,
REORGANIZE PARTITION, COALESCE PARTITION

31
Partition Management

 ALTER TABLE trb3 PARTITION BY KEY(id)


PARTITIONS 2;
 ALTER TABLE tr DROP PARTITION p2;
 ALTER TABLE ADD PARTITION (PARTITION p3
VALUES LESS THAN (2000));

32
Partition Information

 SHOW CREATE TABLE


 SHOW TABLE STATUS
 INFORMATION_SCHEMA.PARTITIONS

33
Partitioning on Workbench

34
MySQL Partitioning Limitations

 Foreign keys are not supported


 Partition tables do not support FULL TEXT
searches
 All columns used in partitioning need to be
part of every unique key in the table

https://dev.mysql.com/doc/refman/5.7/en/partitioning-
limitations.html

35
THANKS YOU

You might also like