Any way to select without causing locking in MySQL?

后端 未结 8 1567
执笔经年
执笔经年 2020-11-22 15:21

Query:

SELECT COUNT(online.account_id) cnt from online;

But online table is also modified by an event, so frequently I can see lock by runn

相关标签:
8条回答
  • 2020-11-22 15:44

    another way to enable dirty read in mysql is add hint: LOCK IN SHARE MODE

    SELECT * FROM TABLE_NAME LOCK IN SHARE MODE; 
    
    0 讨论(0)
  • 2020-11-22 15:46

    Depending on your table type, locking will perform differently, but so will a SELECT count. For MyISAM tables a simple SELECT count(*) FROM table should not lock the table since it accesses meta data to pull the record count. Innodb will take longer since it has to grab the table in a snapshot to count the records, but it shouldn't cause locking.

    You should at least have concurrent_insert set to 1 (default). Then, if there are no "gaps" in the data file for the table to fill, inserts will be appended to the file and SELECT and INSERTs can happen simultaneously with MyISAM tables. Note that deleting a record puts a "gap" in the data file which will attempt to be filled with future inserts and updates.

    If you rarely delete records, then you can set concurrent_insert equal to 2, and inserts will always be added to the end of the data file. Then selects and inserts can happen simultaneously, but your data file will never get smaller, no matter how many records you delete (except all records).

    The bottom line, if you have a lot of updates, inserts and selects on a table, you should make it InnoDB. You can freely mix table types in a system though.

    0 讨论(0)
提交回复
热议问题