Is incrementing a field in MySQL atomic?

前端 未结 6 805
醉梦人生
醉梦人生 2020-12-01 03:40

I\'m making a web site where I would like to increment a counter in a standard MyISAM table.

Simplified example:

UPDATE votes SET num = num + 1;
         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 04:23

    Another approach when using InnoDB is using unique index on multiple column as follow:

    Table 'Sessions' { unique_key(browser_session_id,profile_id) // ensures that inserting 1 entry per session will occur once }

    select count(browser_session_id) from Sessions

    Will guarantee result of unique sessions, as multiple sessions per user is not allowed.

    Conclusions

    • Advantage

      Each insert does require a pre-select.

    • Disadvantage

      It is not suitable for all cases.

      May slow down write performance, and requires extra management

提交回复
热议问题