1

I am new to nestjs and typeorm. I have a big table of products in MySQL. The requirement #1 is that everytime findAll is called, the products are ordered randomly. The requirement #2 is that the data is retrieved as the user scrolls down.

I thought using pagination with skip and take, ordered by id desc, would be enough, but the problem is that on each page, all users would see the same products shuffled, and the objective is that on each page each user sees diferente objects.

Please give me an idea of how to make the shuffle and the loading of products as the user scrolls down

I tried using skip and take and after retrieving the data, calling to a function that shuffles the array.

pdh28907
  • 11
  • 3
  • Does this answer your question? [PHP MySQL pagination with random ordering](https://stackoverflow.com/questions/10729633/php-mysql-pagination-with-random-ordering) – a7md0 Mar 11 '23 at 05:47
  • Not at all, they use Rand from MySQL which performs badly, and with that solution I should get ALL the table – pdh28907 Mar 11 '23 at 05:56
  • You would still be able to paginate that query, given that you also keep the random seed along with the pagination state. Not too sure what is your point of performance of the random function in MySQL, although I would say it's better than your approach of querying all the table rows. – a7md0 Mar 12 '23 at 15:48
  • the mysql random function first creates a temporary table and then calculates a random number for each row. This is a very slow operation on big tables. So my solution is to order by desc or asc a random column of the table, paginate, and then shuffle the result. – pdh28907 Mar 17 '23 at 04:56

0 Answers0