SlideShare a Scribd company logo
1 of 122
Download to read offline
ith
                              nc ew
                       for ma
                     er ns
                  g p itio
            os tin art
         Bo QL p
          M yS

Giuseppe Maxia
MySQL Community Team Lead
twitter: @datacharmer

Thursday, 30 September 2010
about me -Giuseppe Maxia
          a.k.a. The Data Charmer
          MySQL Community Team Lead
          Long time hacking with MySQL features
          Formerly, database consultant, designer, coder.
          A passion for QA
          An even greater passion for open source
          ... and community
          Passionate blogger
          http://datacharmer.blogspot.com




Thursday, 30 September 2010
Slides for this presentation




       http://tinyurl.com/mysql-partitions




     This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.
Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
The problem(s)
     •   Too much data
     •   Not enough RAM
     •   Historical data
     •   Growing data
     •   Rotating data




                              6


Thursday, 30 September 2010
Too much data




                              data




                                     7


Thursday, 30 September 2010
Not enough RAM



                                               RAM



                                               INDEXES
                              data   INDEXES




                                                         8


Thursday, 30 September 2010
Not enough RAM




                                               INDEXES
                                               RAM
                              data   INDEXES




                                                         9


Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
What exactly is this "partitions" thing?
      Logical splitting of tables
      Transparent to user




Thursday, 30 September 2010
Remember the MERGE tables?
                    MERGE TABLE      separate tables
                                     risk of duplicates
                                     insert in each table
                                     no constraints




Thursday, 30 September 2010
It isn't a merge table!
             PARTITIONED TABLE      One table
                                    No risk of duplicates
                                    insert in one table
                                    constraints enforced




Thursday, 30 September 2010
Wait a minute ...
     • WHAT THE HELL DOES "LOGICAL SPLIT" REALLY
       MEANS?
     • LET ME EXPLAIN ...




                                           14


Thursday, 30 September 2010
Physical partitioning (1)
     • Take a map




                                15


Thursday, 30 September 2010
Physical partitioning (2)
     • cut it into pieces




                                16


Thursday, 30 September 2010
Physical partitioning (3)
     • What you have, is several different pieces




                                                    17


Thursday, 30 September 2010
Physical partitioning (4)
     • If you want the map back, you need some application
       (adhesive tape) and you may get it wrong




                                                     18


Thursday, 30 September 2010
Logical partitioning (1)
     • Take a map




                               19


Thursday, 30 September 2010
Logical partitioning (2)
     • fold it to show the piece you need




                                            20


Thursday, 30 September 2010
Logical partitioning (3)
     • what you have is still a map, even if you see only one
       part.




                                                       21


Thursday, 30 September 2010
Logical partitioning (4)
     • if you unfold the map, you still have the whole thing




                                                        22


Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
Partition pruning
     1a - unpartitioned table - SINGLE RECORD



                               select *
                               from
                               table_name
                               where colx =
                               120




Thursday, 30 September 2010
Partition pruning
     1a - unpartitioned table - SINGLE RECORD



                              I   select *
      D                           from
                              N
      A                           table_name
                              D
      T                           where colx =
                              E   120
      A
                              X



Thursday, 30 September 2010
Partition pruning
     1b - unpartitioned table - SINGLE RECORD



                               select *
                               from
                               table_name
                               where colx =
                               350




Thursday, 30 September 2010
Partition pruning
     1c - unpartitioned table - RANGE



                                 select *
                                 from
                                 table_name
                                 where colx
                                 between 120
                                 and 230




Thursday, 30 September 2010
Partition pruning
     2a - table partitioned by colx - SINGLE REC

                    1-99


                100-199          select *
                                 from
                200-299          table_name
                                 where colx =
                300-399          120

                400-499


                500-599
Thursday, 30 September 2010
Partition pruning
     2a - table partitioned by colx - SINGLE REC

                    1-99


                100-199            select *
      D                            from
      A         200-299            table_name
      T                            where colx =
      A                        I
                300-399            120
                              N
                              D
                400-499
                              E
                              X
                500-599
Thursday, 30 September 2010
Partition pruning
     2b - table partitioned by colx - SINGLE REC

                    1-99


                100-199          select *
                                 from
                200-299          table_name
                                 where colx =
                300-399          350

                400-499


                500-599
Thursday, 30 September 2010
Partition pruning
     2c - table partitioned by colx - RANGE

                    1-99

                                  select *
                100-199           from
                                  table_name
                200-299           where colx
                                  between 120
                300-399
                                  and 230
                400-499


                500-599
Thursday, 30 September 2010
Partition pruning

        EXPLAIN
        select *
 before from table_name
        where colx = 120

          EXPLAIN PARTITIONS
   in 5.1 select *
          from table_name
          where colx = 120



Thursday, 30 September 2010
Partition pruning - unpartitioned table
        explain partitions select count(*)
        from table_name where colx=120G
        ***** 1. row ****
                    id: 1
          select_type: SIMPLE
                 table: table_name
            partitions: NULL
                  type: index
        ...




Thursday, 30 September 2010
Partition pruning - unpartitioned table
        explain partitions select count(*)
        from table_name where colx between
        120 and 230G
        ***** 1. row ****
                    id: 1
          select_type: SIMPLE
                 table: table_name
            partitions: NULL
                  type: index
        ...




Thursday, 30 September 2010
Partition pruning - table partitioned by colx
        explain partitions select count(*)
        from table_name where colx between
        120 and 230G
        ***** 1. row ****
                    id: 1
          select_type: SIMPLE
                 table: table_name
            partitions: p02,p03
                  type: index
        ...




Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
Partitioning Types
         Partition a table using CREATE TABLE or ALTER TABLE

         CREATE TABLE <table_name> (<columns>)
           ENGINE=<engine_name>
           PARTITION BY <type> ( <partition_expression> );

         <type> can be
          RANGE
          LIST
          HASH
          KEY




Thursday, 30 September 2010
RANGE
   CREATE TABLE Employee (
     emp_id INT AUTO_INCREMENT,
     name VARCHAR(50),
     store_id TINYINT,
     PRIMARY KEY (emp_id)
   ) ENGINE=MyISAM
   PARTITION BY RANGE (emp_id) (
      PARTITION p0 VALUES LESS THAN             (10000),
      PARTITION p1 VALUES LESS THAN             (20000),
      PARTITION p2 VALUES LESS THAN             (30000),
      PARTITION p3 VALUES LESS THAN             (40000),
      PARTITION p4 VALUES LESS THAN             MAXVALUE)

   • MAXVALUE is optional
   • Partition ranges must be listed smallest to greatest
         and must be integers
Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
Benchmarking partitions
          Compare results
          Unpartitioned vs partitioned
          ISOLATION
          Repeatability
          Check your resources!




Thursday, 30 September 2010
Benchmarking partitions - Compare results
      Execute query
      Record execution time
      CHECK RETRIEVED RECORDS!




Thursday, 30 September 2010
Benchmarking partitions - Unpartitioned vs
  partitioned
      Make it simple.
      Do not change structure
      If needed, remove PK from partitioned table




Thursday, 30 September 2010
Benchmarking partitions - ISOLATION
      Try to reproduce working conditions
      No other servers running while benchmarking
      Restart the server before each test
      Do NOT mix partitioned and unpartitioned tables in
       the same server
      Use MySQL Sandbox




Thursday, 30 September 2010
Benchmarking partitions - Repeatability
      Measure more than once
      Make sure you have the same conditions
      Make sure your results are consistent between runs




Thursday, 30 September 2010
Benchmarking partitions - Check resources
      InnoDB
        check disk space (uses more than MyISAM)
        check CPU usage
      Partitioned MyISAM tables
        use 2 file handles per partition
        If you use more than one partitioned table, count
           total file handles
      If you use Archive partitioned tables
        check CPU usage
        check memory usage



Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
Partitioning by date - limits of partitioning

      Can partition only by
       INTEGER columns
      OR you can partition by
       an expression, which
       must return an integer

Thursday, 30 September 2010
Partitioning by date - your (GOOD) options
      GOOD ideas
        YEAR(date_column)
        TO_DAYS(date_column)



      WHY?
        Both functions optimized for partitions
        Partition pruning does not kick with other functions




Thursday, 30 September 2010
Partitioning by date - HOW TO - YEAR
      CREATE TABLE t1 (
         d date
      )
      PARTITION BY RANGE (YEAR(d))
      (
      PARTITION p01 VALUES LESS THAN   (1999),
      PARTITION p02 VALUES LESS THAN   (2000),
      PARTITION p03 VALUES LESS THAN   (2001),
      PARTITION p04 VALUES LESS THAN   (MAXVALUE)
      );



Thursday, 30 September 2010
Partitioning by date - HOW TO - TO_DAYS
      CREATE TABLE t1 (
         d date)
      PARTITION BY RANGE (TO_DAYS(d))
      (
      PARTITION p01 VALUES LESS THAN
              (TO_DAYS('2008-01-01')),
      PARTITION p02 VALUES LESS THAN
              (TO_DAYS('2008-02-01')),
      PARTITION p03 VALUES LESS THAN
              (TO_DAYS('2008-03-01')),
      PARTITION p04 VALUES LESS THAN (MAXVALUE)
      );
Thursday, 30 September 2010
Partitioning by date - How TO
      How it works
        partition BY FUNCTION
        query BY COLUMN




