当前位置:网站首页>Bit of MySQL_ OR、BIT_ Count function
Bit of MySQL_ OR、BIT_ Count function
2022-07-05 06:21:00 【StoNENeee】
1. Preface
Recently in to see MySQL Official documents , Find a very clever usage , You can quickly calculate the number of days a user visits the system every month . Now let's analyze the principle .
2. Data preparation
CREATE TABLE data_test (year YEAR, month INT UNSIGNED, day INT UNSIGNED);
INSERT INTO data_test VALUES(2022,1,1),(2022,1,20),(2022,1,30),(2022,2,2),(2022,2,5),(2022,2,5);
From the above table creation statement and data insertion statement 2022 Year of 1 The number of visit days in the month is 3,2 The number of visit days in the month is 2.
According to the general query , You can query according to the following statement :
with a as (select `year`, `month` from data_test group by `year`, `month`)
select b.`year`, b.`month`, count(distinct day) days
from a
left join data_test b on a.`month` = b.`month` and a.`year` = b.`year`
group by b.`year`, b.`month`;
+------+-------+------+
| year | month | days |
+------+-------+------+
| 2022 | 1 | 3 |
| 2022 | 2 | 2 |
+------+-------+------+
3. Data query
As usual , We need to check twice to get the result . In fact, we have a simpler way to query , Is the use of MySQL For us BIT_OR
、BIT_COUNT
function .
SELECT `year`, `month`, BIT_COUNT(BIT_OR(1 << day)) AS days
FROM data_test
GROUP BY `year`, `month`;
+------+-------+------+
| year | month | days |
+------+-------+------+
| 2022 | 1 | 3 |
| 2022 | 2 | 2 |
+------+-------+------+
You can see that the results obtained are consistent with the results of the above query using the conventional method , So what is the principle of these two functions ? Let's analyze one by one .
BIT_OR
: This function is used toAn operation
MediumOr operations
Of .BIT_COUNT
: This function is used to calculate 1 The number of .1 << day
: Express 1 Moving to the left day bits .1 << 1
The value is 0000 0000 0000 0000 0000 0000 0000 00101 << 20
The value is 0000 0000 0001 0000 0000 0000 0000 00001 << 30
The value is 0000 0001 0000 0000 0000 0000 0000 0000
For this 3 Value for
Or operations
Words , You can get 0000 0001 0001 0000 0000 0000 0000 0010, take 1 The number of 3
therefore MySQL Get the result through very clever bit operation , It also improves performance .
Last , Welcome to WeChat official account.
边栏推荐
- Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- [2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
- [learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
- Liunx starts redis
- 传统数据库逐渐“难适应”,云原生数据库脱颖而出
- MySQL advanced part 1: stored procedures and functions
- RGB LED infinite mirror controlled by Arduino
- [leetcode] day94 reshape matrix
- Leetcode-6111: spiral matrix IV
猜你喜欢
MIT-6874-Deep Learning in the Life Sciences Week 7
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
MySQL advanced part 1: index
LeetCode-61
MySQL advanced part 2: the use of indexes
高斯消元 AcWing 884. 高斯消元解异或線性方程組
LeetCode-54
博弈论 AcWing 891. Nim游戏
随机推荐
Sqlmap tutorial (II) practical skills I
[rust notes] 16 input and output (Part 2)
Leetcode-6109: number of people who know secrets
论文阅读报告
MySQL advanced part 2: MySQL architecture
高斯消元 AcWing 884. 高斯消元解异或线性方程组
How to understand the definition of sequence limit?
Data visualization chart summary (II)
Groupbykey() and reducebykey() and combinebykey() in spark
MySQL advanced part 2: storage engine
1039 Course List for Student
JS quickly converts JSON data into URL parameters
[BMZCTF-pwn] ectf-2014 seddit
What's wrong with this paragraph that doesn't work? (unresolved)
Introduction to LVS [unfinished (semi-finished products)]
Leetcode heap correlation
容斥原理 AcWing 890. 能被整除的数
求组合数 AcWing 887. 求组合数 III
Arduino 控制的 RGB LED 无限镜
做 SQL 性能优化真是让人干瞪眼