I'm working on a scheduler, for our internal production. the story of the problem is this: we have incoming 'jobs', that get sent to various stations 'machines'. we are storing the list of jobs in a mysql database. I want to make an interface for the production coordinator: the person responsible for getting jobs onto the correct machine - which would consist of a list of incoming jobs, and lists for each machine representing the job que for that machine.
some of the actions we need to be able to do: -reorder the list without a massive write to a sql DB - such as placing a job on a machine at the top of the que not at the end of a que, having the rest of the que update properly(a cascade of renumbering is what I'm trying to avoid).
things i'm worried about are scalability, if we get 100000's of jobs, or if we massively increase the number of our machines.
some of the things we have considered - and are still debating: creating a table with the following structure: jobkey, machineid, status, queposition. with a constraint of queposition being unique in respect to machineid. this would be good for any number of machines but quickly updating the que would require a search through all the db for each machine id and re-ordering would be less that clean. another thought was to have a prekey and postkey field to simulate a linked list, where the prekey is the table key to the last que entry, and post would point to the next. which would solve some problems, but kick the troubles can down to displaying the que.
neither of these seem to solve the problem completely.
this seems like a general enough problem I'm sure there's a good solution to que's - and modifying ordering of said list without conflict. working in javascript, php and mysql.
The idea of having a "queue position" column makes good sense to me. (Go ahead and index on that column combined with machine-id.) Then, when querying the database,
ORDER BY ... DESC
this queue-position column and some other column that makes sense ... a timestamp, say.Queue-position values don't have to be consecutive, nor unique, and a simple
select.. MAX()
query can tell you the highest value that's currently out there (for a particular machine). Just update the one row that you want to bump to the top.Another simple trick, if you do anticipate a slight amount of re-ordering, is to borrow from the old BASIC-programming days and use queue-position values that are incremented, say, by
10
. This gives you unused values that you can use if you'd like your positioning to be fairly accurate. (As I said, the values don't have to be consecutive and they don't have to be unique.)