当前位置:网站首页>MySQL common skills
MySQL common skills
2022-06-23 08:10:00 【Half way out of school】
1. Common operation statements
Connect
mysql -hxx -Pxx -uxx -pxx
Build table
CREATE TABLE `user` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` int(10) DEFAULT '0', `pos` varchar(30) DEFAULT NULL, `phone` varchar(11) DEFAULT NULL, `created_time` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_name_age_pos_phone` (`name`,`age`,`pos`,`phone`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Operation table
select * from student left join score on student.id=score.s_id order by score_val desc limit 15 select DISTINCT course_name from student_table where grade=1; select * ,count(b) as sum from table where a='' group by s having sum>10 order by c desc limit select s.id, c.name from student s left join course c on s.id=c.student_id insert into table (a,b) values (1,2) delete from table where id>10 update table set a=1 where id=1 alter table app_apply add log_index varchar(255) default Null // change 、 Add fields alter table Table name add column Name varchar(20) not null first; alter table Table name add column Name varchar(20) not null after user1; alter table app_apply drop column def ; alter table app_apply change def unit char;
Aggregate functions
–COUNT: Count the number of lines
–SUM: Get the total value of a single column
–AVG: Calculate the average value of a column
–MAX: Calculate the maximum value of the column
–MIN: Calculate the minimum value of the column
SELECT student_class,AVG(student_age) AS Average age FROM t_student GROUP BY (student_class) HAVING AVG(student_age)>20;
Add index
// Add primary key index ALTER TABLE `table_name` ADD PRIMARY KEY (`column`) // Add unique index ALTER TABLE `table_name` ADD UNIQUE (`column`) // Add full text index ALTER TABLE `table_name` ADD FULLTEXT (`column`) // Add a normal index ALTER TABLE `table_name` ADD INDEX index_name (`column` ) // Add Composite Index ALTER TABLE `table_name` ADD INDEX index_name (`column1`, `column2`, `column3`)
##2. The slow query
Check the database status
status; show tables; select * from information_schema.tables where table_name ='table_name' show variables like 'port'; show status like 'Threads%'; // Check the current number of connections to the database , Concurrency number show variables like '%max_connections%'; // See the maximum number of connections set global max_commections = 1000; // Modify the maximum number of connections show grants for 'root'@'localhost'; // View the permissions of a specific user
Index failure
- If the index has multiple columns ( Joint index a,b,c) Follow the leftmost prefix rule , That is, the query starts at the top left of the index and does not skip the columns in the index
- The column to the right of the index range condition ,< 、> The index column to the right of the index range condition will become invalid
- select *
- Do operations on index columns , Such as calculation 、 function 、( Automatically or Manual ) Type conversion, etc , Will result in index invalidation and full table scan
- keyword or 、not in 、!=、<> It's not equal to
- Use LIKE Keyword query statement , If it starts with a wildcard ('%abc') Index failure . Only “%” Index doesn't work until it's in the first place
- String without single quotes
- order by Violation of the leftmost prefix rule , Causes additional file sorting ( It can degrade performance )
- group by Violation of the leftmost prefix rule , Resulting in a temporary watch ( It can degrade performance )
adopt explain analysis sql sentence
explain select * from user where age = 20 and phone = '18730658760' and pos = 'cxy';
Show results analysis :
table | type | possible_keys | key |key_len | ref | rows | Extra
EXPLAIN Explanation of columns :
table Show which table this row's data is about
type This is an important column , Shows what type of connection is used . The best to worst connection type is const、eq_reg、ref、range、indexhe and ALL
rows Display the number of lines to scan
key Index used
perform show processlist;
see mysql Running sql Threads , You can see sql Statement execution time
show processlist
show full processlist
mysqladmin processlist
select * from information_schema.processlist // View all current process
select * from information_schema.processlist where info is not null order by time desc // View the query currently in progress and reverse it according to the execution time
see mysql Slow query log ( By default ,mysql Do not open slow query log )
Log analysis tool mysqldumpslow
Get the most returned recordset 10 individual SQL.
mysqldumpslow -s r -t 10 /database/mysql/mysql06_slow.log
The most visited 10 individual SQL
mysqldumpslow -s c -t 10 /database/mysql/mysql06_slow.log
The number of slow queries is saved in mysql In the library slow_log surface .
SELECT * FROM slow_log where start_time > '2019/05/19 00:00:00';
Business logic
The data is huge
Deep paging Limit Causes a full table scan
Uneven data distribution
Network environment factors
One request execution 30 strip Sql Transmission time increases
network bandwidth Network card full
Packet loss retry
边栏推荐
- 华为云服务器弹性公网IP无法ping
- Captain Abu's soul torture
- GTEST death test
- Markdown learning
- What are open source software, free software, copyleft and CC? Can't you tell them clearly?
- 2 corrections de bogues dans l'outil aquatone
- Mathematical knowledge: Euler function - Euler function
- Set接口和Set子实现类
- Capturing packets to find repeated acks and a large number of TCP retransmissions in TCP sessions -- sack (selective acknowledgement) technology
- Tensorboard的使用
猜你喜欢
随机推荐
Commonly used bypass methods for SQL injection -ctf
Deep learning ----- different methods to implement lenet-5 model
Tri rapide + Tri par bulle + Tri par insertion + Tri par sélection
忽略超长参数违规
Query on the performance of multi table view in MySQL
C#打印缩放
Learn to draw Er graph in an article
ArcMap batch delete points closer
openni.utils.OpenNIError: (OniStatus.ONI_STATUS_ERROR, b‘DeviceOpen using default: no devices found‘
SQL注入常用到的绕过方式-ctf
Quickly delete the node in the code_ modules
配置ASMX无法访问
Matlab random volatility SV, GARCH using MCMC Markov chain Monte Carlo method to analyze exchange rate time series
PHP serialization and deserialization CTF
C print zoom
flutter 制作TabBar的圆角
Guava cache usage summary
Apache Solr arbitrary file read replication
华为云服务器弹性公网IP无法ping
Google common syntax









