Conclusions on the database

1.mysql engine choice

1. Check the current database supported type
Show Engine
2. lookup table engine
Show Status Table from DB1;
. 3 query engine specific table
show table status from db1 where name = 'student';

After the default mysql innodb 1.5

create dastabase if not exists db1 character set utf8;

Table IF EXISTS Student Not Create (
ID int Primary Key anto_increment, name VARCHAR (20 is)) = Engine MyISAM;
modifying table storage engine
alter table student engine = innodb;

Myisam: does not support transactions / foreign key does not support the / support full-text search and table-level locking, blocking each other alone to write, fast read speed, saving resources, so if the application is based query and insert operations, only a few updates and delete operations, and the integrity of the transaction, concurrency requirements are not very high, so the selection of qualified storage engine is very appropriate.
innodb: mysql is the default storage engine, due to the innodb support services / support foreign keys / row-level locking, support for all secondary indexes (not support full-text search after 5.5.5), high cached, so for the integrity of the very things high demands to concurrent data consistency conditions, frequent read and write operations, the storage engine innodb is more appropriate choice, such as BBS, billing systems, etc. recharge transfer.
memory: all data stored in ram, in the need to quickly locate records and other similar data environment, can provide faster access to memory defects that limit the size of the table, the table can not be too large in-memory cache Second, we must ensure that the data table can be restored, the data in the table after abnormal termination of the database can be restored, memory table is usually updated less frequently for a small table to quickly get access to the results.

2. Index

Classification index:
according to functional classification:
ordinary index: the index most basic, he did not have any restrictions.
The only index: the index column value must be unique, but allow free value, if it is a composite index, then the column value combination must be unique.
Primary key index: a special unique index, does not allow null values, while the general index is created when construction of the table.
Composite index: a single index combined.
Foreign key indexes: Only innodb engine supports foreign key index, used to ensure data consistency, integrity and cascading operation.
Full-text index: Quick match all the way documents. innodb engine only supports full-text index after 5.6, memory engine does not support.

According to structural classification:
b + Tree index: mysql most frequently used index data structure is myisam innodb storage engine and default index.
Hash indexes: mysql in Memory storage engine the default index;

Index-related operations
query index:
Show index from studnet;

No index query:
when there is no index query, the query would be least efficient data one by one

Add the name of the general index
create index idx_name on student (name) ;
add a unique index
the Create UNIQUE index idx_name ON Student (name);
(generally do not, only the primary key id)

alter table student add index idx_name(name);

Tree B
B + Tree non-leaf nodes store only key values

create table if not exists user(
name varchar(10),
address varchar(10),
phone varchar(10));

Adding joint index

alter table user add index idx_three(name,address,phone);

Leftmost match

sql must have a name

Characteristics of the transaction

actomic consistency isolation durability

Lock Category

Operating Table Categories:
shared locks:
exclusive locks:

By size category:
table-level lock:
row-level locking:
industry-level locking:
(MyISAM supports table-level locking, innodb support table-level locking, row-level locking)

Classification by use:
pessimistic locking:
optimistic lock:

开启事务
start transaction;
查询事务,并加锁
(共享锁,其他事务可以查询,不能改
select * from student where id=1 lock in share mode;
//不带索引则锁整张表
select * from student where name='小刘' lock in mode;
//排他锁
select * from student where id=1 for update;
释放锁
commit

myisam does not support transactions, direct lock

//加锁
lock tables student read;
select * from student where id=1;
//解锁
unlock tables;

innodb performance optimization recommendations
1. Try to complete the data query by columns indexed, so as to avoid innodb not add row lock table and plus lock
2. Reasonable design an index, to be as accurate as possible, to narrow as much as possible, to avoid unnecessary It locks.
3. minimize data retrieval condition based on the range of
4 to control the size of the transaction as much as possible to reduce the amount of resources locked and locking the length of time.
5. In the same transaction, as do all the resources once the lock needed to reduce the probability of deadlock
6. For very prone to deadlock part of the business, you can try to upgrade the lock granularity, through the table level locks to reduce the generation of deadlock.

Published 25 original articles · won praise 1 · views 783

Guess you like

Origin blog.csdn.net/qq_42770949/article/details/104458537