select 30 random rows where sum amount = x

前端 未结 7 1429
感情败类
感情败类 2020-12-10 02:39

I have a table

items
id int unsigned auto_increment primary key,
name varchar(255)
price DECIMAL(6,2)

I want to get at least 30 random ite

7条回答
  •  一个人的身影
    2020-12-10 03:32

    If you want to be efficient stop wasting your time and go for eventual consitency. Create console script that does what you want to accomplish by any means necessary, then run this script in CRON or with any scheduling software once in a while.

    Having 100, 1000 visitors would you want your query to be executed every time? This is time and resource consuming. Randomly ordered queries cannot be cached by DBMS's too. Go for eventual consistency: create a table to hold that records and purge it each time, lock for writing, then load with new set, every 5 minutes for instance.

    At least this is how I do it in heavily loaded applications. In the code it's matter of running plain SELECT query.

提交回复
热议问题