Dynamic addition and deletion of data in the database, resulting in duplication or omission of paging query data problem analysis and solutions

1. Problem analysis

1. Request data

    Under normal circumstances, in order to reduce the pressure on the server or facilitate display, the front end requests data through paging, and the parameters page and pageSize will be brought when calling the API interface. For example, request the student data of a certain class, and get the data of 10 students on the first page, assuming that they are arranged in reverse order of registration time:

https://api.domain.com/class/student/list?page=1&pageSize=10

    Pagination query is realized by SQL limit statement, limit usage:

select * from table_name limit [offset,] rows


offset: Specify the offset of the first record line (that is, from which line to return), note: the offset of the initial line is 0.
rows: returns the specific number of rows.

    Request the first page of data, page = 1, offset is equal to (1 - 1) * 10, which is 0;

select * from tb_student order by register_time desc limit 0, 10;

    Request the second page of data, page = 2, offset is equal to (2 - 1)*10, which is 10;

select * from tb_student order by register_time desc limit 10, 10;

2. Duplicate or missing data

    Assuming that the first page of data has been obtained, before requesting the second data, a new student is newly registered in the background. If it is arranged in reverse order of registration time, this newly registered student will become the first data. If it is in the "stack" way To understand: the last piece of data on the original first page, because of the insertion of new data, is PUSHed to the position of the first piece of data on the second page, so when we get the data on the second page, it will be "repeatedly " obtained To the last record of the original first page of data, when this happens, the front end usually displays two pieces of student data that are next to each other and identical.

    In the same way, if before we get the data on the second page, the students on the first page are deleted from the database because of logout, and the starting position offset of all the data is reduced by 1, and the first data on the second page It becomes the last piece of data on the first page, so the result returned when we request the second page of data will be "missing" this piece of data.

Summary: When the database data changes dynamically, if our offset does not have real-time dynamic correction, and still uses the method of offset = (page - 1) * pageSize to obtain data, data duplication or missing will occur .

Two, the solution

    After the above analysis, we know that every time we request data, if the correct offset can be obtained, there will be no appeal. This offset should be the last student record of the first page of student data requested . The position of " when sorting in reverse order ". Therefore, we get the ID of the last record, upload it to the interface provided by the background through the parameter " fromId = ID ", and obtain the correct offset for requesting the next page of data through calculation, so that no matter how the database data is added or deleted, there will be no duplication or missing cases. Generally speaking, every time data is requested, 10 new records are obtained starting from the next record of the record represented by formIId.

    Request the first page of data, fromId = 0

https://api.domain.com/class/student/list?fromId=0&pageSize=10

    We agree that fromId = 0, which means requesting the first page of data, so the offset value is 0; assuming the student_id of the last piece of data on the first page of data = 1001;

    Request the second page of data, with the parameter fromId = 1001;

https://api.domain.com/class/student/list?fromId=1001&pageSize=10

    Real-time calculation in the background to obtain the starting position of the next record after the last record (fromId):

select count(*) from tb_student where register_time >= (select register_time from tb_student where student_id = 1001);


Precautions:

1. There is a select query nested in the statement, and the inner nested query statement can only return one record, and multiple records cannot appear, otherwise an error will be thrown after executing sql.

2. If sorted by registration time in ascending order, " >= " should be replaced with " <= " .

3. The field register_time used for sorting cannot have the same value, otherwise the query data may be missing (for example, the register_time value of the 11th data and the 10th data are the same, the offset value obtained by executing the above SQL statement will cause Article 11 data will not appear in the data requested on the second page).

    Use the value returned by the above SQL query statement as the offset to request the second page of student data, and the query statement is as follows:

select * from tb_student order by register_time desc limit [offset,] 10;

Guess you like

Origin blog.csdn.net/crazestone0614/article/details/132346462