Thursday, 30 September 2010
Partitioning by date - WRONG!!!
      PARTITION BY RANGE(year(from_date))

      select count(*) from salaries where year
      (from_date) = 1998;
      +----------+
      | count(*) |
      +----------+
      |   247489 |
      +----------+
      1 row in set (2.25 sec)



Thursday, 30 September 2010
Partitioning by date - RIGHT

      PARTITION BY RANGE(year(from_date))

      select count(*) from salaries where
      from_date between '1998-01-01' and
      '1998-12-31';
      +----------+
      | count(*) |
      +----------+
      |   247489 |
      +----------+
      1 row in set (0.46 sec)

Thursday, 30 September 2010
Partitioning by date - EXPLAIN
      explain partitions select count(*) from
      salaries where year(from_date) = 1998G
      ***** 1. row ****
                  id: 1
        select_type: SIMPLE
               table: salaries
          partitions:
      p01,p02,p03,p04,p05,p06,p07,p08,p09,p10,p
      11,p12,p13,p14,p15,p16,p17,p18,p19
                type: index
      ...


Thursday, 30 September 2010
Partitioning by date - EXPLAIN
      explain partitions select count(*) from
      salaries where from_date between
      '1998-01-01' and '1998-12-31'G
      ***** 1. row ****
                  id: 1
        select_type: SIMPLE
               table: salaries
          partitions: p14,p15
      ...




Thursday, 30 September 2010
Partitioning by date with different sizes
      Mixing partitions by year, month, day in the same
       table

      HOW TO:
        Use the "TO_DAYS" function
        set appropriate intervals




