当前位置:网站首页>Linux server development, SQL statements, indexes, views, stored procedures, triggers

Linux server development, SQL statements, indexes, views, stored procedures, triggers

2022-07-07 07:50:00 Tuen Mun pheasant calls me chicken

Recommend a free open course of zero sound College , Personally, I think the teacher spoke well , Share with you :Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK, Streaming media ,CDN,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Etc , Learn now

1. Architecture

select(listen+1,readfd,NULL,NILL,0);
int clientfd=accept(listenfd,&addr,&len);
my_sql_thread_create(key_thread_one_connection,&id,^connection_atrrib,handle_connection,(void*)channel_info);

MySql Use a select() function , Only listen for read Events ,0 Indicates that the connection is blocked waiting for the connection to arrive . When the supervisor hears that the connection arrives , utilize accept() Accept the connection , Assign a thread to each connection , One fd One thread .redis use reactor Network encapsulation asynchronous mode , Operating memory , The difference is ,MySql In a blocking way , After all, hard disk operation is heavy IO operation .
Why? MySql Use connection pool ? There are three main points , First , Because it's blocked IO, You need to wait for a return to initiate another connection , Waiting time is too long , Affect the efficiency of execution . secondly ,MySql Short connection adopted , If you don't respond for a long time, the connection will be kicked out , Password verification and other operations are complicated , Connection pooling can reduce waiting time . Last ,MySql Can only support 150+ link , Connection pooling can make full use of the concurrency model .
 Insert picture description here

SQL Processing flow

  • SQL Interface:Sql Syntax analysis
  • Parser: Object permission validation and filtering
  • Optimizer: Analyze the specific execution path of the statement , According to the execution mode of cache analysis , Optimizer
  • Cathes & Buffer: cache

MySql Adopt pluggable data engine , Use specific engines according to the table .

2. Three paradigms and anti paradigms of database

  • Paradigm one : Make sure that each column remains atomic , All fields in the database are non decomposable atomic values .
  • Formula Two : Make sure that every column in the table is related to the primary key , You can't just relate to a part of the primary key ( Composite index ).
  • Formula 3 : Similar to II , Ensure that each column is directly related to the primary key , Not indirectly ; Reduce data redundancy .
  • Anti paradigm : Paradigms can avoid data redundancy , Reduce database space , Reduce the trouble of maintaining data integrity ; However, the use of normal form results in more tables , Cause more connection queries , Affect performance , Therefore, anti - normal form design is also considered .

Be careful :

  • Null queries do not go through the index , It is recommended not to judge empty .is null Cause index invalidation .
  • LIMIT 1,2 The speed of range query is relatively fast .
  • The purpose of grouping query is to remove duplicates and merge .
  • in /not in A full scan will be performed

other

Delete the detail difference of the table

  • DROP TABLE ‘table_name’; Delete table and table structure .
  • TRUNCATE TABLE ‘table_name’; Truncation table , There is a self incrementing index that accumulates from the initial value . Relatively efficient .
  • DELETE TABLE ‘table_name’; Delete line by line , There is a self incrementing index that continues to accumulate from the previous value .

Why? inodb To actively create a primary key , And take the self increasing integer as the primary key ?

Foreign key constraints

CREATE TABLE ‘course’{
‘cid’ int(11) NOT NULL AUTO_INCREMENT,
‘cname’ varchar(32) NOT NULL,
‘teacher_id’ int(11) NOT NULL,
PRIMARY KEY (‘cid’),
KET ‘fk_course_teacher’ (‘teacher_id’),
CONSTRAINT ‘fk_course_teacher’ FOREIGN KEY (‘teacher_id’) REFERENCES "teacher’(‘id’)
}ENGINE=InnoDB AUTO _INCREMENT=5 DEFAULT CHARSET=utf8;
– When the delete teacher In the table id Line time ,teacher_id It's also deleted .

Group aggregation

SELECT ‘gender’,GROUP CONCAT(sname) FROM ‘student’ GROUP BY ‘gender’;

原网站

版权声明
本文为[Tuen Mun pheasant calls me chicken]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130647575486.html