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 979
梦毁少年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:49

    You probably don't want to lock the table. If you do that you'll have to worry about trapping errors when the other crawlers try to write to the database - which is what you were thinking when you said "...terribly complex and relies on many other things."

    Instead you should probably wrap the group of queries in a MySQL transaction (see http://dev.mysql.com/doc/refman/5.0/en/commit.html) like this:

    START TRANSACTION;
    SELECT @URL:=url FROM tablewiththeurls WHERE uncrawled=1 ORDER BY somecriterion LIMIT 1;
    INSERT INTO loggingtable SET url=@URL;
    COMMIT;
    

    Or something close to that.

    [edit] I just realized - you could probably do everything you need in a single query and not even have to worry about transactions. Something like this:

    INSERT INTO loggingtable (url) SELECT url FROM tablewithurls u LEFT JOIN loggingtable l ON l.url=t.url WHERE {some criterion used to pick the url to work on} AND l.url IS NULL.
    

提交回复
热议问题