MySQL 按日期分表

來源:互聯網
上載者:User

標籤:

一、表不存在時則建立

  之前做項目實在是太趕了,很多東西都沒記錄。是時候補回來了

  MySQL做一個大表,由於要存記錄,所以資料量很大,查詢很慢。恰好查詢的時候,又不需要時間太久的冷資料。現在將其實現原理提取成一個控制台小程式。

  首先,建立一個簡單的資料庫訪問類。

    public static class CommonDao    {        private static MySqlConnection conn = new MySqlConnection(ConfigurationManager.AppSettings["DB"]);        //建立串連        /// <summary>        /// Insert Update Delete語句的執行        /// </summary>        /// <param name="SQL">SQL語句</param>        /// <returns>返回影響行數</returns>        public static int ExecuteNonQuerySQL(string SQL)        {            MySqlCommand cmd = conn.CreateCommand();            cmd.CommandText = SQL;            if (conn.State != ConnectionState.Open)            {                conn.Open();            }            int Count = cmd.ExecuteNonQuery();            conn.Close();            return Count;        }    }

  由於是按分鐘分表,所以寫個定時器,每一分鐘往裡面插入5條資料:

        static void Main(string[] args)        {            //每分鐘執行一次,插入5條資料            Timer t = new Timer(60000);            t.Enabled = true;       //到達時間就執行一次,或者是持續執行            t.AutoReset = true;     //一直執行            t.Elapsed += (sender, eea) =>            {                for (int i = 0; i < 5; i++)                {                    DateTime dt = DateTime.Now;                    string TableName = "person" + dt.ToString("mm");                    string sql = "INSERT INTO " + TableName + " VALUES(null,‘superman‘,‘" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "‘)";                    try                    {                        CommonDao.ExecuteNonQuerySQL(sql);                    }                    catch (Exception e)                    {                        MySqlException ex = e as MySqlException;                        //1146的代號就是表不存在的錯誤                        if (ex.Number == 1146)                        {                            //如果表不存在,則先建立這個表                            string SQLCreateTable = "CREATE TABLE " + TableName + " LIKE person";  //從模板表來建立新表,這樣的好處是索引、自增什麼的可以一次性建好                            CommonDao.ExecuteNonQuerySQL(SQLCreateTable);                        }                    }                }            };            Console.ReadKey();        }

  第一張Person模板表的格式如下:

CREATE TABLE `person` (  `Id` int(11) NOT NULL AUTO_INCREMENT,  `Name` varchar(255) DEFAULT NULL,  `OperateTime` datetime DEFAULT NULL,  PRIMARY KEY (`Id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  

 

  於是就每分鐘都會建立表並添加資料。

  

二、分表後跨表查詢的問題

  分表後有很多問題要處理,簡單做兩個,maybe會有其他問題。

  1、資料在哪個表裡;

  2、資料跨表了,那麼就需要表串連;

 

MySQL 按日期分表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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