當我們對MySQL進行分表操作后,將不能依賴MySQL的自動增量來產生唯一ID了,因為數據已經分散到多個表中。
應盡量避免使用自增IP來做為主鍵,為數據庫分表操作帶來極大的不便。
在postgreSQL、oracle、db2數據庫中有一個特殊的特性---sequence。 任何時候數據庫可以根據當前表中的記錄數大小和步長來獲取到該表下一條記錄數。然而,MySQL是沒有這種序列對象的。
可以通過下面的方法來實現sequence特性產生唯一ID:
1. 通過MySQL表生成ID
對于插入也就是insert操作,首先就是獲取唯一的id了,就需要一個表來專門創建id,插入一條記錄,并獲取最后插入的ID。代碼如下:
1
2
3
|
CREATE TABLE `ttlsa_com`.`create_id` ( `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE = MYISAM |
也就是說,當我們需要插入數據的時候,必須由這個表來產生id值,我的php代碼的方法如下:
1
2
3
4
5
6
7
|
<?php function get_AI_ID() { $sql = "insert into create_id (id) values('')" ; $this ->db->query( $sql ); return $this ->db->insertID(); } ?> |
這種方法效果很好,但是在高并發情況下,MySQL的AUTO_INCREMENT將導致整個數據庫慢。如果存在自增字段,MySQL會維護一個自增 鎖,innodb會在內存里保存一個計數器來記錄auto_increment值,當插入一個新行數據時,就會用一個表鎖來鎖住這個計數器,直到插入結 束。如果是一行一行的插入是沒有問題的,但是在高并發情況下,那就悲催了,表鎖會引起SQL阻塞,極大的影響性能,還可能會達到 max_connections值。
innodb_autoinc_lock_mode:可以設定3個值:0、1、2
0:traditonal (每次都會產生表鎖)
1:consecutive (默認,可預判行數時使用新方式,不可時使用表鎖,對于simple insert會獲得批量的鎖,保證連續插入)
2:interleaved (不會鎖表,來一個處理一個,并發最高)
對于myisam表引擎是traditional,每次都會進行表鎖的。
2. 通過redis生成ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
function get_next_autoincrement_waitlock($timeout = 60){ $count = $timeout > 0 ? $timeout : 60; while ($r->get( "serial:lock" )){ $count++; sleep(1); if ($count > 10) return false ; } return true ; } function get_next_autoincrement($timeout = 60){ // first check if we are locked... if (get_next_autoincrement_waitlock($timeout) == false ) return 0; $id = $r->incr( "serial" ); if ( $id > 1 ) return $id; // if ID == 1, we assume we do not have "serial" key... // first we need to get lock. if ($r->setnx( "serial:lock" ), 1){ $r->expire( "serial:lock" , 60 * 5); // get max(id) from database. $id = select_db_query( "select max(id) from user_posts" ); // or alternatively: // select id from user_posts order by id desc limit 1 // increase it $id++; // update Redis key $r->set( "serial" , $id); // release the lock $r->del( "serial:lock" ); return $id; } // can not get lock. return 0; } $r = new Redis(); $r->connect( "127.0.0.1" , "6379" ); $id = get_next_autoincrement(); if ($id){ $sql = "insert into user_posts(id,user,message)values($id,'$user','$message')" $data = exec_db_query($sql); } |
3. 隊列方式
其實這也算是上面的一個解說
使用隊列服務,如redis、memcacheq等等,將一定量的ID預分配在一個隊列里,每次插入操作,先從隊列中獲取一個ID,若插入失敗的話,將該ID再次添加到隊列中,同時監控隊列數量,當小于閥值時,自動向隊列中添加元素。
這種方式可以有規劃的對ID進行分配,還會帶來經濟效應,比如QQ號碼,各種靚號,明碼標價。如網站的userid, 允許uid登陸,推出各種靚號,明碼標價,對于普通的ID打亂后再隨機分配。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php class common { private $r ; function construct() { $this ->__construct(); } public function __construct(){ $this ->r= new Redis(); $this ->r->connect( '127.0.0.1' , 6379); } function set_queue_id( $ids ){ if ( is_array ( $ids ) && isset( $ids )){ foreach ( $ids as $id ){ $this ->r->LPUSH( 'next_autoincrement' , $id ); } } } function get_next_autoincrement(){ return $this ->r->LPOP( 'next_autoincrement' ); } } $createid = array (); while ( count ( $createid )<20){ $num =rand(1000,4000); if (!in_array( $num , $createid )) $createid []= $num ; } $id = new common(); $id ->set_queue_id( $createid ); var_dump( $id ->get_next_autoincrement()); |
監控隊列數量,并自動補充隊列和取到id但并沒有使用
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。