Why divide tables and sub-Libraries:
When our data table data volume, access is very large, or is used frequently, a data table can not withstand such a large data access and storage, so in order to reduce the burden of the database, to speed up the storage of data, you need to divide a table into multiple sheets, and a class of data into different tables, When the sub-table has not been able to meet the requirements, we can also divide the library, and with a few database storage.
The table will have different implementation methods depending on the requirements and functions, and here is an example of what I do in the project:
requirements: Product,product_price Two sheet is a one-to-many relationship, and the price of products and products per day, a product corresponding to several prices, and now because the product table data is very large, more than millions data per day, on the day for the table to be divided into tables, The database is divided in months, and the table is named ' product_price2014-07-20 ' table format: The first is the original number according to the library table name, followed by the date (year-month-day).
The code for creating the database and table is as follows:
functionget_product_price_table(){$db _info=Array(); //This month$newmot _time=Date("Y-m"); //Today's date$newday _time=Date("Y-m-d"); //Monthly database, today's table$db _name= ' acbooking '.$newmot _time;$table _name= "Product_price".$newday _time; //Yesterday's date$yesday _time=Date("Y-m-d",Strtotime("-1 day")); //Remove yesterday's data, get the last ID of Product_price, as the starting value for the new table ID$last _one_product_price=Get_info(' product_price_table_id ',Array(' Time '=$yesday _time)); if($last _one_product_price[' Table_id_end ']>0){$table _id=$last _one_product_price[' Table_id_end ']+1; }Else{$table _id= 1; } //Create a database
$Db _string_line = C (' Db_type '). ':/ /‘. C (' Db_user '). ': '. C (' db_pwd '). ' @‘. C (' Db_host '). ' /'. $new _db_database; $DB _p=c (' Db_prefix '); $Model =m ($table, $DB _p, $Db _string_line); $ db_name= $Model->execute ($sql) ;//The new database is created with the SQL statement configuration, and the SQL statement is omitted here;
//create datasheet
$Db _ String_line = C (' Db_type '). ':/ /‘. C (' Db_user '). ': '. C (' db_pwd '). ' @‘. C (' Db_host '). ' /'. $new _db_database; $DB _p=c (' Db_prefix '); $Model =m ($table, $DB _p, $Db _string_line); $ table_name = $Model->execute ($sql) ;
//Return to new database, new table $db _info[' database ']=$db _name; $db _info[' table ']=$table _name; return $db _info; }
After you create a table, you can store all the data for the day, change a table every day, and perform efficiently ....