Thursday, 30 September 2010
Partitioning by date with different sizes
ALTER TABLE salaries
partition by range (to_days(from_date))
(
# 5 years
        partition p01 values less than (to_days('1985-01-01')),
        partition p06 values less than (to_days('1990-01-01')),
# 1 year
        partition p11 values less than (to_days('1995-01-01')),
        partition p12 values less than (to_days('1996-01-01')),
        partition p13 values less than (to_days('1997-01-01')),
        partition p14 values less than (to_days('1998-01-01')),
        partition p15 values less than (to_days('1999-01-01')),
        partition p16 values less than (to_days('2000-01-01')),




Thursday, 30 September 2010
Partitioning by date with different sizes
ALTER TABLE salaries
partition by range (to_days(from_date))
(
# 1 month
    partition p17 values less than (to_days('2001-01-01')),
    partition p18 values less than (to_days('2001-02-01')),
    partition p19 values less than (to_days('2001-03-01')),
    partition p20 values less than (to_days('2001-04-01')),
    partition p21 values less than (to_days('2001-05-01')),
    partition p22 values less than (to_days('2001-06-01')),
    partition p23 values less than (to_days('2001-07-01')),
    partition p24 values less than (to_days('2001-08-01')),
    partition p25 values less than (to_days('2001-09-01')),
    partition p26 values less than (to_days('2001-10-01')),
    partition p27 values less than (to_days('2001-11-01')),
    partition p28 values less than (to_days('2001-12-01')),




Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
Hands on - Partitioning with MyISAM
      Primary key matters - not always needed
      Size of indexes matters




Thursday, 30 September 2010
employees test database




Thursday, 30 September 2010
How many partitions
      from information_schema.partitions
      +-------+-----------------+----------+
      | pname | expr            | descr    |
      +-------+-----------------+----------+
      | p01   | year(from_date) | 1985     |
      | p02   | year(from_date) | 1986     |

      ...
      | p13   | year(from_date) | 1997     |
      | p14   | year(from_date) | 1998     |
      | p15   | year(from_date) | 1999     |
      | p16   | year(from_date) | 2000     |
        ...
      | p19   | year(from_date) | MAXVALUE |
      +-------+-----------------+----------+
Thursday, 30 September 2010
How many records
      select count(*) from salaries;
       +----------+
       | count(*) |
       +----------+
       | 2844047 |
       +----------+




Thursday, 30 September 2010
How many records in 1998
  not partitioned
      select count(*) from salaries where
      from_date between '1998-01-01' and
      '1998-12-31';
      +----------+
      | count(*) |
      +----------+
      |   247489 |
      +----------+
      1 row in set (1.52 sec)

      # NOT PARTITIONED



Thursday, 30 September 2010
How many records in 1998
  partitioned
      select count(*) from salaries where
      from_date between '1998-01-01' and
      '1998-12-31';
      +----------+
      | count(*) |
      +----------+
      |   247489 |
      +----------+
      1 row in set (0.41 sec)

      # partition p15



Thursday, 30 September 2010
Deleting records in 1998
  NOT partitioned

      delete from salaries where from_date
      between '1998-01-01' and '1998-12-31';
      Query OK, 247489 rows affected (19.13
      sec)



      # NOT PARTITIONED




Thursday, 30 September 2010
Deleting records in 1998
  partitioned

      alter table salaries drop partition p15;
      Query OK, 0 rows affected (1.35 sec)




Thursday, 30 September 2010
Partitions with InnoDB
      Slower than MyISAM
      But more robust
      Requires more storage




Thursday, 30 September 2010
Partitioning with InnoDB - File per table

      CREATE TABLE table_name (
         ...
         ...

      ) ENGINE = INNODB
      ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8
      PARTITION BY XXXX




Thursday, 30 September 2010
Partitions with InnoDB (laptop)
      Key points:
      Takes much more storage than other engines
      engine                                storage
                                             (MB)
      innodb                                     221
      myisam                                     181
      archive                                     74
      innodb partitioned (whole)                 289
      innodb partitioned (file per table)        290
      myisam partitioned                         182
      archive partitioned                         72

Thursday, 30 September 2010
Benchmarking results (laptop)
           engine                query year   query year
                                 2000         2002
           InnoDB                    1.25         1.25

           MyISAM                    1.72         1.73

           Archive                   2.47         2.45

           InnoDB partitioned        0.24         0.10
           whole
           InnoDB Partitioned        0.45         0.10
           (file per table)
           MyISAM partitioned        0.18         0.12

           Archive partitioned       0.22         0.12

Thursday, 30 September 2010
Partitions with InnoDB (huge server)
      Key points:
      Takes much more storage than other engines
      engine                                storage
                                              (GB)
      innodb (with PK)                           330
      myisam (with PK)                           141
      archive                                      13
      innodb partitioned (no PK)                 237
      myisam partitioned (no PK)                 107
      archive partitioned                          13

Thursday, 30 September 2010
Benchmarking results (huge server)
    engine                         6 month range
    InnoDB                              4 min 30s
    MyISAM                               25.03s
    Archive                            22 min 25s
    InnoDB partitioned by month           13.19
    MyISAM partitioned by year            6.31
    MyISAM partitioned by month           4.45
    Archive partitioned by year           16.67
    Archive partitioned by month          8.97



Thursday, 30 September 2010
Partitions with Archive
      Key points:
      REMOVE INDEXES (not supported by archive)
      For pure statistics applications, ARCHIVE can be
       ALMOST AS FAST AS MyISAM!




Thursday, 30 September 2010
Partitions with ARCHIVE - in practice
      CREATE TABLE titles (
          emp_no       INT             NOT NULL,
          title        VARCHAR(50)     NOT NULL,
          from_date    DATE            NOT NULL,
          to_date      DATE,
          KEY          (emp_no),
          FOREIGN KEY (emp_no)
           REFERENCES employees
                (emp_no) ON DELETE CASCADE,
          PRIMARY KEY (emp_no,title,
                        from_date)
      )ENGINE = InnoDB;


Thursday, 30 September 2010
Partitions with ARCHIVE - in practice
      CREATE TABLE titles (
          emp_no       INT             NOT NULL,
          title        VARCHAR(50)     NOT NULL,
          from_date    DATE            NOT NULL,
          to_date      DATE #,
          # KEY          (emp_nono),
          # FOREIGN KEY (emp_no)
          # REFERENCES employees
          #      (emp_no) ON DELETE CASCADE,
          # PRIMARY KEY (emp_no,title,
          #                from_date)
      ) ENGINE = ARCHIVE;


Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
TOOLS
      The INFORMATION_SCHEMA.PARTITIONS table
      The partition helper
        http://datacharmer.blogspot.com/2008/12/partition-
         helper-improving-usability.html
        A Perl script that creates partitioning statements
      The mysqldump_partition_backup




Thursday, 30 September 2010
INFORMATION SCHEMA PARTITIONS table
  |     TABLE_NAME                      |
  |     PARTITION_NAME                  |
  |     SUBPARTITION_NAME               |
  |     PARTITION_ORDINAL_POSITION      |
  |     SUBPARTITION_ORDINAL_POSITION   |
  |     PARTITION_METHOD                |
  |     SUBPARTITION_METHOD             |
  |     PARTITION_EXPRESSION            |
  |     SUBPARTITION_EXPRESSION         |
  |     PARTITION_DESCRIPTION           |
  |     TABLE_ROWS                      |
  |     PARTITION_COMMENT               |

Thursday, 30 September 2010
INFORMATION SCHEMA PARTITIONS table
  select * from partitions where table_name='salaries'
  and table_schema='employees' limit 1G
                   TABLE_SCHEMA: employees
                     TABLE_NAME: salaries
                 PARTITION_NAME: p01
              SUBPARTITION_NAME: NULL
     PARTITION_ORDINAL_POSITION: 1
  SUBPARTITION_ORDINAL_POSITION: NULL
               PARTITION_METHOD: RANGE COLUMNS
            SUBPARTITION_METHOD: NULL
           PARTITION_EXPRESSION: from_date
        SUBPARTITION_EXPRESSION: NULL
          PARTITION_DESCRIPTION: '1985-12-31'
                     TABLE_ROWS: 18238

Thursday, 30 September 2010
INFORMATION SCHEMA PARTITIONS table
  select partition_name,partition_description, table_rows
  from partitions where table_name='salaries' and
  table_schema='employees';
  +----------------+-----------------------+------------+
  | partition_name | partition_description | table_rows |
  +----------------+-----------------------+------------+
  | p01            | '1985-12-31'          |       18238 |
  | p02            | '1986-12-31'          |       37915 |
  | p03            | '1987-12-31'          |       57395 |
  | p04            | '1988-12-31'          |       76840 |
  | p05            | '1989-12-31'          |       95890 |
  | p06            | '1990-12-31'          |      114520 |
  | p07            | '1991-12-31'          |      132578 |
  | p08            | '1992-12-31'          |      151019 |
  | p09            | '1993-12-31'          |      168103 |
  | p10            | '1994-12-31'          |      185121 |
  | p11            | '1995-12-31'          |      201576 |
  | p12            | '1996-12-31'          |      218244 |
  | p13            | '1997-12-31'          |      233144 |
  | p14            | '1998-12-31'          |      247458 |

Thursday, 30 September 2010
The Partition helper
      Introduction, syntax, and examples
      http://forge.mysql.com/tools/tool.php?id=173




Thursday, 30 September 2010
The partition helper
$ partition_helper --help
  The Partition Helper, version 1.0.1
    This program creates a ALTER TABLE
  statement to add or reorganize
  partitions for MySQL 5.1 or later
    (C) 2008 Giuseppe Maxia
syntax: partitions_helper [options]
    -t --table = name
    -c --column = name
    -i --interval = name
      "year", "month", or a number
    -p --partitions = number

...
Thursday, 30 September 2010
The partition helper
...
           --first_partition = number
           --reorganize = name
           -s --start = name
           -e --end = name
           -f --function = name
           -l --list
           --prefix = name
           --explain




Thursday, 30 September 2010
The partition helper example
partitions_helper --table=t1 
   --column=mydate 
   --interval=year 
   --start=1998-01-01 --end=2002-12-01
# partitions: 5
ALTER TABLE t1
PARTITION by range (to_days(mydate))
(
   partition p001 VALUES LESS THAN (to_days('1998-01-01'))
, partition p002 VALUES LESS THAN (to_days('1999-01-01'))
, partition p003 VALUES LESS THAN (to_days('2000-01-01'))
, partition p004 VALUES LESS THAN (to_days('2001-01-01'))
, partition p005 VALUES LESS THAN (to_days('2002-01-01'))
);




Thursday, 30 September 2010
The partition helper example
partitions_helper --table=t1 
   --column=mydate 
   --interval=month 
   --start=1998-01-01 --end=2002-12-01
# partitions: 60
ALTER TABLE t1
PARTITION by range (to_days(mydate))
(
  partition p001 VALUES LESS THAN (to_days('1998-01-01'))
, partition p002 VALUES LESS THAN (to_days('1998-02-01'))
, partition p003 VALUES LESS THAN (to_days('1998-03-01'))

 #[...]
, partition p058 VALUES LESS THAN (to_days('2002-10-01'))
, partition p059 VALUES LESS THAN (to_days('2002-11-01'))
, partition p060 VALUES LESS THAN (to_days('2002-12-01'))
);

Thursday, 30 September 2010
The partition helper example
partitions_helper --table=t1 
   --column=mydate 
   --interval=week 
   --start=1998-01-01 --end=2002-12-01
# partitions: 255
ALTER TABLE t1
PARTITION by range (to_days(mydate))
(
  partition p001 VALUES LESS THAN (to_days('1998-01-01'))
, partition p002 VALUES LESS THAN (to_days('1998-01-08'))
, partition p003 VALUES LESS THAN (to_days('1998-01-15'))

 #[...]
, partition p253 VALUES LESS THAN (to_days('2002-10-31'))
, partition p254 VALUES LESS THAN (to_days('2002-11-07'))
, partition p255 VALUES LESS THAN (to_days('2002-11-14'))
);

Thursday, 30 September 2010
The partition helper example
partitions_helper --table=t1 
   --column=mynum 
   --interval=250 
   --start=1000 --end=3000
# partitions: 8
ALTER TABLE t1
PARTITION by range (mydate)
(
   partition p001 VALUES LESS   THAN   (1250)
, partition p002 VALUES LESS    THAN   (1500)
, partition p003 VALUES LESS    THAN   (1750)
, partition p004 VALUES LESS    THAN   (2000)
, partition p005 VALUES LESS    THAN   (2250)
, partition p006 VALUES LESS    THAN   (2500)
, partition p007 VALUES LESS    THAN   (2750)
, partition p008 VALUES LESS    THAN   (3000)
);

Thursday, 30 September 2010
Partitions maintenance through events
      An article by Greg Haase about this topic
      http://dev.mysql.com/tech-resources/articles/
       partitioning-event_scheduler.html




Thursday, 30 September 2010
mysqldump_partition_backup
      a script created by Roland Bouman
      http://forge.mysql.com/tools/tool.php?id=258
      detects the partitions for a table, and generates
       mysqldump statements for each one




Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
the NULL partition
     • when partitioning by date
     • using TO_DAYS
     • the first partition matters
         partition                   rows
         p1                          100,000
         p2                          150,000
         p3                          120,000
         p4                          100,000
                                               92


Thursday, 30 September 2010
partition       condition              rows
          p1              to_days('1990-01-01') 1,000,000
          p2              to_days('1990-02-01') 1,000,000
          p3              to_days('1990-03-01') 1,000,000
          p4              to_days('1990-04-01') 1,000,000
          p5              to_days('1990-05-01') 1,000,000
          p6              to_days('1990-06-01') 1,000,000




Thursday, 30 September 2010
date_field between
          '1990-04-10' and '1990-04-15'


         partition       condition              rows
         p1              to_days('1990-01-01') 1,000,000
         p2              to_days('1990-02-01') 1,000,000
         p3              to_days('1990-03-01') 1,000,000
         p4              to_days('1990-04-01') 1,000,000
         p5              to_days('1990-05-01') 1,000,000
         p6              to_days('1990-06-01') 1,000,000



                                                = 2,000,000 rows




Thursday, 30 September 2010
alter table t1
      reorganize partition p1 into (
         partition p0 values less than (0),
         partition p1
          values less than (to_days('1990-01-01'))
      );

          partition       condition              rows
          p0              0                      0
          p1              to_days('1990-01-01') 1,000,000
          p2              to_days('1990-02-01') 1,000,000
          p3              to_days('1990-03-01') 1,000,000
          p4              to_days('1990-04-01') 1,000,000
          p5              to_days('1990-05-01') 1,000,000
          p6              to_days('1990-06-01') 1,000,000




Thursday, 30 September 2010
date_field between
          '1990-04-10' and '1990-04-15'


         partition       condition              rows
         p0              0                      0
         p1              to_days('1990-01-01') 1,000,000
         p2              to_days('1990-02-01') 1,000,000
         p3              to_days('1990-03-01') 1,000,000
         p4              to_days('1990-04-01') 1,000,000
         p5              to_days('1990-05-01') 1,000,000
         p6              to_days('1990-06-01') 1,000,000



                                                = 1,000,000 rows




Thursday, 30 September 2010
Lock before inserting single records
     • If you send MANY SINGLE INSERT at once
     • LOCK before inserting

     • No need to do it for bulk inserts




                                                97


Thursday, 30 September 2010
Your needs
                                What
                               Partition pruning
                                Syntax
     Table of
     contents                 Benchmarking

                               Partitioning by date
                               hands on

                                 tools

                                 tips

                               leveraging replication
Thursday, 30 September 2010
Replication schemes
                              INNODB                    INNODB
                              NOT                       NOT
        MASTER                PARTITIONED               PARTITIONED

     concurrent insert                                                SLAVE
                                                                 concurrent read

                     INNODB                  MyISAM
                     PARTITIONED             PARTITIONED
                     BY RANGE                BY RANGE
   SLAVE                                                    SLAVE
                                                                     99
  concurrent batch processing               large batch processing
Thursday, 30 September 2010
Replication schemes
                                INNODB        concurrent insert
                                PARTITIONED
                                BY HASH
        MASTER


                                                        INNODB
                                                        NON
                              MyISAM                    PARTITIONED
                              PARTITIONED
        SLAVE                 BY RANGE                               SLAVE
                                                                     100
              batch processing                           concurrent reads
Thursday, 30 September 2010
Replication schemes - dimensions
                               INNODB                          ARCHIVE
                               PARTITIONED                     PARTITIONED
                               BY HASH                   SLAVE BY RANGE
        MASTER                                                 (locations)

     concurrent insert                                    dimensional processing

     ARCHIVE                                         ARCHIVE
     PARTITIONED              SLAVE          SLAVE   PARTITIONED
     BY RANGE                                        BY RANGE
     (date)                                          (product)
                                                                    101
    dimensional processing                           dimensional processing
Thursday, 30 September 2010
Slides for this presentation




       http://tinyurl.com/mysql-partitions




     This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.
Thursday, 30 September 2010
THANKS!

                              Q&A
                              Comment on Twitter:
                              @datacharmer
Thursday, 30 September 2010
BONUS SLIDES




           NEW
         FEATURES
                              104


Thursday, 30 September 2010
MySQL 5.5 enhancements
     • PARTITION BY RANGE COLUMNS
     • PARTITION BY LIST COLUMNS
     • TO_SECONDS




                                    105


Thursday, 30 September 2010
R E
    MySQL 5.5 enhancements
    CREATE TABLE t (         FO
      dt date
                          B E     5.1
    )
    PARTITION BY RANGE (TO_DAYS(dt))
    (
      PARTITION p01 VALUES LESS THAN
    (TO_DAYS('2007-01-01')),
      PARTITION p02 VALUES LESS THAN
    (TO_DAYS('2008-01-01')),
      PARTITION p03 VALUES LESS THAN
    (TO_DAYS('2009-01-01')),
      PARTITION p04 VALUES LESS THAN
    (MAXVALUE));
                                       106


Thursday, 30 September 2010
R E
    MySQL 5.5 enhancements
                              FO
    SHOW CREATE TABLE t G B E     5.1
           Table: t
    Create Table: CREATE TABLE `t` (
      `dt` date DEFAULT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1
    /*!50100 PARTITION BY RANGE (TO_DAYS
    (dt))
    (PARTITION p01 VALUES LESS THAN
    (733042) ENGINE = MyISAM,
    […]

                                    107


Thursday, 30 September 2010
MySQL 5.5 enhancements
                                  E R
    CREATE TABLE t (
                                FT
      dt date
                              A       5.5
    )
    PARTITION BY RANGE COLUMNS (dt)
    (
      PARTITION p01 VALUES LESS THAN
    ('2007-01-01'),
      PARTITION p02 VALUES LESS THAN
    ('2008-01-01'),
      PARTITION p03 VALUES LESS THAN
    ('2009-01-01'),
      PARTITION p04 VALUES LESS THAN
    (MAXVALUE));
                                   108


Thursday, 30 September 2010
MySQL 5.5 enhancements
                                  E R
                                FT
 SHOW CREATE TABLE t
        Table: t              A       5.5
 Create Table: CREATE TABLE `t` (
   `dt` date DEFAULT NULL
 ) ENGINE=MyISAM DEFAULT
 CHARSET=latin1
 /*!50500 PARTITION BY RANGE COLUMNS
 (dt)
 (PARTITION p01 VALUES LESS THAN
 ('2007-01-01') ENGINE = MyISAM,
 […]
                                   109


Thursday, 30 September 2010
MySQL 5.5 - Multiple columns
    CREATE TABLE t (
      a int,
      b int
    )PARTITION BY RANGE COLUMNS   (a,b)
    (
      PARTITION p01 VALUES LESS   THAN
    (10,1),
      PARTITION p02 VALUES LESS   THAN
    (10,10),
      PARTITION p03 VALUES LESS   THAN
    (10,20),
      PARTITION p04 VALUES LESS   THAN
    (MAXVALUE, MAXVALUE));
                                    110


Thursday, 30 September 2010
partition definition
                                          by range (a,b)
           records            partition          LESS THAN
          a       b             p01            10            10
          1      10             p02            10            20
          10      9             p03            10            30
          10     10             p04            10         MAXVALUE
          10     11             p05      MAXVALUE MAXVALUE




Thursday, 30 September 2010
partition definition
                                                    by range (a,b)
         records
                                        partition          LESS THAN
        a       b
                                          p01            10            10
        1      10
                                          p02            10            20
        10      9
                                          p03            10            30
        10     10                         p04            10         MAXVALUE
        10     11                         p05      MAXVALUE MAXVALUE

                               (1,10) < (10,10) ?

                                      (a < 10)
                                        OR
                              ((a = 10) AND (b < 10))

                                  (1 < 10)
                                    OR                          TRUE
                         ((1 = 10) AND (10 < 10))
Thursday, 30 September 2010
partition definition
                                                   by range (a,b)
       records                         partition          LESS THAN
      a       b                          p01            10            10
      1      10                          p02            10            20
      10      9                          p03            10            30
      10     10                          p04            10         MAXVALUE
      10     11                          p05      MAXVALUE MAXVALUE

                               (10,9) < (10,10) ?

                                      (a < 10)
                                        OR
                              ((a = 10) AND (b < 10))

                                 (10 < 10)
                                    OR                        TRUE
                         ((10 = 10) AND (9 < 10))
Thursday, 30 September 2010
partition definition
                                                   by range (a,b)
       records                         partition          LESS THAN
      a       b                          p01            10            10
      1      10                          p02            10            20
      10      9                          p03            10            30
      10     10                          p04            10         MAXVALUE
      10     11                          p05      MAXVALUE MAXVALUE

                               (10,10) < (10,10) ?

                                      (a < 10)
                                        OR
                              ((a = 10) AND (b < 10))

                                (10 < 10)
                                   OR                        FALSE
                        ((10 = 10) AND (10 < 10))
Thursday, 30 September 2010
partition definition
                                                   by range (a,b)
       records                         partition          LESS THAN
      a       b                          p01            10            10
      1      10                          p02            10            20
      10      9                          p03            10            30
      10     10                          p04            10         MAXVALUE
      10     11                          p05      MAXVALUE MAXVALUE

                               (10,10) < (10,20) ?

                                      (a < 10)
                                        OR
                              ((a = 10) AND (b < 20))

                                (10 < 10)
                                   OR                         TRUE
                        ((10 = 10) AND (10 < 20))
Thursday, 30 September 2010
CREATE TABLE employees (
  emp_no int(11) NOT NULL,
  birth_date date NOT NULL,
  first_name varchar(14) NOT NULL,
  last_name varchar(16) NOT NULL,
  gender char(1) DEFAULT NULL,
  hire_date date NOT NULL
) ENGINE=MyISAM
PARTITION BY RANGE COLUMNS(gender,hire_date)
(PARTITION p01 VALUES LESS THAN ('F','1990-01-01'),
 PARTITION p02 VALUES LESS THAN ('F','2000-01-01'),
 PARTITION p03 VALUES LESS THAN ('F',MAXVALUE),
 PARTITION p04 VALUES LESS THAN ('M','1990-01-01'),
 PARTITION p05 VALUES LESS THAN ('M','2000-01-01'),
 PARTITION p06 VALUES LESS THAN ('M',MAXVALUE),
 PARTITION p07 VALUES LESS THAN (MAXVALUE,MAXVALUE)




                                             116


Thursday, 30 September 2010
MySQL 5.5 enhancements
     • TRUNCATE PARTITION
     • TO_SECONDS()




                              117


Thursday, 30 September 2010
MySQL 5.6 enhancements
     • ALTER TABLE … EXCHANGE PARTITION




                                          118


Thursday, 30 September 2010
ALTER TABLE t1
             EXCHANGE PARTITION p2
             WITH TABLE t2




Thursday, 30 September 2010
t1                t2
           f1     f2       f3    f1    f2       f3
           1      a        xxx        (empty)
  p1       2      b        xxx
           3      c        yyy
           4      d        xxx

           5      e        zzz
  p2
           6      f        www
           7      g        ccc
           8      h        sss
           9      i        www
  p3       10     j        xxx
           11     k        yyy
           12     l        zzz




Thursday, 30 September 2010
t1
           f1     f2       f3           t2
           1      a        xxx   f1    f2       f3
  p1       2      b        xxx        (empty)

           3      c        yyy
           4      d        xxx

           5      e        zzz
  p2
           6      f        www
           7      g        ccc
           8      h        sss
           9      i        www
  p3       10     j        xxx
           11     k        yyy
           12     l        zzz




Thursday, 30 September 2010
t1                  t2
           f1     f2       f3    f1   f2       f3
           1      a        xxx   5    e        zzz
  p1       2      b        xxx   6    f        www
                                 7    g        ccc
           3      c        yyy
                                 8    h        sss
           4      d        xxx

  p2             (empty)
           9      i        www

  p3       10     j        xxx
           11     k        yyy
           12     l        zzz




Thursday, 30 September 2010

More Related Content

More from Giuseppe Maxia

MySQL NDB 8.0 clusters in your laptop with dbdeployer
MySQL NDB 8.0 clusters in your laptop with dbdeployerMySQL NDB 8.0 clusters in your laptop with dbdeployer
MySQL NDB 8.0 clusters in your laptop with dbdeployerGiuseppe Maxia
 
Dbdeployer, the universal installer
Dbdeployer, the universal installerDbdeployer, the universal installer
Dbdeployer, the universal installerGiuseppe Maxia
 
Test complex database systems in your laptop with dbdeployer
Test complex database systems in your laptop with dbdeployerTest complex database systems in your laptop with dbdeployer
Test complex database systems in your laptop with dbdeployerGiuseppe Maxia
 
A quick tour of Mysql 8 roles
A quick tour of Mysql 8 rolesA quick tour of Mysql 8 roles
A quick tour of Mysql 8 rolesGiuseppe Maxia
 
Juggle your data with Tungsten Replicator
Juggle your data with Tungsten ReplicatorJuggle your data with Tungsten Replicator
Juggle your data with Tungsten ReplicatorGiuseppe Maxia
 
Tungsten Replicator tutorial
Tungsten Replicator tutorialTungsten Replicator tutorial
Tungsten Replicator tutorialGiuseppe Maxia
 
Preventing multi master conflicts with tungsten
Preventing multi master conflicts with tungstenPreventing multi master conflicts with tungsten
Preventing multi master conflicts with tungstenGiuseppe Maxia
 
MySQL high availability power and usability
MySQL high availability power and usabilityMySQL high availability power and usability
MySQL high availability power and usabilityGiuseppe Maxia
 
Solving MySQL replication problems with Tungsten
Solving MySQL replication problems with TungstenSolving MySQL replication problems with Tungsten
Solving MySQL replication problems with TungstenGiuseppe Maxia
 
State of the art of MySQL replication and clustering
State of the art of MySQL replication and clusteringState of the art of MySQL replication and clustering
State of the art of MySQL replication and clusteringGiuseppe Maxia
 
Testing mysql creatively in a sandbox
Testing mysql creatively in a sandboxTesting mysql creatively in a sandbox
Testing mysql creatively in a sandboxGiuseppe Maxia
 
Mysql 5.5 and 5.6 replication
Mysql 5.5 and 5.6 replicationMysql 5.5 and 5.6 replication
Mysql 5.5 and 5.6 replicationGiuseppe Maxia
 
Lightning talks percona live mysql_2012
Lightning talks percona live mysql_2012Lightning talks percona live mysql_2012
Lightning talks percona live mysql_2012Giuseppe Maxia
 

More from Giuseppe Maxia (20)

MySQL NDB 8.0 clusters in your laptop with dbdeployer
MySQL NDB 8.0 clusters in your laptop with dbdeployerMySQL NDB 8.0 clusters in your laptop with dbdeployer
MySQL NDB 8.0 clusters in your laptop with dbdeployer
 
Test like a_boss
Test like a_bossTest like a_boss
Test like a_boss
 
Dbdeployer, the universal installer
Dbdeployer, the universal installerDbdeployer, the universal installer
Dbdeployer, the universal installer
 
Test complex database systems in your laptop with dbdeployer
Test complex database systems in your laptop with dbdeployerTest complex database systems in your laptop with dbdeployer
Test complex database systems in your laptop with dbdeployer
 
Dbdeployer
DbdeployerDbdeployer
Dbdeployer
 
Dbdeployer
DbdeployerDbdeployer
Dbdeployer
 
A quick tour of Mysql 8 roles
A quick tour of Mysql 8 rolesA quick tour of Mysql 8 roles
A quick tour of Mysql 8 roles
 
MySQL document_store
MySQL document_storeMySQL document_store
MySQL document_store
 
Replication skeptic
Replication skepticReplication skeptic
Replication skeptic
 
Juggle your data with Tungsten Replicator
Juggle your data with Tungsten ReplicatorJuggle your data with Tungsten Replicator
Juggle your data with Tungsten Replicator
 
MySQL in your laptop
MySQL in your laptopMySQL in your laptop
MySQL in your laptop
 
Script it
Script itScript it
Script it
 
Tungsten Replicator tutorial
Tungsten Replicator tutorialTungsten Replicator tutorial
Tungsten Replicator tutorial
 
Preventing multi master conflicts with tungsten
Preventing multi master conflicts with tungstenPreventing multi master conflicts with tungsten
Preventing multi master conflicts with tungsten
 
MySQL high availability power and usability
MySQL high availability power and usabilityMySQL high availability power and usability
MySQL high availability power and usability
 
Solving MySQL replication problems with Tungsten
Solving MySQL replication problems with TungstenSolving MySQL replication problems with Tungsten
Solving MySQL replication problems with Tungsten
 
State of the art of MySQL replication and clustering
State of the art of MySQL replication and clusteringState of the art of MySQL replication and clustering
State of the art of MySQL replication and clustering
 
Testing mysql creatively in a sandbox
Testing mysql creatively in a sandboxTesting mysql creatively in a sandbox
Testing mysql creatively in a sandbox
 
Mysql 5.5 and 5.6 replication
Mysql 5.5 and 5.6 replicationMysql 5.5 and 5.6 replication
Mysql 5.5 and 5.6 replication
 
Lightning talks percona live mysql_2012
Lightning talks percona live mysql_2012Lightning talks percona live mysql_2012
Lightning talks percona live mysql_2012
 

Recently uploaded

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
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
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
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
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
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
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
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
 

Recently uploaded (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
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
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
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
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
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
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
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
 

Boosting performance with Mysql partitions

  • 1. ith nc ew for ma er ns g p itio os tin art Bo QL p M yS Giuseppe Maxia MySQL Community Team Lead twitter: @datacharmer Thursday, 30 September 2010
  • 2. about me -Giuseppe Maxia  a.k.a. The Data Charmer  MySQL Community Team Lead  Long time hacking with MySQL features  Formerly, database consultant, designer, coder.  A passion for QA  An even greater passion for open source  ... and community  Passionate blogger  http://datacharmer.blogspot.com Thursday, 30 September 2010
  • 3. Slides for this presentation http://tinyurl.com/mysql-partitions This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. Thursday, 30 September 2010
  • 4. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 5. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 6. The problem(s) • Too much data • Not enough RAM • Historical data • Growing data • Rotating data 6 Thursday, 30 September 2010
  • 7. Too much data data 7 Thursday, 30 September 2010
  • 8. Not enough RAM RAM INDEXES data INDEXES 8 Thursday, 30 September 2010
  • 9. Not enough RAM INDEXES RAM data INDEXES 9 Thursday, 30 September 2010
  • 10. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 11. What exactly is this "partitions" thing?  Logical splitting of tables  Transparent to user Thursday, 30 September 2010
  • 12. Remember the MERGE tables? MERGE TABLE  separate tables  risk of duplicates  insert in each table  no constraints Thursday, 30 September 2010
  • 13. It isn't a merge table! PARTITIONED TABLE  One table  No risk of duplicates  insert in one table  constraints enforced Thursday, 30 September 2010
  • 14. Wait a minute ... • WHAT THE HELL DOES "LOGICAL SPLIT" REALLY MEANS? • LET ME EXPLAIN ... 14 Thursday, 30 September 2010
  • 15. Physical partitioning (1) • Take a map 15 Thursday, 30 September 2010
  • 16. Physical partitioning (2) • cut it into pieces 16 Thursday, 30 September 2010
  • 17. Physical partitioning (3) • What you have, is several different pieces 17 Thursday, 30 September 2010
  • 18. Physical partitioning (4) • If you want the map back, you need some application (adhesive tape) and you may get it wrong 18 Thursday, 30 September 2010
  • 19. Logical partitioning (1) • Take a map 19 Thursday, 30 September 2010
  • 20. Logical partitioning (2) • fold it to show the piece you need 20 Thursday, 30 September 2010
  • 21. Logical partitioning (3) • what you have is still a map, even if you see only one part. 21 Thursday, 30 September 2010
  • 22. Logical partitioning (4) • if you unfold the map, you still have the whole thing 22 Thursday, 30 September 2010
  • 23. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 24. Partition pruning 1a - unpartitioned table - SINGLE RECORD select * from table_name where colx = 120 Thursday, 30 September 2010
  • 25. Partition pruning 1a - unpartitioned table - SINGLE RECORD I select * D from N A table_name D T where colx = E 120 A X Thursday, 30 September 2010
  • 26. Partition pruning 1b - unpartitioned table - SINGLE RECORD select * from table_name where colx = 350 Thursday, 30 September 2010
  • 27. Partition pruning 1c - unpartitioned table - RANGE select * from table_name where colx between 120 and 230 Thursday, 30 September 2010
  • 28. Partition pruning 2a - table partitioned by colx - SINGLE REC 1-99 100-199 select * from 200-299 table_name where colx = 300-399 120 400-499 500-599 Thursday, 30 September 2010
  • 29. Partition pruning 2a - table partitioned by colx - SINGLE REC 1-99 100-199 select * D from A 200-299 table_name T where colx = A I 300-399 120 N D 400-499 E X 500-599 Thursday, 30 September 2010
  • 30. Partition pruning 2b - table partitioned by colx - SINGLE REC 1-99 100-199 select * from 200-299 table_name where colx = 300-399 350 400-499 500-599 Thursday, 30 September 2010
  • 31. Partition pruning 2c - table partitioned by colx - RANGE 1-99 select * 100-199 from table_name 200-299 where colx between 120 300-399 and 230 400-499 500-599 Thursday, 30 September 2010
  • 32. Partition pruning EXPLAIN select * before from table_name where colx = 120 EXPLAIN PARTITIONS in 5.1 select * from table_name where colx = 120 Thursday, 30 September 2010
  • 33. Partition pruning - unpartitioned table explain partitions select count(*) from table_name where colx=120G ***** 1. row **** id: 1 select_type: SIMPLE table: table_name partitions: NULL type: index ... Thursday, 30 September 2010
  • 34. Partition pruning - unpartitioned table explain partitions select count(*) from table_name where colx between 120 and 230G ***** 1. row **** id: 1 select_type: SIMPLE table: table_name partitions: NULL type: index ... Thursday, 30 September 2010
  • 35. Partition pruning - table partitioned by colx explain partitions select count(*) from table_name where colx between 120 and 230G ***** 1. row **** id: 1 select_type: SIMPLE table: table_name partitions: p02,p03 type: index ... Thursday, 30 September 2010
  • 36. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 37. Partitioning Types  Partition a table using CREATE TABLE or ALTER TABLE  CREATE TABLE <table_name> (<columns>) ENGINE=<engine_name> PARTITION BY <type> ( <partition_expression> );  <type> can be  RANGE  LIST  HASH  KEY Thursday, 30 September 2010
  • 38. RANGE CREATE TABLE Employee ( emp_id INT AUTO_INCREMENT, name VARCHAR(50), store_id TINYINT, PRIMARY KEY (emp_id) ) ENGINE=MyISAM PARTITION BY RANGE (emp_id) ( PARTITION p0 VALUES LESS THAN (10000), PARTITION p1 VALUES LESS THAN (20000), PARTITION p2 VALUES LESS THAN (30000), PARTITION p3 VALUES LESS THAN (40000), PARTITION p4 VALUES LESS THAN MAXVALUE) • MAXVALUE is optional • Partition ranges must be listed smallest to greatest and must be integers Thursday, 30 September 2010
  • 39. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 40. Benchmarking partitions  Compare results  Unpartitioned vs partitioned  ISOLATION  Repeatability  Check your resources! Thursday, 30 September 2010
  • 41. Benchmarking partitions - Compare results  Execute query  Record execution time  CHECK RETRIEVED RECORDS! Thursday, 30 September 2010
  • 42. Benchmarking partitions - Unpartitioned vs partitioned  Make it simple.  Do not change structure  If needed, remove PK from partitioned table Thursday, 30 September 2010
  • 43. Benchmarking partitions - ISOLATION  Try to reproduce working conditions  No other servers running while benchmarking  Restart the server before each test  Do NOT mix partitioned and unpartitioned tables in the same server  Use MySQL Sandbox Thursday, 30 September 2010
  • 44. Benchmarking partitions - Repeatability  Measure more than once  Make sure you have the same conditions  Make sure your results are consistent between runs Thursday, 30 September 2010
  • 45. Benchmarking partitions - Check resources  InnoDB  check disk space (uses more than MyISAM)  check CPU usage  Partitioned MyISAM tables  use 2 file handles per partition  If you use more than one partitioned table, count total file handles  If you use Archive partitioned tables  check CPU usage  check memory usage Thursday, 30 September 2010
  • 46. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 47. Partitioning by date - limits of partitioning  Can partition only by INTEGER columns  OR you can partition by an expression, which must return an integer Thursday, 30 September 2010
  • 48. Partitioning by date - your (GOOD) options  GOOD ideas  YEAR(date_column)  TO_DAYS(date_column)  WHY?  Both functions optimized for partitions  Partition pruning does not kick with other functions Thursday, 30 September 2010
  • 49. Partitioning by date - HOW TO - YEAR CREATE TABLE t1 ( d date ) PARTITION BY RANGE (YEAR(d)) ( PARTITION p01 VALUES LESS THAN (1999), PARTITION p02 VALUES LESS THAN (2000), PARTITION p03 VALUES LESS THAN (2001), PARTITION p04 VALUES LESS THAN (MAXVALUE) ); Thursday, 30 September 2010
  • 50. Partitioning by date - HOW TO - TO_DAYS CREATE TABLE t1 ( d date) PARTITION BY RANGE (TO_DAYS(d)) ( PARTITION p01 VALUES LESS THAN (TO_DAYS('2008-01-01')), PARTITION p02 VALUES LESS THAN (TO_DAYS('2008-02-01')), PARTITION p03 VALUES LESS THAN (TO_DAYS('2008-03-01')), PARTITION p04 VALUES LESS THAN (MAXVALUE) ); Thursday, 30 September 2010
  • 51. Partitioning by date - How TO  How it works  partition BY FUNCTION  query BY COLUMN Thursday, 30 September 2010
  • 52. Partitioning by date - WRONG!!! PARTITION BY RANGE(year(from_date)) select count(*) from salaries where year (from_date) = 1998; +----------+ | count(*) | +----------+ | 247489 | +----------+ 1 row in set (2.25 sec) Thursday, 30 September 2010
  • 53. Partitioning by date - RIGHT PARTITION BY RANGE(year(from_date)) select count(*) from salaries where from_date between '1998-01-01' and '1998-12-31'; +----------+ | count(*) | +----------+ | 247489 | +----------+ 1 row in set (0.46 sec) Thursday, 30 September 2010
  • 54. Partitioning by date - EXPLAIN explain partitions select count(*) from salaries where year(from_date) = 1998G ***** 1. row **** id: 1 select_type: SIMPLE table: salaries partitions: p01,p02,p03,p04,p05,p06,p07,p08,p09,p10,p 11,p12,p13,p14,p15,p16,p17,p18,p19 type: index ... Thursday, 30 September 2010
  • 55. Partitioning by date - EXPLAIN explain partitions select count(*) from salaries where from_date between '1998-01-01' and '1998-12-31'G ***** 1. row **** id: 1 select_type: SIMPLE table: salaries partitions: p14,p15 ... Thursday, 30 September 2010
  • 56. Partitioning by date with different sizes  Mixing partitions by year, month, day in the same table  HOW TO:  Use the "TO_DAYS" function  set appropriate intervals Thursday, 30 September 2010
  • 57. Partitioning by date with different sizes ALTER TABLE salaries partition by range (to_days(from_date)) ( # 5 years partition p01 values less than (to_days('1985-01-01')), partition p06 values less than (to_days('1990-01-01')), # 1 year partition p11 values less than (to_days('1995-01-01')), partition p12 values less than (to_days('1996-01-01')), partition p13 values less than (to_days('1997-01-01')), partition p14 values less than (to_days('1998-01-01')), partition p15 values less than (to_days('1999-01-01')), partition p16 values less than (to_days('2000-01-01')), Thursday, 30 September 2010
  • 58. Partitioning by date with different sizes ALTER TABLE salaries partition by range (to_days(from_date)) ( # 1 month partition p17 values less than (to_days('2001-01-01')), partition p18 values less than (to_days('2001-02-01')), partition p19 values less than (to_days('2001-03-01')), partition p20 values less than (to_days('2001-04-01')), partition p21 values less than (to_days('2001-05-01')), partition p22 values less than (to_days('2001-06-01')), partition p23 values less than (to_days('2001-07-01')), partition p24 values less than (to_days('2001-08-01')), partition p25 values less than (to_days('2001-09-01')), partition p26 values less than (to_days('2001-10-01')), partition p27 values less than (to_days('2001-11-01')), partition p28 values less than (to_days('2001-12-01')), Thursday, 30 September 2010
  • 59. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 60. Hands on - Partitioning with MyISAM  Primary key matters - not always needed  Size of indexes matters Thursday, 30 September 2010
  • 62. How many partitions from information_schema.partitions +-------+-----------------+----------+ | pname | expr | descr | +-------+-----------------+----------+ | p01 | year(from_date) | 1985 | | p02 | year(from_date) | 1986 | ... | p13 | year(from_date) | 1997 | | p14 | year(from_date) | 1998 | | p15 | year(from_date) | 1999 | | p16 | year(from_date) | 2000 | ... | p19 | year(from_date) | MAXVALUE | +-------+-----------------+----------+ Thursday, 30 September 2010
  • 63. How many records select count(*) from salaries; +----------+ | count(*) | +----------+ | 2844047 | +----------+ Thursday, 30 September 2010
  • 64. How many records in 1998 not partitioned select count(*) from salaries where from_date between '1998-01-01' and '1998-12-31'; +----------+ | count(*) | +----------+ | 247489 | +----------+ 1 row in set (1.52 sec) # NOT PARTITIONED Thursday, 30 September 2010
  • 65. How many records in 1998 partitioned select count(*) from salaries where from_date between '1998-01-01' and '1998-12-31'; +----------+ | count(*) | +----------+ | 247489 | +----------+ 1 row in set (0.41 sec) # partition p15 Thursday, 30 September 2010
  • 66. Deleting records in 1998 NOT partitioned delete from salaries where from_date between '1998-01-01' and '1998-12-31'; Query OK, 247489 rows affected (19.13 sec) # NOT PARTITIONED Thursday, 30 September 2010
  • 67. Deleting records in 1998 partitioned alter table salaries drop partition p15; Query OK, 0 rows affected (1.35 sec) Thursday, 30 September 2010
  • 68. Partitions with InnoDB  Slower than MyISAM  But more robust  Requires more storage Thursday, 30 September 2010
  • 69. Partitioning with InnoDB - File per table CREATE TABLE table_name ( ... ... ) ENGINE = INNODB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8 PARTITION BY XXXX Thursday, 30 September 2010
  • 70. Partitions with InnoDB (laptop)  Key points:  Takes much more storage than other engines engine storage (MB) innodb 221 myisam 181 archive 74 innodb partitioned (whole) 289 innodb partitioned (file per table) 290 myisam partitioned 182 archive partitioned 72 Thursday, 30 September 2010
  • 71. Benchmarking results (laptop) engine query year query year 2000 2002 InnoDB 1.25 1.25 MyISAM 1.72 1.73 Archive 2.47 2.45 InnoDB partitioned 0.24 0.10 whole InnoDB Partitioned 0.45 0.10 (file per table) MyISAM partitioned 0.18 0.12 Archive partitioned 0.22 0.12 Thursday, 30 September 2010
  • 72. Partitions with InnoDB (huge server)  Key points:  Takes much more storage than other engines engine storage (GB) innodb (with PK) 330 myisam (with PK) 141 archive 13 innodb partitioned (no PK) 237 myisam partitioned (no PK) 107 archive partitioned 13 Thursday, 30 September 2010
  • 73. Benchmarking results (huge server) engine 6 month range InnoDB 4 min 30s MyISAM 25.03s Archive 22 min 25s InnoDB partitioned by month 13.19 MyISAM partitioned by year 6.31 MyISAM partitioned by month 4.45 Archive partitioned by year 16.67 Archive partitioned by month 8.97 Thursday, 30 September 2010
  • 74. Partitions with Archive  Key points:  REMOVE INDEXES (not supported by archive)  For pure statistics applications, ARCHIVE can be ALMOST AS FAST AS MyISAM! Thursday, 30 September 2010
  • 75. Partitions with ARCHIVE - in practice CREATE TABLE titles ( emp_no INT NOT NULL, title VARCHAR(50) NOT NULL, from_date DATE NOT NULL, to_date DATE, KEY (emp_no), FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE, PRIMARY KEY (emp_no,title, from_date) )ENGINE = InnoDB; Thursday, 30 September 2010
  • 76. Partitions with ARCHIVE - in practice CREATE TABLE titles ( emp_no INT NOT NULL, title VARCHAR(50) NOT NULL, from_date DATE NOT NULL, to_date DATE #, # KEY (emp_nono), # FOREIGN KEY (emp_no) # REFERENCES employees # (emp_no) ON DELETE CASCADE, # PRIMARY KEY (emp_no,title, # from_date) ) ENGINE = ARCHIVE; Thursday, 30 September 2010
  • 77. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 78. TOOLS  The INFORMATION_SCHEMA.PARTITIONS table  The partition helper  http://datacharmer.blogspot.com/2008/12/partition- helper-improving-usability.html  A Perl script that creates partitioning statements  The mysqldump_partition_backup Thursday, 30 September 2010
  • 79. INFORMATION SCHEMA PARTITIONS table | TABLE_NAME | | PARTITION_NAME | | SUBPARTITION_NAME | | PARTITION_ORDINAL_POSITION | | SUBPARTITION_ORDINAL_POSITION | | PARTITION_METHOD | | SUBPARTITION_METHOD | | PARTITION_EXPRESSION | | SUBPARTITION_EXPRESSION | | PARTITION_DESCRIPTION | | TABLE_ROWS | | PARTITION_COMMENT | Thursday, 30 September 2010
  • 80. INFORMATION SCHEMA PARTITIONS table select * from partitions where table_name='salaries' and table_schema='employees' limit 1G TABLE_SCHEMA: employees TABLE_NAME: salaries PARTITION_NAME: p01 SUBPARTITION_NAME: NULL PARTITION_ORDINAL_POSITION: 1 SUBPARTITION_ORDINAL_POSITION: NULL PARTITION_METHOD: RANGE COLUMNS SUBPARTITION_METHOD: NULL PARTITION_EXPRESSION: from_date SUBPARTITION_EXPRESSION: NULL PARTITION_DESCRIPTION: '1985-12-31' TABLE_ROWS: 18238 Thursday, 30 September 2010
  • 81. INFORMATION SCHEMA PARTITIONS table select partition_name,partition_description, table_rows from partitions where table_name='salaries' and table_schema='employees'; +----------------+-----------------------+------------+ | partition_name | partition_description | table_rows | +----------------+-----------------------+------------+ | p01 | '1985-12-31' | 18238 | | p02 | '1986-12-31' | 37915 | | p03 | '1987-12-31' | 57395 | | p04 | '1988-12-31' | 76840 | | p05 | '1989-12-31' | 95890 | | p06 | '1990-12-31' | 114520 | | p07 | '1991-12-31' | 132578 | | p08 | '1992-12-31' | 151019 | | p09 | '1993-12-31' | 168103 | | p10 | '1994-12-31' | 185121 | | p11 | '1995-12-31' | 201576 | | p12 | '1996-12-31' | 218244 | | p13 | '1997-12-31' | 233144 | | p14 | '1998-12-31' | 247458 | Thursday, 30 September 2010
  • 82. The Partition helper  Introduction, syntax, and examples  http://forge.mysql.com/tools/tool.php?id=173 Thursday, 30 September 2010
  • 83. The partition helper $ partition_helper --help The Partition Helper, version 1.0.1 This program creates a ALTER TABLE statement to add or reorganize partitions for MySQL 5.1 or later (C) 2008 Giuseppe Maxia syntax: partitions_helper [options] -t --table = name -c --column = name -i --interval = name "year", "month", or a number -p --partitions = number ... Thursday, 30 September 2010
  • 84. The partition helper ... --first_partition = number --reorganize = name -s --start = name -e --end = name -f --function = name -l --list --prefix = name --explain Thursday, 30 September 2010
  • 85. The partition helper example partitions_helper --table=t1 --column=mydate --interval=year --start=1998-01-01 --end=2002-12-01 # partitions: 5 ALTER TABLE t1 PARTITION by range (to_days(mydate)) ( partition p001 VALUES LESS THAN (to_days('1998-01-01')) , partition p002 VALUES LESS THAN (to_days('1999-01-01')) , partition p003 VALUES LESS THAN (to_days('2000-01-01')) , partition p004 VALUES LESS THAN (to_days('2001-01-01')) , partition p005 VALUES LESS THAN (to_days('2002-01-01')) ); Thursday, 30 September 2010
  • 86. The partition helper example partitions_helper --table=t1 --column=mydate --interval=month --start=1998-01-01 --end=2002-12-01 # partitions: 60 ALTER TABLE t1 PARTITION by range (to_days(mydate)) ( partition p001 VALUES LESS THAN (to_days('1998-01-01')) , partition p002 VALUES LESS THAN (to_days('1998-02-01')) , partition p003 VALUES LESS THAN (to_days('1998-03-01')) #[...] , partition p058 VALUES LESS THAN (to_days('2002-10-01')) , partition p059 VALUES LESS THAN (to_days('2002-11-01')) , partition p060 VALUES LESS THAN (to_days('2002-12-01')) ); Thursday, 30 September 2010
  • 87. The partition helper example partitions_helper --table=t1 --column=mydate --interval=week --start=1998-01-01 --end=2002-12-01 # partitions: 255 ALTER TABLE t1 PARTITION by range (to_days(mydate)) ( partition p001 VALUES LESS THAN (to_days('1998-01-01')) , partition p002 VALUES LESS THAN (to_days('1998-01-08')) , partition p003 VALUES LESS THAN (to_days('1998-01-15')) #[...] , partition p253 VALUES LESS THAN (to_days('2002-10-31')) , partition p254 VALUES LESS THAN (to_days('2002-11-07')) , partition p255 VALUES LESS THAN (to_days('2002-11-14')) ); Thursday, 30 September 2010
  • 88. The partition helper example partitions_helper --table=t1 --column=mynum --interval=250 --start=1000 --end=3000 # partitions: 8 ALTER TABLE t1 PARTITION by range (mydate) ( partition p001 VALUES LESS THAN (1250) , partition p002 VALUES LESS THAN (1500) , partition p003 VALUES LESS THAN (1750) , partition p004 VALUES LESS THAN (2000) , partition p005 VALUES LESS THAN (2250) , partition p006 VALUES LESS THAN (2500) , partition p007 VALUES LESS THAN (2750) , partition p008 VALUES LESS THAN (3000) ); Thursday, 30 September 2010
  • 89. Partitions maintenance through events  An article by Greg Haase about this topic  http://dev.mysql.com/tech-resources/articles/ partitioning-event_scheduler.html Thursday, 30 September 2010
  • 90. mysqldump_partition_backup  a script created by Roland Bouman  http://forge.mysql.com/tools/tool.php?id=258  detects the partitions for a table, and generates mysqldump statements for each one Thursday, 30 September 2010
  • 91. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 92. the NULL partition • when partitioning by date • using TO_DAYS • the first partition matters partition rows p1 100,000 p2 150,000 p3 120,000 p4 100,000 92 Thursday, 30 September 2010
  • 93. partition condition rows p1 to_days('1990-01-01') 1,000,000 p2 to_days('1990-02-01') 1,000,000 p3 to_days('1990-03-01') 1,000,000 p4 to_days('1990-04-01') 1,000,000 p5 to_days('1990-05-01') 1,000,000 p6 to_days('1990-06-01') 1,000,000 Thursday, 30 September 2010
  • 94. date_field between '1990-04-10' and '1990-04-15' partition condition rows p1 to_days('1990-01-01') 1,000,000 p2 to_days('1990-02-01') 1,000,000 p3 to_days('1990-03-01') 1,000,000 p4 to_days('1990-04-01') 1,000,000 p5 to_days('1990-05-01') 1,000,000 p6 to_days('1990-06-01') 1,000,000 = 2,000,000 rows Thursday, 30 September 2010
  • 95. alter table t1 reorganize partition p1 into ( partition p0 values less than (0), partition p1 values less than (to_days('1990-01-01')) ); partition condition rows p0 0 0 p1 to_days('1990-01-01') 1,000,000 p2 to_days('1990-02-01') 1,000,000 p3 to_days('1990-03-01') 1,000,000 p4 to_days('1990-04-01') 1,000,000 p5 to_days('1990-05-01') 1,000,000 p6 to_days('1990-06-01') 1,000,000 Thursday, 30 September 2010
  • 96. date_field between '1990-04-10' and '1990-04-15' partition condition rows p0 0 0 p1 to_days('1990-01-01') 1,000,000 p2 to_days('1990-02-01') 1,000,000 p3 to_days('1990-03-01') 1,000,000 p4 to_days('1990-04-01') 1,000,000 p5 to_days('1990-05-01') 1,000,000 p6 to_days('1990-06-01') 1,000,000 = 1,000,000 rows Thursday, 30 September 2010
  • 97. Lock before inserting single records • If you send MANY SINGLE INSERT at once • LOCK before inserting • No need to do it for bulk inserts 97 Thursday, 30 September 2010
  • 98. Your needs What Partition pruning Syntax Table of contents Benchmarking Partitioning by date hands on tools tips leveraging replication Thursday, 30 September 2010
  • 99. Replication schemes INNODB INNODB NOT NOT MASTER PARTITIONED PARTITIONED concurrent insert SLAVE concurrent read INNODB MyISAM PARTITIONED PARTITIONED BY RANGE BY RANGE SLAVE SLAVE 99 concurrent batch processing large batch processing Thursday, 30 September 2010
  • 100. Replication schemes INNODB concurrent insert PARTITIONED BY HASH MASTER INNODB NON MyISAM PARTITIONED PARTITIONED SLAVE BY RANGE SLAVE 100 batch processing concurrent reads Thursday, 30 September 2010
  • 101. Replication schemes - dimensions INNODB ARCHIVE PARTITIONED PARTITIONED BY HASH SLAVE BY RANGE MASTER (locations) concurrent insert dimensional processing ARCHIVE ARCHIVE PARTITIONED SLAVE SLAVE PARTITIONED BY RANGE BY RANGE (date) (product) 101 dimensional processing dimensional processing Thursday, 30 September 2010
  • 102. Slides for this presentation http://tinyurl.com/mysql-partitions This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. Thursday, 30 September 2010
  • 103. THANKS! Q&A Comment on Twitter: @datacharmer Thursday, 30 September 2010
  • 104. BONUS SLIDES NEW FEATURES 104 Thursday, 30 September 2010
  • 105. MySQL 5.5 enhancements • PARTITION BY RANGE COLUMNS • PARTITION BY LIST COLUMNS • TO_SECONDS 105 Thursday, 30 September 2010
  • 106. R E MySQL 5.5 enhancements CREATE TABLE t ( FO dt date B E 5.1 ) PARTITION BY RANGE (TO_DAYS(dt)) ( PARTITION p01 VALUES LESS THAN (TO_DAYS('2007-01-01')), PARTITION p02 VALUES LESS THAN (TO_DAYS('2008-01-01')), PARTITION p03 VALUES LESS THAN (TO_DAYS('2009-01-01')), PARTITION p04 VALUES LESS THAN (MAXVALUE)); 106 Thursday, 30 September 2010
  • 107. R E MySQL 5.5 enhancements FO SHOW CREATE TABLE t G B E 5.1 Table: t Create Table: CREATE TABLE `t` ( `dt` date DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 /*!50100 PARTITION BY RANGE (TO_DAYS (dt)) (PARTITION p01 VALUES LESS THAN (733042) ENGINE = MyISAM, […] 107 Thursday, 30 September 2010
  • 108. MySQL 5.5 enhancements E R CREATE TABLE t ( FT dt date A 5.5 ) PARTITION BY RANGE COLUMNS (dt) ( PARTITION p01 VALUES LESS THAN ('2007-01-01'), PARTITION p02 VALUES LESS THAN ('2008-01-01'), PARTITION p03 VALUES LESS THAN ('2009-01-01'), PARTITION p04 VALUES LESS THAN (MAXVALUE)); 108 Thursday, 30 September 2010
  • 109. MySQL 5.5 enhancements E R FT SHOW CREATE TABLE t Table: t A 5.5 Create Table: CREATE TABLE `t` ( `dt` date DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 /*!50500 PARTITION BY RANGE COLUMNS (dt) (PARTITION p01 VALUES LESS THAN ('2007-01-01') ENGINE = MyISAM, […] 109 Thursday, 30 September 2010
  • 110. MySQL 5.5 - Multiple columns CREATE TABLE t ( a int, b int )PARTITION BY RANGE COLUMNS (a,b) ( PARTITION p01 VALUES LESS THAN (10,1), PARTITION p02 VALUES LESS THAN (10,10), PARTITION p03 VALUES LESS THAN (10,20), PARTITION p04 VALUES LESS THAN (MAXVALUE, MAXVALUE)); 110 Thursday, 30 September 2010
  • 111. partition definition by range (a,b) records partition LESS THAN a b p01 10 10 1 10 p02 10 20 10 9 p03 10 30 10 10 p04 10 MAXVALUE 10 11 p05 MAXVALUE MAXVALUE Thursday, 30 September 2010
  • 112. partition definition by range (a,b) records partition LESS THAN a b p01 10 10 1 10 p02 10 20 10 9 p03 10 30 10 10 p04 10 MAXVALUE 10 11 p05 MAXVALUE MAXVALUE (1,10) < (10,10) ? (a < 10) OR ((a = 10) AND (b < 10)) (1 < 10) OR TRUE ((1 = 10) AND (10 < 10)) Thursday, 30 September 2010
  • 113. partition definition by range (a,b) records partition LESS THAN a b p01 10 10 1 10 p02 10 20 10 9 p03 10 30 10 10 p04 10 MAXVALUE 10 11 p05 MAXVALUE MAXVALUE (10,9) < (10,10) ? (a < 10) OR ((a = 10) AND (b < 10)) (10 < 10) OR TRUE ((10 = 10) AND (9 < 10)) Thursday, 30 September 2010
  • 114. partition definition by range (a,b) records partition LESS THAN a b p01 10 10 1 10 p02 10 20 10 9 p03 10 30 10 10 p04 10 MAXVALUE 10 11 p05 MAXVALUE MAXVALUE (10,10) < (10,10) ? (a < 10) OR ((a = 10) AND (b < 10)) (10 < 10) OR FALSE ((10 = 10) AND (10 < 10)) Thursday, 30 September 2010
  • 115. partition definition by range (a,b) records partition LESS THAN a b p01 10 10 1 10 p02 10 20 10 9 p03 10 30 10 10 p04 10 MAXVALUE 10 11 p05 MAXVALUE MAXVALUE (10,10) < (10,20) ? (a < 10) OR ((a = 10) AND (b < 20)) (10 < 10) OR TRUE ((10 = 10) AND (10 < 20)) Thursday, 30 September 2010
  • 116. CREATE TABLE employees ( emp_no int(11) NOT NULL, birth_date date NOT NULL, first_name varchar(14) NOT NULL, last_name varchar(16) NOT NULL, gender char(1) DEFAULT NULL, hire_date date NOT NULL ) ENGINE=MyISAM PARTITION BY RANGE COLUMNS(gender,hire_date) (PARTITION p01 VALUES LESS THAN ('F','1990-01-01'), PARTITION p02 VALUES LESS THAN ('F','2000-01-01'), PARTITION p03 VALUES LESS THAN ('F',MAXVALUE), PARTITION p04 VALUES LESS THAN ('M','1990-01-01'), PARTITION p05 VALUES LESS THAN ('M','2000-01-01'), PARTITION p06 VALUES LESS THAN ('M',MAXVALUE), PARTITION p07 VALUES LESS THAN (MAXVALUE,MAXVALUE) 116 Thursday, 30 September 2010
  • 117. MySQL 5.5 enhancements • TRUNCATE PARTITION • TO_SECONDS() 117 Thursday, 30 September 2010
  • 118. MySQL 5.6 enhancements • ALTER TABLE … EXCHANGE PARTITION 118 Thursday, 30 September 2010
  • 119. ALTER TABLE t1 EXCHANGE PARTITION p2 WITH TABLE t2 Thursday, 30 September 2010
  • 120. t1 t2 f1 f2 f3 f1 f2 f3 1 a xxx (empty) p1 2 b xxx 3 c yyy 4 d xxx 5 e zzz p2 6 f www 7 g ccc 8 h sss 9 i www p3 10 j xxx 11 k yyy 12 l zzz Thursday, 30 September 2010
  • 121. t1 f1 f2 f3 t2 1 a xxx f1 f2 f3 p1 2 b xxx (empty) 3 c yyy 4 d xxx 5 e zzz p2 6 f www 7 g ccc 8 h sss 9 i www p3 10 j xxx 11 k yyy 12 l zzz Thursday, 30 September 2010
  • 122. t1 t2 f1 f2 f3 f1 f2 f3 1 a xxx 5 e zzz p1 2 b xxx 6 f www 7 g ccc 3 c yyy 8 h sss 4 d xxx p2 (empty) 9 i www p3 10 j xxx 11 k yyy 12 l zzz Thursday, 30 September 2010