当前位置:网站首页>Shardingsphere JDBC < bind table > avoid join Cartesian product
Shardingsphere JDBC < bind table > avoid join Cartesian product
2022-06-13 05:46:00 【Coffee is not bitter**】
List of articles
During normal project development , It is inevitable to use cascading query to realize some functions , The same is true after dividing the database and tables . But when operating two tables , We found that our ten pieces of data appeared 20 strip , That is, the Cartesian product phenomenon . So how to avoid this situation ?
In the first part, we sorted out some The core concept Binding tables can avoid this situation .
One 、 What is Cartesian product ?
In short, it is the result of multiplication of two sets . At the database level , When the Cartesian phenomenon occurs , In fact, it is not the result of the program . So in the process of development , See what the requirements are , Avoid unnecessary results returning .
Two 、demo Scenario implementation
We have two tables , One course, One user,user Table has status,course Watch, too status, according to status The same will course Of value The value assigned to user Of status Field and then query the results , And then determine whether the results are qualified .
Tips :course and user The entity is not displayed this time
1) The key codes are as follows :
/** * @Auther: huoyajing * @Date: 2021/12/4 05:55 * @Description:courseMapper, use mybatis-plus It's directly packaged CRUD */
@Repository
public interface CourseMapper extends BaseMapper<Course> {
}
@Repository
public interface UserMapper extends BaseMapper<User> {
/** * Query the user status * @return */
@Select("select u.id,u.name,u.type,c.value as status from user u left join course c on u.status=c.status")
List<User> queryUserStatus();
}
@Test
public void addCourse1() {
// Course insertion
Course course1=new Course();
course1.setName("huohuo0");
course1.setType(0L);
course1.setStatus(0);
course1.setValue(" fail, ");
courseMapper.insert(course1);
Course course2=new Course();
course2.setName("huohuo1");
course2.setType(1L);
course2.setStatus(1);
course2.setValue(" pass ");
courseMapper.insert(course2);
// Student information insert
for (int i=0;i<10;i++){
User user=new User();
user.setName("user No:"+i);
user.setStatus(""+i%2);
user.setType(i);
userMapper.insert(user);
}
}
2) Query results
The test result of itself is ten users , The results of the plan are also ten . But there was 20 strip , The Cartesian product phenomenon appears 
3) Cartesian product emergence analysis

Through analysis, it is found that the query result is A1B1,A1B2,A2B1,A2B2. But this is not the result we want , How to solve it ?
3、 ... and 、 What is a bound table ?
Main table and sub table with consistent fragmentation rules
Four 、 Binding table configuration
spring.shardingsphere.sharding.binding-tables[0]=user,course
1) What does the binding table solve ?
take table-strategy.inline.algorithm-expression Of value The same calculated values are bound together to query , Just like ours course,user Two status Equality query , if user Tabular status=0 stay user_1, that course Of status=0 Will only be in course_1. Then other intersection queries are meaningless .
2) Why? binding-tables[0]?

We found that binding-tables yes Collection type , It means that during your normal project development process , There is not necessarily only one bound table , There must be multiple , We have just given an example here .
5、 ... and 、 result


6、 ... and 、 summary
The database and table must be designed for most scenarios from the beginning , Otherwise, later maintenance is not easy
边栏推荐
- Calculate the number of days between two times (supports cross month and cross year)
- Some methods of string
- A simple recursion problem of linked list
- Web site learning and sorting
- High availability of Nacos series
- KVM virtualization management tool
- Fast power code
- Detailed explanation of R language sparse matrix
- Class conflicts caused by tongweb Enterprise Edition and embedded Edition
- Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience
猜你喜欢

Quartz database storage

Byte buddy print execution time and method link tracking

AUTOSAR实战教程pdf版

MySQL table data modification

Automatic database backup (using Navicat)

Install harbor (online offline)

JS output uincode code

Problems encountered in the use of PgSQL

Service fusing and degradation of Note Series

MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
随机推荐
Etcd understanding of microservice architecture
NVIDIA Jetson nano/xavier NX capacity expansion tutorial
ZABBIX wechat alarm
中断处理过程
Detailed explanation of R language sparse matrix
Compilation croisée helloworld avec cmake
Standard input dialog for pyqt5 qinputdialog
Pychart professional edition's solution to SQL script error reporting
@Detailed explanation of propertysource usage method and operation principle mechanism
AUTOSAR actual combat tutorial pdf version
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Getclassloader() returns null, getclassloader() gets null
Set the correct width and height of the custom dialog
Calculate the number of days between two times (supports cross month and cross year)
890. Find and Replace Pattern
The problem of distinguishing and sharing sessions for multiple applications in tongweb
2020 personal annual summary
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
15 inclusivegateway and eventgateway of flowable gateway