How to partition MySQL table by day?

前端 未结 2 1136
天命终不由人
天命终不由人 2021-01-22 00:45

I\'m running MySQL 5.1 and storing data from web logs into a table. There is a datetime column which I want to partition by day. Every night I add new data from the previous day

相关标签:
2条回答
  • 2021-01-22 01:19

    I tried this once. I ended up creating a cron job to do the partitioning on a regular basis (once a month). Keep in mind that you have a maximum of 1024 partitions per table (http://dev.mysql.com/doc/refman/5.1/en/partitioning-limitations.html).

    Offhand, I probably wouldn't recommend it. For my needs, I saw this created a significant slowdown in any searches that that required cross-partition results.

    Based on your updated explanation, I would first recommend to create the necessary indexes. I would read MySQL Optimization chapter (in specific the section on indexes), to better learn how to ensure you have the necessary indexes. You can also use the slow_query log to help isolate the problematic queries.

    Once you have that narrowed down, I can see your need for partitioning change to wanting to partition to limit the size of a particular partition (perhaps for storage space or for quick truncation, etc). At that point, you may decide to partition on a monthly or annual basis.

    Partitioning using the date as a partition key will obviously force you into creating an index for the date field. Start with that and see how it goes before you get into the extra efforts of partitioning on a scheduled basis.

    0 讨论(0)
  • 2021-01-22 01:20

    Indexes are a must for any table. The details of the index(es) derive from the SELECTs you have; let's see them.

    Rules of thumb:

    • Don't partition a table of less than a million rows
    • Don't use more than about 50 partitions.
    • If you are 'purging old data' after some number of days/weeks/months, see my blog for the code on how to do that.
    • PARTITION BY RANGE() is the only useful partition mechanism.
    0 讨论(0)
提交回复
热议问题