我们应该使用读取提交的隔离级别吗?

时间:2016-08-23 07:27:16

标签: mysql sql read-committed

我有一个名为“radacct”的mysql innodb表。此表包含用户的互联网使用记录,如上传,下载,帐户ID等(下面的表格模式)。 radacct表以随机间隔更新,路由器发送数据。我们还使用此表来计算互联网用户的总带宽,带宽计算查询(选择)大约需要3-4秒。当并行执行带宽计算查询和来自竞争锁的路由器的更新查询时,会出现问题。这是由于RepeatableRead锁(表级锁定)还是更好地在这里使用ReadCommitted隔离?

describe freeradius.radacct;
+----------------------+-------------+------+-----+---------+----------------+
| Field                | Type        | Null | Key | Default | Extra          |
+----------------------+-------------+------+-----+---------+----------------+
| radacctid            | bigint(21)  | NO   | PRI | NULL    | auto_increment |
| acctsessionid        | varchar(32) | NO   | MUL |         |                |
| acctuniqueid         | varchar(32) | NO   | MUL |         |                |
| username             | varchar(64) | NO   | MUL |         |                |
| groupname            | varchar(64) | NO   |     |         |                |
| realm                | varchar(64) | YES  |     |         |                |
| nasipaddress         | varchar(15) | NO   | MUL |         |                |
| nasportid            | varchar(15) | YES  |     | NULL    |                |
| nasporttype          | varchar(32) | YES  |     | NULL    |                |
| acctstarttime        | datetime    | YES  | MUL | NULL    |                |
| acctstoptime         | datetime    | YES  | MUL | NULL    |                |
| acctsessiontime      | int(12)     | YES  | MUL | NULL    |                |
| acctauthentic        | varchar(32) | YES  |     | NULL    |                |
| connectinfo_start    | varchar(50) | YES  |     | NULL    |                |
| connectinfo_stop     | varchar(50) | YES  |     | NULL    |                |
| acctinputoctets      | bigint(20)  | YES  |     | NULL    |                |
| acctoutputoctets     | bigint(20)  | YES  |     | NULL    |                |
| calledstationid      | varchar(50) | NO   |     |         |                |
| callingstationid     | varchar(50) | NO   |     |         |                |
| acctterminatecause   | varchar(32) | NO   |     |         |                |
| servicetype          | varchar(32) | YES  |     | NULL    |                |
| framedprotocol       | varchar(32) | YES  |     | NULL    |                |
| framedipaddress      | varchar(15) | NO   | MUL |         |                |
| acctstartdelay       | int(12)     | YES  |     | NULL    |                |
| acctstopdelay        | int(12)     | YES  |     | NULL    |                |
| xascendsessionsvrkey | varchar(10) | YES  |     | NULL    |                |
+----------------------+-------------+------+-----+---------+----------------+

mysql> show session variables like '%isol%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

带宽计算查询

SELECT sum(acctinputoctets),sum(acctoutputoctets) from radacct 
where username='davidjohnsoon' and acctstarttime 
between  '2015-10-11 14:10:17' and '2016-08-22 14:53:00'

mysql> show full processlist;

上插入/更新查询
INSERT INTO radacct (acctsessionid, acctuniqueid, username,         realm, nasipaddress, nasportid, nasporttype, acctstarttime, acctstoptime,acctsessiontime, acctauthentic, connectinfo_start, connectinfo_stop, acctinputoctets, acctoutputoctets, calledstationid, callingstationid, acctterminatecause,servicetype, framedprotocol, framedipaddress, acctstartdelay, acctstopdelay)  
VALUES  ('260204248', 'a5b889ad247a514b', 'johnson',  '','100.44.44.44', '297797794', 'Ethernet',  DATE_SUB('2016-08-23 13:02:50',INTERVAL (1 + 0) SECOND), '2016-08-23 13:02:50', '1', 'RADIUS', '',              '', '0' << 32 | '0', '0' << 32  | '0','','90:61:0c:1a:94:96','User-Error','Framed-User', 'PPP', '','0', '0') 

1 个答案:

答案 0 :(得分:1)

RC和RR之间的差异是:选择结果将在一个事务中改变或不改变;

所以在RR中,如果发生更新查询,结果在一个事务中不会改变;

RR中的例子:

会话1:10:00:00-开启交易;

会话1:10:00:00-执行带宽计算查询;

会话1:10:00:08-完成带宽计算查询,结果= 100;

会话2:10:00:05-更新查询;

会话1:10:00:09-执行带宽计算查询;

会话1:10:00:17-完成带宽计算查询,结果= 100;

会话1:10:00:18-结束交易;

但是对于RC,示例中的第二个带宽计算查询将得到不同的结果,可能是101;

如果在执行带宽计算查询时执行更新查询,则不会发生任何更改;

我的意见: 在这种情况下,RC和RR都可以;