Mysql Optimization primary

Mysql table replication

What is the mysql table copy?
Copy the mysql table. Structure including table data, index, default values, etc.


Copy Table Structure

	create table tablename_1 like tablename ;
 
解析:
			tablename -->  被复制的表
			tablename_1  -->  复制的表

Copy the table data

	create table tablename_1 select * from tablename ;

Copy table structure and data

	create table tablename_1 like tablename ;
	insert into tablename_1  select * from tablename ;

index

What is the index?
The index is a way to help Mysql efficient data acquisition of the data structure

What index

  1. Frequently as a query field
  2. Associated with the other tables in the query field

What is not indexed

  1. Frequent change of additions and deletions table
  2. Less data table
  3. The average data duplication and distribution of table fields (not repeat too much significance index)

Index Classification

  1. Primary key index

  2. The only index

    The value of the index columns must be unique, but allow free value. If it is a combination of the index, the column value must be unique.

  3. General index

    This is the basic index, without any restrictions.

  4. Full-text index

Add index

	语法:alter table tablename add 索引类型 索引名称(字段名)
	
	alter table tablename add index index_name(name); 
	主键索引一般建表时都会设置为id
	alter table tablename add primary key(name);
	alter table tablename add unique unique_name(name);

Delete Index

	语法: alter table tablename drop index 索引名称
	
	
	如果主键带有自增长,删除主键索引前必须取消自增长
	假设自增长字段为id
	alter table tablename change id id int;
	alter table tablename drop primary key;

View Index

	show index from tablename

Mysql views

What is a view?
Official explanation: present view is a virtual table, a logical table itself does not contain data. As a select statement stored in the data dictionary.
Folk explanation: a shortcut to execute defined queries (for reference only)

Advantage views

  1. simple
  2. Safety
  3. Independent data

Create a view

	create view viewname as sql语句;
	
示范代码:
	create view sel_user as select * from user;
	
	show tables;
	
	select * from sel_user;	

Delete View

	drop view viewname;

Query View

	show tables;

Pretreatment

What is pre-mysql?
Personal interpretation: sql statement is executed repeatedly in a dynamic part with placeholders instead of (?)

Advantage pretreatment

  1. Safety
  2. Improve efficiency (reuse)

grammar

创建预处理语句
	prepare prepare_name from 'sql语句';

删除预处理语句
	drop prepare prepare_name;

如何调用预处理语句?
	设置一个变量	set @i = 3;
	execute prepare_name using @i;

示范代码:
		prepare sel_id_name from 'select * from user where id = ?'; 
		set @i=3;
		EXECUTE sel_id_name using @i;

Affairs

Use affairs premise: Table engine must innodb

What is a transaction

The official explanation: a series of operations performed by a single logical unit of work, either fully executed or not executed completely.

Four characteristics

Atomicity

All operations in a transaction, either completed or not completed all, does not end in the middle of a link. Transaction error occurs during execution, it will be rolled back to the state before the start of the transaction, as the transaction never performed the same.

Consistency (Stability)

And after the end of the transaction before the transaction began, the integrity of the database is not corrupted. Data is written to be in full compliance with preset rules, otherwise the transaction withdraw .

Isolation

Multiple concurrent transactions simultaneously read and write and revise their data, if the result of a transaction affecting other matters, other matters withdrawn.

Persistent (reliability)

After the transaction, to modify the data is permanent, even if the system failure will not be lost. (Innodb table-driven reconstruction will make use of the log file modification)

Keyword

  1. Submit commit
  2. Rollback rollback

Use commit and rollbacj start and end work


Knowledge supplement

View table structure

	desc tablename;

Gets create a data table statement

	show create table tablename;
	
	结果:
			CREATE TABLE `t1` (
 				 `id` int(12) unsigned NOT NULL AUTO_INCREMENT,
				  `name` char(15) DEFAULT NULL,
				  `sex` char(1) DEFAULT NULL,
				  `age` int(5) DEFAULT NULL,
				  PRIMARY KEY (`id`)
			) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4

How to modify table structure

	alter table tablename change 原字段名 新字段名 数据类型 [primary key | default | not null | ...];

Blog from Yang Shuai Shuai
reprint please indicate the source of pirated reserved

Guess you like

Origin blog.csdn.net/KinCae/article/details/90676314