Some methods commonly used in Mysql

1. Time stamp to date

select *,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i%s') as date from user where FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i%s') > curdate() group by date order by date desc;

In the above SQL statement, the create_time field is formatted as a date, and the fields after the format are searched and sorted.

A closer look may find that I directly use the formatted alias dete for group and order, while the where query reformats the fields, which is related to the execution order of mysql. When executing to where, the result has not been queried yet, and the alias will not take effect naturally, so you need to pay attention to it.

2. Conditional judgment

①IFNULL
select IFNULL(score1,score2) from score;

If score1 is NUll, score2 is returned; otherwise score1 is returned

②IF
select IF(sex,'男','女') from user;

The first parameter is the condition. If it is a floating point number, consider it as 0 and return'female'

The first parameter can also be written as a comparison: sex=1

③Condition judgment in aggregate function
select count(case when age>10 then id end) from user;

The above sql statement queries the number of users whose age is greater than 10

3. Chinese field sorting

Sort the data in the data table according to the first letter of the Chinese field worth pinyin, assuming that the field is name

If the character encoding of name is latin_swedish_ci

select * from user order by birary(name) asc;

If the character encoding of name is utf8_general_ci

select * form user where 1 order by convert(name using gbk) collate gbk_chinese_ci asc;

4. Sort in the specified order

select * form user where id in(1,2,3,4,5) order by field(id,5,4,3,2,1);

The returned value will be sorted according to id 5,4,3,2,1

5. Splicing fields

select concat(id,name,'666') from user;

The parameter in concat() can be a string or a field in the data table. When one of the fields in the parameter is NULL, the row data is NULL. If you want to avoid this situation, you can use CONCAT_WS (), the usage is slightly different

6. The initial value of the initial self-increment id is 1

alter table 表名 auto_increment=1;

7. Other fields after group

After using group to combine rows with the same specified field into a row, except for the group field which is our expected value, other fields in the row may not be what we want.

For example, after I want to merge, the value of the time field create_time is the largest, but many times we are not sure whether the create_time is the largest or smallest. At this time, we need to add the fields we want when selecting, such as:

select *,count(id),MAX(create_time) as last_time from pay_list group by uid;

8. Some simple operating principles of mysql

① When mysql executes the query statement, regarding the execution order of the where condition, it is said that it is executed sequentially from left to right on the Internet. Therefore, when querying a table with a large amount of data, it is best to put the condition that can exclude the most invalid data. On the far left ( even if it is wrong, there is nothing to lose ).
② On the operating principle of the mysql in condition
select * from a where id in(id_str);
the execution sequence of the statement is similar to the following process.

$a = select * from a;
$b = id_str;
$result = [];
foreach($a as $v){
	foreach($b as $vv){
		if($v['id'] == $vv){
			$resulr[] = $v;
		}
	}
}

When where requires multiple in conditions, and the conditions are related. For
example, we want to filter: Beijing, age 21, 22; Shanghai, age 22, 23 people.
Traditional writing: select * from A where address in (' Beijing','Shanghai') and age in (21,22,23); is wrong, it will filter out: Shanghai, invalid data of age 21.
We can change the above statement to: select * from A where (address ,age ) in (('Beijing','21'),('Beijing','22'),('Shanghai','22'),('Shanghai','23'),);

9, tens of millions of data count ()

When there is a lot of data, use count to query the number, even if there is an index, it takes tens of seconds, and the efficiency is too low. See online that you can use mysqlexplainThe function gets the number of rows.
This function is specifically used to view the execution efficiency of sql statements, and there are many articles available for reference on the Internet.
Definition: The explain command is very fast, because explain does not actually execute the query, but the number of rows [estimated] by the query optimizer.
After we use explain, we will see that a lot of parameters are returned.
Insert picture description here
Take rows, which is the number of rows.

10. Million-level data paging optimization

In fact, there are less than one million data, and my data table has less than 400,000 data. When fetching the last paged data in the case of joint tables, the speed reached about 3 seconds. Finally found that the problem appeared in order by.
Insert picture description hereInsert picture description here
Insert picture description here
As you can see, using order by causes the query speed to be 10 times slower.
When order by is used, mysql uses Using filesort, which means that additional external sorting actions are required in addition to the index. Aid is the primary key index, and it is not used.
After searching, I know that if you want to use the index in the order by field, the select field must be consistent with the order by. In
Insert picture description here
Insert picture description here
this case, we query the entire data period and use order by to sort it becomes infeasible, because we can’t put The required columns are indexed. At this time, I can first query only the primary key id, and after obtaining the data of a certain page, use the in query to query all the data.

select * from ask_question where aid in (select t.aid from (select aid from ask_question limit 30000,10) t);

As you can see, a double subquery is used. This is because mysql stipulates that limit cannot be used in subqueries, but it is enough to nest a layer of subqueries in word queries.

Guess you like

Origin blog.csdn.net/u012830303/article/details/82256024