简体   繁体   中英

Fragmenting a huge table

I currently am working on some project that insert a lot of data into some tables. To ensure that my system is fast enough, I want to fragment my huge table into some smaller tables representing the months data. I have an idea of how it will work, but I still need some more informations.

The primary keys of my tables must be continuous so I thought of an architecture that would look like this:

CREATE TABLE `foo` (
    `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
}

CREATE TABLE `foo012014` (
    `id` bigint(11),
    `description` varchar(255),
}

CREATE TABLE `foo022014` (
    `id` bigint(11),
    `description` varchar(255),
}

On every insertion, the PHP page will look if a table already exists for the month and if not will create it.

The thing is, how do I get to bind the "foo" child table primary key to the "foo" mother table? Plus, is this design a bad practice or is it good?

It's not a good pratice, and difficult your queries.

With just the id you already have an index, which allows for better indexing of your data.

If your queries are also nicely written and organized, the time to execute a query in your database will be relatively small with 1 million rows or 20.

Solutions

First

For a better maintenance I recommend the following:

  • Add a new field in your table food : created datetime DEFAULT CURRENT_TIMESTAMP (works in MySQL 5.6+, for other versions, or set manually in every insert, or change to timestamp )

And, just use this field for group your data basead in datetime values, like that: 2014-01-24 13:18 .

It's easy to select and manipulate.

Second

Create a external table with month and year , like that:

drop table if exists foo_periods;
create table foo_periods (
  id int not null auto_increment primary key,
  month smallint(4) not null,
  year smallint(4) not null,
  created datetime,
  modified datetime,
  active boolean not null default 1,
  index foo_periods_month (month),
  index foo_periods_year (year)
);

You can change smallint in month to varchar if you feels better.

Then, just create a FK , and done!

ALTER TABLE foo 
  ADD COLUMN foo_period_id int not null;

ALTER TABLE foo
    ADD CONSTRAINT foo_foo_period_id
    FOREIGN KEY (foo_period_id)
    REFERENCES foo_periods (id);

References

If you want read more about fragmentation / optimization in MySQL, this is a great post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM