当前位置:网站首页>Xiaobai learns MySQL - Statistical 'opportunism'

Xiaobai learns MySQL - Statistical 'opportunism'

2022-06-25 04:34:00 bisal(Chen Liu)

MySQL Chinese support LIMIT Clause , Limit the number of rows returned . If it's done first LIMIT, I also want to know whether to take LIMIT How many lines can be returned , But I don't want to execute the same statement again , What can I do now ?

here , You can use MySQL A function provided FOUND_ROWS() Characteristics to solve .

(1) First , Execute once to include SQL_CALC_FOUND_ROWS Option SELECT sentence ,

[email protected] 15:49:  [test]> select SQL_CALC_FOUND_ROWS * from tt limit 1;
+------+---------------------+--------+
| code | cdate               | ctotal |
+------+---------------------+--------+
| AAA  | 2022-01-01 00:00:00 |      1 |
+------+---------------------+--------+
1 row in set, 1 warning (0.00 sec)

(2) perform FOUND_ROWS() function , The number of records in this table is returned ,

[email protected] 16:02:  [test]> select found_rows();
+--------------+
| found_rows() |
+--------------+
|           15 |
+--------------+
1 row in set, 1 warning (0.00 sec)

You can see , and COUNT(*) Number of records , It's the same ,

[email protected] 16:03:  [test]> select count(*) from tt;
+----------+
| count(*) |
+----------+
|       15 |
+----------+
1 row in set (0.01 sec)

The reason for the addition SQL_CALC_FOUND_ROWS, Because it means the number of results to be achieved , For later use FOUND_ROWS() Function to pave the way .

If you execute LIMIT I didn't bring it SQL_CALC_FOUND_ROWS,

[email protected] 15:49:  [test]> select * from tt limit 1;
+------+---------------------+--------+
| code | cdate               | ctotal |
+------+---------------------+--------+
| AAA  | 2022-01-01 00:00:00 |      1 |
+------+---------------------+--------+
1 row in set (0.00 sec)

perform FOUND_ROWS() The previous one is returned SELECT The actual number of rows returned by the statement ,

[email protected] 15:49:  [test]> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            1 |
+--------------+
1 row in set, 1 warning (0.00 sec)

FOUND_ROWS() Data obtained , It's just temporary , Execute the next statement , It will fail. , If the logic needs to use , Can be saved in advance , For example, define variable storage in a program .

About FOUND_ROWS() There are relevant introductions in the official website documents ,

https://dev.mysql.com/doc/refman/5.7/en/information-functions.html

efa270bacf6cd46634fb8de3c79665bd.png

As for the principle , I haven't seen the code , But I think SQL_CALC_FOUND_ROWS It means that when executing , The number of records in the table may be counted once , Stored in a temporary variable or a temporary area , So we need to execute this immediately FOUND_ROWS(), Get this value , Although we should do what we should do , After all, it is equivalent to MySQL Did it instead of us , At least at the executive level , It's still helpful .

Xiaobai studies MySQL

Xiaobai studies MySQL - Incremental Statistics SQL The needs of

Xiaobai studies MySQL - You've encountered this kind of scenario where you can't log in ?

Xiaobai studies MySQL - There are some differences between users created by different versions

Xiaobai studies MySQL - A tool for randomly inserting test data

Xiaobai studies MySQL - varchar Why are type fields often defined as 255?

Xiaobai studies MySQL - A case of flexible index creation

Xiaobai studies MySQL - “ Be opportunistic ” The number of records in a statistical table

Xiaobai studies MySQL - Once slow SQL The positioning of

Xiaobai studies MySQL - TIMESTAMP The type field is not empty and the default value property is affected

Xiaobai studies MySQL - Talk about the importance of data backup

Xiaobai studies MySQL - InnoDB Support optimize table?

Xiaobai studies MySQL - table_open_cache The role of

Xiaobai studies MySQL - Table space defragmentation method

Xiaobai studies MySQL - Case sensitive problem solving

Xiaobai studies MySQL - only_full_group_by Validation rules for

Xiaobai studies MySQL - max_allowed_packet

Xiaobai studies MySQL - mysqldump Parameter differences to ensure data consistency

Xiaobai studies MySQL - The query will lock the table ?

Xiaobai studies MySQL - The problem of index key length limitation

Xiaobai studies MySQL - MySQL Will be affected by “ High water level ” Influence ?

Xiaobai studies MySQL - Database software and initialization installation

Xiaobai studies MySQL - Chat

Recently updated articles :

Xiaobai studies MySQL - Incremental Statistics SQL The needs of

Several guesses about the design of Tencent conference number

Mystery of power bank

China Super League and Guoan in the new season , Through the thorns

Oracle Of CTAS Can I bring constraints and other attributes to the new table ?

Recent hot articles :

" Red Alert " Game open source code brings us a shock

Article classification and indexing :

official account 1000 Article classification and index

原网站

版权声明
本文为[bisal(Chen Liu)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250231104723.html