High Performance MySQL 7th Chapter MySQL advanced features partition table

Source: Internet
Author: User

Partition table:

A partitioned table is a separate logical table that is implemented by multiple physical tables.

The way MySQL implements partitioned tables is to encapsulate the underlying tables. This means that there is no global index, and the index is built on each of the underlying tables (unlike Oracle).

There are several scenarios for using partitioned tables:

    • The amount of data is very large and cannot be put into memory.
    • Only part of the data is hot-spot data, and other data is historical data.

Limit:

    • A table can only have 1024 partitions, the author is stable under 100, and too many will have performance problems.
    • The partition table cannot use FOREIGN key constraints.
    • The PARTITION by range partition expression must be an integer or an expression that returns an integer.
    • The partitioned by RANGE columns partition can accept reshape, date, and character types as partition parameters.
    • All partitions must use the same storage engine.

Problems that can occur:

    • If you use partition by RANGE, all null values on the partition expression (which is itself null or when using an illegal value) are placed on the first partition. When you do a query, you will not only query where the partition is located, but also query the partition where the first null is located. If the first partition is large, there will be a performance risk. Workaround, reduce the amount of data in the first partition. Or make an empty partition for the first partition.

Attach the partition by range example:

  1. CREATE TABLE Employees (
  2. ID Intnotnull,
  3. FName VARCHAR (30),
  4. LName VARCHAR (30),
  5. Hired Datenotnulldefault '1970-01-01 ',
  6. Separated datenotnulldefault '9999-12-31 ',
  7. RANGE Partitioning
  8. 3001
  9. Job_code Intnotnull,
  10. store_id Intnotnull
  11. )
  12. PARTITION by RANGE (store_id) (
  13. PARTITION p0 VALUES less THAN (6),
  14. PARTITION p1 VALUES less THAN (11),
  15. PARTITION P2 VALUES less THAN (16),
  16. PARTITION P3 VALUES less THAN (21)
  17. )
    • The partition column and index column do not match. For example, a partition is established on column A, an index is established on column B, and the b= is used. When retrieving, if there is no condition for a to limit the partitions to be scanned, the B-index will be looked up on each partition. If the partition is more, it is also a performance risk.

EXPLAIN partitions See which partitions are used
  1. CREATE TABLE trb1 (id INT, name VARCHAR (), purchased DATE)
  2. PARTITION by RANGE (ID)
  3. (
  4. PARTITION p0 VALUES Less THAN (3),
  5. PARTITION p1 VALUES less THAN (7),
  6. PARTITION P2 VALUES less THAN (9),
  7. PARTITION P3 VALUES less THAN (11)
  8. );
  9. INSERT Into trb1 VALUES
  10. (1, 'desk organiser ', ' 2003-10-15 '),
  11. (2, 'CD player ', ' 1993-11-05 '),
  12. Obtaining information about partitions
  13. 3044
  14. (3, 'TV set ', ' 1996-03-10 '),
  15. (4, 'bookcase ', ' 1982-01-10 '),
  16. (5, 'exercise bike ', ' 2004-05-09 '),
  17. (6, 'sofa ', ' 1987-06-05 '),
  18. (7, 'popcorn maker ', ' 2001-11-22 '),
  19. (8, 'aquarium ', ' 1992-08-04 '),
  20. (9, 'study desk ', ' 1984-09-16 '),
  21. (Ten, 'lava lamp ', ' 1998-12-25 ');
  22. mysql> EXPLAIN Partitions SELECT * from Trb1\g
  23. 1. Row ***************************
  24. Id:1
  25. Select_type:simple
  26. Table : Trb1
  27. Partitions:p0,p1,p2,p3
  28. Type:all
  29. Possible_keys:null
  30. Key : NULL
  31. Key_len:null
  32. Ref:null
  33. rows : Ten
  34. Extra:using Filesort
  35. mysql> EXPLAIN Partitions SELECT * FROM TRB1 WHERE ID < 5\g
  36. 1. Row ***************************
  37. Id:1
  38. Select_type:simple
  39. Table : Trb1
  40. Partitions:p0,p1
  41. Type:all
  42. Possible_keys:null
  43. Key : NULL
  44. Key_len:null
  45. Ref:null
  46. rows : Ten
  47. Extra:usingwhere

High Performance MySQL 7th Chapter MySQL advanced features partition table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.