当前位置:网站首页>Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
2022-07-02 20:23:00 【Java technology stack】
Environmental preparation
Database version : MySQL 5.7.20-log
Build table SQL:
DROP TABLE IF EXISTS `t_ware_sale_statistics`;CREATE TABLE `t_ware_sale_statistics` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Primary key id', `business_id` bigint(20) NOT NULL COMMENT ' Business organization code ', `ware_inside_code` bigint(20) NOT NULL COMMENT ' Commodity self coding ', `weight_sale_cnt_day` double(16,4) DEFAULT NULL COMMENT ' Average daily sales ', `last_thirty_days_sales` double(16,4) DEFAULT NULL COMMENT ' lately 30 Daily sales ', `last_sixty_days_sales` double(16,4) DEFAULT NULL COMMENT ' lately 60 Daily sales ', `last_ninety_days_sales` double(16,4) DEFAULT NULL COMMENT ' lately 90 Daily sales ', `same_period_sale_qty_thirty` double(16,4) DEFAULT NULL COMMENT ' In the same period last year 30 Daily sales ', `same_period_sale_qty_sixty` double(16,4) DEFAULT NULL COMMENT ' In the same period last year 60 Daily sales ', `same_period_sale_qty_ninety` double(16,4) DEFAULT NULL COMMENT ' In the same period last year 90 Daily sales ', `create_user` bigint(20) DEFAULT NULL COMMENT ' founder ', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ' Creation time ', `modify_user` bigint(20) DEFAULT NULL COMMENT ' Final modifier ', `modify_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ' Final revision time ', `is_delete` tinyint(2) DEFAULT '2' COMMENT ' Whether or not to delete ,1: yes ,2: no ', PRIMARY KEY (`id`) USING BTREE, KEY `idx_business_ware` (`business_id`,`ware_inside_code`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT=' Commodity sales statistics ';
- 1.
Initialization data
To prepare the 769063 Data
Demand background
Sales of goods under the business organization , The same business organization can sell different goods , The same commodity can be sold in different business institutions , That is to say : The relationship between business organization and commodity is many to many
Suppose there are now n Organizations , There are several commodities under each organization , How to query the sales of goods in these stores ?
Be specific , It's like this
How to find out 100001 Lower commodity 1000、1001、1003 、 100002 Lower commodity 1003、1004 、 100003 Lower commodity 1006、1008、1009 Sales of
It's like a double-layer list ( A set of goods list in the business organization list ) Query for ; Neither the business organization list nor the commodity list is fixed , It's dynamic
So the problem is : How to query multiple business organizations , Sales of some goods ( Once I described the problem , Maybe it's more blurred , If only everyone understood the meaning !)
Circular query
It's easy to think of , Circulate the list of business institutions at the code level , Each business organization checks the database once , The pseudocode is as follows :
Concrete SQL It's like this
SQL Can go index
Implement a simple , Also understand ,SQL You can also go , Everything seems perfect
But the reality is : Department development specification constraints , You can't cycle through the database
Oh, Ho , This way can only give up , Find another way
OR Splicing
adopt MyBatis Of dynamic SQL function , Conduct SQL Splicing , It's like this
Concrete SQL It's like this
SQL You can also go
Implement a simple , Also understand ,SQL You can also go , And only query the database once , It seems feasible
The only pity is : It's a little expensive OR, If there are many business institutions , that SQL It will be longer
As one of the candidates , Let's move on
Mixed query and filtering
It's also using Mybatis Of dynamic SQL , take business_id Put the lists together 、 ware_inside_code Put together , It's like this
Concrete SQL It's like this
SQL You can also go
Implement a simple , Also understand ,SQL You can also go , And only query the database once , It seems to work
however : The result set found is greater than or equal to the result set we want , Your products , Your delicacies !
Therefore, you also need to filter the found result set , Filter out the result set we want
Let's be one of the candidates , Let's move on
Line by line comparison
SQL-92 Line to line comparison function is added in , thus , Comparison predicate = 、< 、> and IN The parameters of the predicate are no longer just scalar values , It can also be a list of values
Of course , Still have to use Mybatis Of dynamic SQL , It's like this
Concrete SQL It's like this
SQL Can also walk
Implement a simple ,SQL You can also go , And only query the database once , Feel feasible
It's just : It's a little hard to understand , Because we usually use so little , So this kind of writing looks strange
in addition , The line comparison is SQL standard , Not a specification for a relational database , In other words, relational databases should support this writing method
summary
1、 Finally, I chose Line by line comparison This way to achieve the requirements , Don't ask me why , Asking is forcing gegao !
2、 There are many ways to realize a requirement , We need to consider the business and various constraints , Choose the most suitable one
3、 The line comparison is SQL-92 Introduced in ,SQL-92 yes 1992 Specifications formulated in , Line to line comparison is not a new feature , It's a basic function that has existed for a long time !
I think it's good , Don't forget to like it + Forward !
边栏推荐
- AcWing 341. Optimal trade solution (shortest path, DP)
- 疫情封控65天,我的居家办公心得分享 | 社区征文
- Codeforces round 651 (Div. 2) (a thinking, B thinking, C game, D dichotomy, e thinking)
- AcWing 1129. Heat wave solution (shortest path SPFA)
- Educational codeforces round 129 (rated for Div. 2) supplementary problem solution
- upload-labs
- 测试人员如何做不漏测?这7点就够了
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- 励志!大凉山小伙全奖直博!论文致谢看哭网友
- Wu Enda's machine learning mind mapping insists on clocking in for 23 days - building a knowledge context, reviewing, summarizing and replying
猜你喜欢
[real case] trap of program design - beware of large data
Notes on hardware design of kt148a voice chip IC
Outsourcing for three years, abandoned
After writing 100000 lines of code, I sent a long article roast rust
数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
Postman interface test practice, these five questions you must know
测试人员如何做不漏测?这7点就够了
蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
编写完10万行代码,我发了篇长文吐槽Rust
随机推荐
疫情封控65天,我的居家办公心得分享 | 社区征文
证券如何在线开户?手机开户是安全么?
at编译环境搭建-win
Implementing yolox from scratch: dataset class
sql-labs
How to open an account online? Is it safe to open a mobile account?
[daily question] 241 Design priorities for operational expressions
Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!
Dictionaries
CheckListBox control usage summary
【Hot100】23. 合并K个升序链表
在网上炒股开户安全吗?我是新手,还请指导
B端电商-订单逆向流程
【871. 最低加油次数】
What is online account opening? Is it safe to open an account online now?
【NLP】一文详解生成式文本摘要经典论文Pointer-Generator
Complete example of pytorch model saving +does pytorch model saving only save trainable parameters? Yes (+ solution)
Summary of interview experience, escort your offer, full of knowledge points
AcWing 341. Optimal trade solution (shortest path, DP)
For (Auto A: b) and for (Auto & A: b) usage