简体   繁体   中英

How to best reorder 100k database rows every hour?

Mysql - i want to reorder a 100k row database every hour. I have a field called 'order' that i sort by. how can i best reorder it?

I currently do this (pseudo):

mainpage.php : select * from table order by `order` desc limit 100;

and hourly:

cronjob.php  : select * from table order by rand(); 
$i=0;
foreach($row) {
$i++;
update table set order = $i where id = $row['id']
}

but it takes ages.

If i just do 'update table set order = rand()' there will be duplicates and i don't want order to have duplicates (but it isn't set to a UNIQUE index because as it is updating there will be duplicates.

whats the best way to go about doing this?

(i do it this way because just doing "select * from table order by rand() limit 100" was really slow, but having it on an index is much faster. it just takes quite a while to reorder it)

(mysql 5)

为什么不使用自动增量字段,然后生成一个供选择时使用的随机值列表(在最小和最大之间)?

MySQL isn't a sequential-on-disk storage anyway. This doesn't get you anything. Doing this might make rows show up in your management client in a certain order, but it won't actually add any speed to anything. Please just add an ORDER BY clause to your select statement.

how about creating another table - my_wierd_sort_table.

then delete it entirely, and insert the PK and your rand() column - and use that to order by in your selects...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM