How do I lock read/write to MySQL tables so that I can select and then insert without other programs reading/writing to the database?

前端 未结 5 974
梦毁少年i
梦毁少年i 2020-12-14 06:15

I am running many instances of a webcrawler in parallel.

Each crawler selects a domain from a table, inserts that url and a start time into a log table, and then sta

5条回答
  •  别那么骄傲
    2020-12-14 06:48

    I got some inspiration from @Eljakim's answer and started this new thread where I figured out a great trick. It doesn't involve locking anything and is very simple.

    INSERT INTO crawlLog (companyId, timeStartCrawling)
    SELECT id, now()
    FROM companies
    WHERE id NOT IN
    (
        SELECT companyId
        FROM crawlLog AS crawlLogAlias
    )
    LIMIT 1
    

提交回复
热议问题