I need a little help with SELECT FOR UPDATE
(resp. LOCK IN SHARE MODE
).
I have a table with around 400 000 records and I need to run two different processing functions on each row.
The table structure is appropriately this:
data (
`id`,
`mtime`, -- When was data1 set last
`data1`,
`data2` DEFAULT NULL,
`priority1`,
`priority2`,
PRIMARY KEY `id`,
INDEX (`mtime`),
FOREIGN KEY ON `data2`
)
Functions are a little different:
- first function - has to run in loop on all records (is pretty fast), should select records based on
priority1
; setsdata1
andmtime
- second function - has to run only once on each records (is pretty slow), should select records based on
priority2
; setsdata1
andmtime
They shouldn't modify the same row at the same time, but the select may return one row in both of them (priority1
and priority2
have different values) and it's okay for transaction to wait if that's the case (and I'd expect that this would be the only case when it'll block).
I'm selecting data based on following queries:
-- For the first function - not processed first, then the oldest,
-- the same age goes based on priority
SELECT id FROM data ORDER BY mtime IS NULL DESC, mtime, priority1 LIMIT 250 FOR UPDATE;
-- For the second function - only processed not processed order by priority
SELECT if FROM data ORDER BY priority2 WHERE data2 IS NULL LIMIT 50 FOR UPDATE;
But what I am experiencing is that every time only one query returns at the time.
So my questions are:
- Is it possible to acquire two separate locks in two separate transactions on separate bunch of rows (in the same table)?
- Do I have that many collisions between first and second query (I have troubles debugging that, any hint on how to debug
SELECT ... FROM (SELECT ...) WHERE ... IN (SELECT)
would be appreciated )? - Can
ORDER BY ... LIMIT ...
cause any issues? - Can indexes and keys cause any issues?