Code analysis of user variables in mysql query statements

Code analysis of user variables in mysql query statements

In the previous article, we introduced the MySQL optimization summary - total number of queries. In this article, we will introduce another knowledge in query statements: code parsing of user variables.

Let’s start with the code

SELECT `notice`.`id` , `notice`.`fid` , `notice`.`has_read` , `notice`.`notice_time` , `notice`.`read_time` , `f`.`fnum` , `f`.`forg` , `f`.`fdst` , `f`.`actual_parking` AS `parking` , `f`.`scheduled_deptime` , `f`.`estimated_deptime` , `f`.`actual_deptime` , `f`.`scheduled_arrtime` , `f`.`estimated_arrtime` , `f`.`actual_arrtime` , `f`.`is_vip` , `f`.`aoc_notice`
FROM (
  select
  t.id, 
  t.fid, 
  t.has_read, 
  t.notice_time, 
  t.read_time
  from
  (
    select
    a.id, 
    a.fid, 
    a.has_read,
    a.notice_time, 
    a.read_time,
    @v_rownum := @v_rownum+1,
    if(@v_fid=a.fid,@v_rowid:=@v_rowid+1,@v_rowid:=1 ) as row_count,
    @v_fid:=a.fid
    from
    (
      SELECT
      id, 
      fid, 
      has_read, 
      notice_time, 
      read_time 
      FROM vkm_user_notice_flight
      where `notice_type` = 'process_update' and uid=82
      order by fid, notice_time desc
    ) a,
    (
      select @v_rownum:=0, @v_rowid:=0, @v_fid:=null
    ) b
  )
  where t.row_count = 1
) AS `notice`
LEFT JOIN `vkm_flight` AS `f` ON `notice`.`fid` = `f`.`id`
LEFT JOIN `vkm_parking` AS `parking` ON `f`.`actual_parking` = `parking`.`parking_num`

A friend sent me a SQL statement like this at work and I was confused at first and didn’t understand it at all! Because I am not very proficient in MySQL and can only do simple additions, deletions and modifications. In fact, the above code is written very directly.

SELECT `notice`.`id` , `notice`.`fid` , `notice`.`has_read` , `notice`.`notice_time` , `notice`.`read_time` , `f`.`fnum` , `f`.`forg` , `f`.`fdst` , `f`.`actual_parking` AS `parking` , `f`.`scheduled_deptime` , `f`.`estimated_deptime` , `f`.`actual_deptime` , `f`.`scheduled_arrtime` , `f`.`estimated_arrtime` , `f`.`actual_arrtime` , `f`.`is_vip` , `f`.`aoc_notice`
FROM `notice` LEFT JOIN `vkm_flight` AS `f` ON `notice`.`fid` = `f`.`id` LEFT JOIN `vkm_parking` AS `parking` ON `f`.`actual_parking` = `parking`.`parking_num`

But the query efficiency of the above code is really different! I don't understand the specific code above, and I'm asking for guidance, but I searched on Baidu and found one is the use of if in mysql and the other is the use of user variables in mysql

The user name can be set by set var value or in the above form @var:=val;

Use of if in mysql if(exp1,exp2,exp3) In if, if exp1 is true, then execute exp2, otherwise execute exp3;

Now looking at the above code may be much simpler! Based on the above code, I wrote a simple application example again

select id,fnum,forg,fdst,@v_rownum:=@v_rownum+1 from vkm_flight,(select @v_rownum:=0) b

This returns the value of @v_rownum

Summarize

The above is all the content of this article about the code analysis of user variables in MySQL query statements. I hope it will be helpful to everyone. Interested friends can continue to refer to this site: Examples of using or statements in MySQL, Detailed explanation of the meanings of N and M in the MySQL data type DECIMAL(N,M), etc. If you have any questions, you can leave a message at any time and the editor will reply to you in time. Thank you friends for supporting this site!

You may also be interested in:
  • Example analysis of mysql variable usage [system variables, user variables]
  • Detailed explanation of mysql user variables and set statement examples

<<:  VMware Workstation 12 Pro Linux installation tutorial

>>:  VUE+Canvas implements the game of God of Wealth receiving ingots

Recommend

Example analysis of the principle and solution of MySQL sliding order problem

This article uses examples to explain the princip...

Detailed explanation of Apache website service configuration based on Linux

As an open source software, Apache is one of the ...

Specific use of Node.js package manager npm

Table of contents Purpose npm init and package.js...

jQuery implements all selection and reverse selection operation case

This article shares the specific code of jQuery t...

Detailed explanation of adding click event in echarts tooltip in Vue

Table of contents need Workaround 1. Set tooltip ...

Summary of various methods for JS data type detection

Table of contents background What are the methods...

Mysql optimization techniques for querying dates based on time

For example, to query yesterday's newly regis...

Detailed process of implementing the 2048 mini game in WeChat applet

Rendering Example Code Today we are going to use ...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

Detailed explanation of TIMESTAMPDIFF case in MySQL

1. Syntax TIMESTAMPDIFF(unit,begin,end); Returns ...

WeChat applet custom menu navigation to achieve staircase effect

Design Intentions When developing a page, you oft...

How to isolate users in docker containers

In the previous article "Understanding UID a...

How to load Flash in HTML (2 implementation methods)

First method : CSS code: Copy code The code is as ...

Database SQL statement optimization

Why optimize: With the launch of the actual proje...