MySQL by date sub-table

Source: Internet
Author: User

one, when the table does not exist, create

Before doing the project is too rush, a lot of things are not recorded. It's time to make it back.

MySQL makes a big table, because to save history, so the amount of data is very large, the query is very slow. It does not take too long for cold data to be queried exactly. Now the implementation principle is extracted into a console applet.

First, create a simple database access class.

     Public Static classCommondao {Private StaticMysqlconnection conn =NewMysqlconnection (configurationmanager.appsettings["DB"]);//Create a connection        /// <summary>        ///execution of the Insert Update DELETE statement/// </summary>        /// <param name= "SQL" >SQL statements</param>        /// <returns>returns the number of affected rows</returns>         Public Static intExecutenonquerysql (stringSQL) {Mysqlcommand cmd=Conn.            CreateCommand (); Cmd.commandtext=SQL; if(Conn. State! =ConnectionState.Open) {conn.            Open (); }            intCount =cmd.            ExecuteNonQuery (); Conn.            Close (); returnCount; }    }

Since it is a minute table, write a timer and insert 5 of data into it every minute:

        Static voidMain (string[] args) {            //executes once per minute, inserting 5 dataTimer T =NewTimer (60000); T.enabled=true;//The arrival time is executed once, or it is continuously executedT.autoreset =true;//always executet.elapsed+ = (sender, EEA) = =            {                 for(inti =0; I <5; i++) {DateTime dt=DateTime.Now; stringTableName =" Person"+ dt. ToString ("mm"); stringsql ="INSERT into"+ TableName +"VALUES (null, ' Superman ', '"+ dt. ToString ("YYYY-MM-DD HH:mm:ss") +"')"; Try{commondao.executenonquerysql (SQL); }                    Catch(Exception e) {mysqlexception ex= E asmysqlexception; //The 1146 code name is the error that the table does not exist.                        if(ex. Number = =1146)                        {                            //If the table does not exist, the table is created first                            stringSqlcreatetable ="CREATE TABLE"+ TableName +" like person";//to create a new table from a Template table, the benefit is that indexing, self-increment, etc. can be built at once .Commondao.executenonquerysql (sqlcreatetable);            }                    }                }            };        Console.readkey (); }

The first Person Template table is formatted as follows:

CREATE TABLE' Person ' (' Id ')int( One) not NULLauto_increment, ' Name 'varchar(255)DEFAULT NULL, ' Operatetime 'datetime DEFAULT NULL,  PRIMARY KEY(' Id ')) ENGINE=InnoDBDEFAULTCHARSET=UTF8;

  

The table is created every minute and the data is added.

  

second, the problem of cross-table query after the sub-table

After the table has a lot of problems to deal with, simply do two, maybe will have other problems.

1, the data in which table;

2, the data across the table, then you need to connect the table;

MySQL by date sub-table

Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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