Mysql randomly disrupts the array problem

for example, 10 pieces of data I took out of the database and paged into two pages to upset the order
is 10 pieces of data to be disrupted and then paged
how to deal with it? Tp framework


personally, if you consider performance, it is troublesome for you to take 20 items at a time and then divide them into two pages. In fact, a more direct method is to take 10 items per page to disturb and return to the user.
in fact, if you do not consider the performance, from the business point of view, what is the purpose of you to check the data and disrupt the two pages? Do you have to break up two pages out of 20? If not, it is recommended to take 10 items per page and then disturb them.


what is this scenario?


mysql comes with the function of randomly fetching data, and the data taken out is inherently random. Why disrupt


from the upstairs answer and your question, what I understand is that the data is returned and needs to be displayed after being out of order. This is very simple to render the array out of order after getting the data, and then say that it is possible to have 10000 items at a time. This disorder will have performance problems, so I recommend using the picture lazy loading method to obtain data. There will be no problem with too much data disorder on each page.


if this is the problem, query the data from the table according to some condition, and then let the records be returned in random order.

then, consider creating a new field of type float in the table, such as rnd, and indexing the field accordingly. When the record is saved, write a random number to rnd. When the actual application query returns results, it is sorted by rnd (such as order). In this way, the query efficiency is relatively high, which is equivalent to transferring the calculation of random sorting to record writing, and it is already a relatively simple query when using the query results.


I think it would be better to take out more than 10 pieces of data at one time, and then form an array. Select 10 items from them as 2-page pagination, so the performance should be the best


regardless of performance:
go directly to the database, mysql comes with random commands, and you can add it when paging.
consider performance:
take it out and put it in nosql, such as redis, save it to a list, order a collection, and then page it with the corresponding l (z) range.

Menu