苹果14pro max没有塑封膜,iphone14全系列参数对比 苹果14防水吗 -爱游戏平台

苗坤旺离型膜

鐩綍

涓€銆丼quid 浠g悊鏈嶅姟鍣?銆佷唬鐞嗙殑宸ヤ綔鏈哄埗2銆佷唬鐞嗘湇鍔″櫒鐨勬蹇靛強鍏朵綔鐢?銆丼quid 浠g悊鐨勭被鍨?/p>

浜屻€佸畨瑁?squid 鏈嶅姟1锛庣紪璇戝畨瑁?squid2锛庝慨鏀?squid 鐨勯厤缃枃浠?锛嶴quid 鐨勮繍琛屾帶鍒?銆佹鏌ラ厤缃枃浠惰娉曟槸鍚︽纭?銆佸惎鍔?squid锛岀涓€娆″惎鍔?squid 鏈嶅姟鏃讹紝浼氳嚜鍔ㄥ垵濮嬪寲缂撳瓨鐩綍

4锛巗upervisor浣跨敤璇﹁в5銆乻upervisor甯哥敤鍛戒护6銆佹瀯寤轰紶缁熶唬鐞嗘湇鍔″櫒7銆佽缃畐eb鏈嶅姟鍣?銆佸鎴风閰嶇疆锛堟坊鍔犱唬鐞嗭級

涓夈€佹瀯寤洪€忔槑浠g悊鏈嶅姟鍣?銆佺幆澧冮厤缃?銆佸湪浠g悊鏈嶅姟鍣ㄤ笂娣诲姞缃戝崱3銆丼quid鏈嶅姟鍣ㄩ厤缃?銆佸紑鍚矾鐢辫浆鍙戯紝瀹炵幇鏈満涓笉鍚岀綉娈电殑鍦板潃杞彂5銆亀eb鏈嶅姟鍣ㄩ厤缃?銆佸鎴风鍏抽棴浠g悊7銆佹煡鐪?squid 璁块棶鏃ュ織鐨勬柊澧炶褰?銆乧lient1 涓绘満璁剧疆澶栫綉璁块棶

鍥涖€丄cl璁块棶鎺у埗1锛庡畾涔夎闂帶鍒跺垪琛?銆佸惎鍔ㄥ璞″垪琛ㄧ鐞?/p>

浜斻€丼quid 鏃ュ織鍒嗘瀽1銆佸畨瑁呭浘鍍忓鐞嗚蒋浠跺寘2銆佹坊鍔犱笉璁″叆绔欑偣鏂囦欢锛屾坊鍔犵殑鍩熷悕灏嗕笉琚樉绀哄湪鎺掑簭涓?銆侀獙璇?銆佽繍琛?銆佹坊鍔犺鍒掍换鍔★紝鎵ц姣忓ぉ鐢熸垚鎶ュ憡

鍏€丼quid 鍙嶅悜浠g悊1銆佸伐浣滄満鍒?銆佹竻绌轰箣鍓嶉€忔槑妯″紡閰嶇疆鐨?iptables 瑙勫垯3銆佸悗绔妭鐐规湇鍔″櫒璁剧疆4銆佸鎴锋満鐨勫煙鍚嶆槧灏勯厤缃?/p>

涓€銆丼quid 浠g悊鏈嶅姟鍣?/p>

squid 涓昏鎻愪緵缂撳瓨鍔犻€熴€佸簲鐢ㄥ眰杩囨护鎺у埗鐨勫姛鑳姐€?/p>

1銆佷唬鐞嗙殑宸ヤ綔鏈哄埗

1锛庝唬鏇垮鎴锋満鍚戠綉绔欒姹傛暟鎹紝浠庤€屽彲浠ラ殣钘忕敤鎴风殑鐪熷疄ip鍦板潃銆?/p>

2锛庡皢鑾峰緱鐨勭綉椤垫暟鎹紙闈欐€?web 鍏冪礌锛変繚瀛樺埌缂撳瓨涓苟鍙戦€佺粰瀹㈡埛鏈猴紝浠ヤ究涓嬫璇锋眰鐩稿悓鐨勬暟鎹椂蹇€熷搷搴斻€?/p>

2銆佷唬鐞嗘湇鍔″櫒鐨勬蹇靛強鍏朵綔鐢?/p>

浠g悊鏈嶅姟鍣ㄦ槸涓€涓綅浜庡鎴风鍜屽師濮嬶紙璧勬簮锛夋湇鍔″櫒涔嬮棿鐨勬湇鍔″櫒锛屼负浜嗕粠鍘熷鏈嶅姟鍣ㄥ彇寰楀唴瀹癸紝瀹㈡埛绔悜浠g悊鏈嶅姟鍣ㄥ彂閫佷竴涓姹傚苟鎸囧畾鐩爣鍘熷鏈嶅姟鍣紝鐒跺悗浠g悊鏈嶅姟鍣ㄥ悜鍘熷鏈嶅姟鍣ㄨ浆浜よ姹傚苟灏嗚幏寰楃殑鍐呭杩斿洖缁欏鎴风銆?/p>

缂撳瓨浠g悊瀵逛簬web鑷冲叧閲嶈锛屽挨鍏跺浜庡ぇ鍨嬮珮璐熻浇web绔欑偣銆傜紦瀛樺彲浣滀负鎬ц兘浼樺寲鐨勪竴涓噸瑕佹墜娈碉紝鍙互鏋佸ぇ鍑忚交鍚庣鏈嶅姟鍣ㄧ殑璐熻浇銆傞€氬父瀵逛簬闈欐€佽祫婧愶紝鍗宠緝灏戠粡甯告洿鏂扮殑璧勬簮锛屽鍥剧墖锛宑ss鎴杍s绛夎繘琛岀紦瀛橈紝浠庤€屽湪姣忔鍒锋柊娴忚鍣ㄧ殑鏃跺€欙紝涓嶇敤閲嶆柊璇锋眰锛岃€屾槸浠庣紦瀛橀噷闈㈣鍙栵紝杩欐牱灏卞彲浠ュ噺杞绘湇鍔″櫒鐨勫帇鍔涖€?/p>

鍏朵富瑕佷綔鐢ㄦ湁锛?/p>

璧勬簮鑾峰彇锛氫唬鏇垮鎴风瀹炵幇浠庡師濮嬫湇鍔″櫒鐨勮祫婧愯幏鍙栵紱

鍔犻€熻闂細浠g悊鏈嶅姟鍣ㄥ彲鑳界鍘熷鏈嶅姟鍣ㄦ洿杩戯紝浠庤€岃捣鍒颁竴瀹氱殑鍔犻€熶綔鐢紱

缂撳瓨浣滅敤锛氫唬鐞嗘湇鍔″櫒淇濆瓨浠庡師濮嬫湇鍔″櫒鎵€鑾峰彇鐨勮祫婧愶紝浠庤€屽疄鐜板鎴风蹇€熺殑鑾峰彇锛?/p>

闅愯棌鐪熷疄鍦板潃锛氫唬鐞嗘湇鍔″櫒浠f浛瀹㈡埛绔幓鑾峰彇鍘熷鏈嶅姟鍣ㄨ祫婧愶紝浠庤€岄殣钘忓鎴风鐪熷疄淇℃伅銆?/p>

3銆丼quid 浠g悊鐨勭被鍨?/p>

1銆佷紶缁熶唬鐞嗭細閫傜敤浜嶪nternet 姝e悜浠g悊锛岄渶鍦ㄥ鎴锋満鎸囧畾浠g悊鏈嶅姟鍣ㄧ殑鍦板潃鍜岀鍙c€?/p>

2銆侀€忔槑浠g悊锛氬鎴锋満涓嶉渶鎸囧畾浠g悊鏈嶅姟鍣ㄧ殑鍦板潃鍜岀鍙o紝鑰屾槸閫氳繃榛樿璺敱銆侀槻鐏绛栫暐灏哤eb璁块棶閲嶅畾鍚戠粰浠g悊鏈嶅姟鍣ㄥ鐞嗐€?

3銆佸弽鍚戜唬鐞嗭細濡傛灉 squid 鍙嶅悜浠g悊鏈嶅姟鍣ㄤ腑缂撳瓨浜嗚璇锋眰鐨勮祫婧愶紝鍒欏皢璇ヨ姹傜殑璧勬簮鐩存帴杩斿洖缁欏鎴风锛涘惁鍒欏弽鍚戜唬鐞嗘湇鍔″櫒灏嗗悜鍚庡彴鐨?web 鏈嶅姟鍣ㄨ姹傝祫婧愶紝鐒跺悗灏嗚姹傜殑搴旂瓟杩斿洖缁欏鎴风锛屽悓鏃朵篃灏嗚搴旂瓟缂撳瓨鍦ㄦ湰鍦帮紝渚涗笅涓€涓姹傝€呬娇鐢ㄣ€?/p>

浜屻€佸畨瑁?squid 鏈嶅姟

鍏抽棴鏈嶅姟鍣ㄧ殑闃茬伀澧?/p>

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

1锛庣紪璇戝畨瑁?squid

灏嗘簮鐮佸寘鏀惧叆opt鐩綍涓嬶紝骞惰繘琛岃в鍘嬫枃浠?/p>

yum -y install gcc gcc-c make

tar zxvf squid-3.5.28.tar.gz -c /opt/

cd /opt/squid-3.5.28

./configure --prefix=/usr/local/squid \ #鎸囧畾瀹夎鐩綍璺緞

--sysconfdir=/etc \ #鎸囧畾閰嶇疆鏂囦欢璺緞

--enable-arp-acl \ #mac鍦板潃绠℃帶锛岄槻姝㈠鎴风浣跨敤ip娆洪獥

--enable-linux-netfilter \ #浣跨敤鍐呮牳杩囨护

--enable-linux-tproxy \ #鏀寔閫忔槑妯″紡

--enable-async-io=100 \ #寮傛io锛屾彁鍗囧瓨鍌ㄦ€ц兘

--enable-err-language="simplify_chinese" \ #閿欒淇℃伅鐨勬樉绀鸿瑷€

--enable-underscore \ #鍏佽url涓湁涓嬪垝绾?/p>

--disable-poll \ #鍏抽棴榛樿浣跨敤 poll 妯″紡

--enable-epoll \ #寮€鍚痚poll妯″紡鎻愬崌鎬ц兘

--enable-gnuregex #浣跨敤gnu姝e垯琛ㄨ揪寮?/p>

----------------------------------------------------------------------------------------------------------

./configure --prefix=/usr/local/squid \

--sysconfdir=/etc \

--enable-arp-acl \

--enable-linux-netfilter \

--enable-linux-tproxy \

--enable-async-io=100 \

--enable-err-language="simplify_chinese" \

--enable-underscore \

--disable-poll \

--enable-epoll \

--enable-gnuregex

杩涜缂栬瘧瀹夎

make && make install

璁剧疆杞繛鎺ュ垱寤虹敤鎴峰拰鍔犳潈

ln -s /usr/local/squid/sbin/* /usr/local/sbin/

ln -s /usr/local/squid/bin/* /usr/local/bin/

useradd -m -s /sbin/nologin squid

chown -r squid:squid /usr/local/squid/var/ #姝ょ洰褰曠敤浜庡瓨鏀剧紦瀛樻枃浠?/p>

2锛庝慨鏀?squid 鐨勯厤缃枃浠?/p>

vim /etc/squid.conf

......

--56琛?-鎻掑叆

http_access allow all #鏀惧湪 http_access deny all 涔嬪墠锛屽厑璁镐换鎰忓鎴锋満浣跨敤浠g悊鏈嶅姟锛屾帶鍒惰鍒欒嚜涓婅€屼笅鍖归厤

http_access deny all

http_port 3128 #鐢ㄦ潵鎸囧畾浠g悊鏈嶅姟鐩戝惉鐨勫湴鍧€鍜岀鍙?榛樿鐨勭鍙e彿涓?3128)

--61琛?-鎻掑叆

cache_effective_user squid #娣诲姞锛屾寚瀹氱▼搴忕敤鎴凤紝鐢ㄦ潵璁剧疆鍒濆鍖栥€佽繍琛屾椂缂撳瓨鐨勮处鍙?鍚﹀垯鍚姩涓嶆垚鍔?/p>

cache_effective_group squid #娣诲姞锛屾寚瀹氳处鍙峰熀鏈粍

coredump_dir /usr/local/squid/var/cache/squid #鎸囧畾缂撳瓨鏂囦欢鐩綍

3锛嶴quid 鐨勮繍琛屾帶鍒?/p>

1銆佹鏌ラ厤缃枃浠惰娉曟槸鍚︽纭?/p>

squid -k parse

2銆佸惎鍔?squid锛岀涓€娆″惎鍔?squid 鏈嶅姟鏃讹紝浼氳嚜鍔ㄥ垵濮嬪寲缂撳瓨鐩綍

squid -z #-z 閫夐」鐢ㄦ潵鍒濆鍖栫紦瀛樼洰褰?/p>

squid #鍚姩 squid 鏈嶅姟

netstat -anpt | grep "squid"

4锛巗upervisor浣跨敤璇﹁в

绠€浠?/p>

浣跨敤鏂囨。锛歨ttp://supervisord.org/

supervisor鏄疨ython寮€鍙戠殑c/s鏈嶅姟锛屾槸linux绯荤粺涓嬬殑杩涚▼绠$悊宸ュ叿銆?/p>

鍙互鐩戝惉銆佸惎鍔ㄣ€佸仠姝€侀噸鍚竴涓垨澶氫釜杩涚▼

鐢╯upervisor绠$悊鐨勮繘绋嬶紝褰撲竴涓繘绋嬫剰澶栬鏉€姝伙紝supervisor鐩戝惉鍒拌繘绋嬫鍚庯紝浼氳嚜鍔ㄥ皢瀹冮噸鍚紝寰堟柟渚跨殑鍋氬埌杩涚▼鐨勮嚜鍔ㄦ仮澶嶇殑鍔熻兘锛屼笉鍦ㄩ渶瑕佽嚜宸卞啓鑴氭湰鏉ユ帶鍒?/p>

[unix_http_server]

file=/var/run/supervisor.sock ;unix socket 鏂囦欢锛宻upervisorctl 浼氫娇鐢?/p>

;chmod=0700 ;socket鏂囦欢鐨刴ode锛岄粯璁ゆ槸0700

chmod=0766

;chown=nobody:nogroup ;socket鏂囦欢鐨刼wner锛屾牸寮忥細uid:gid

;[inet_http_server] ;http鏈嶅姟鍣紝鎻愪緵web绠$悊鐣岄潰

;port=0.0.0.0:9001 ;web绠$悊鍚庡彴杩愯鐨処p鍜岀鍙o紝濡傛灉寮€鏀惧埌鍏綉锛岄渶瑕佹敞鎰忓畨鍏ㄦ€?/p>

;username=user ;鐧诲綍绠$悊鍚庡彴鐨勭敤鎴峰悕

;password=123 ;鐧诲綍绠$悊鍚庡彴鐨勫瘑鐮?/p>

[supervisord]

logfile=/var/log/supervisord.log ;鏃ュ織鏂囦欢锛岄粯璁ゆ槸 $cwd/supervisord.log

logfile_maxbytes=50mb ;鏃ュ織鏂囦欢澶у皬锛岃秴鍑轰細rotate锛岄粯璁?50mb锛屽鏋滆鎴?锛岃〃绀轰笉闄愬埗澶у皬

logfile_backups=10 ;鏃ュ織鏂囦欢淇濈暀澶囦唤鏁伴噺榛樿10锛岃涓?琛ㄧず涓嶅浠?/p>

loglevel=info ;鏃ュ織绾у埆锛岄粯璁nfo锛屽叾瀹? debug,warn,trace

pidfile=/var/run/supervisord.pid ;pid 鏂囦欢

nodaemon=false ;鏄惁鍦ㄥ墠鍙板惎鍔紝榛樿鏄痜alse锛屽嵆浠?daemon 鐨勬柟寮忓惎鍔?/p>

minfds=1024 ;鍙互鎵撳紑鐨勬枃浠舵弿杩扮鐨勬渶灏忓€硷紝榛樿 1024

minprocs=200 ;鍙互鎵撳紑鐨勮繘绋嬫暟鐨勬渶灏忓€硷紝榛樿 200

[supervisorctl]

serverurl=unix:///tmp/supervisor.sock ;閫氳繃unix socket杩炴帴supervisord锛岃矾寰勪笌unix_http_server閮ㄥ垎鐨刦ile涓€鑷?/p>

;serverurl=http://127.0.0.1:9001 ;閫氳繃http鐨勬柟寮忚繛鎺upervisord

; [program:xx]鏄绠$悊鐨勮繘绋嬮厤缃弬鏁帮紝xx鏄繘绋嬬殑鍚嶇о

[program:tomcat]

command=/usr/local/apache-tomcat-8.0.35/bin/catalina.sh run ;绋嬪簭鍚姩鍛戒护

autostart=true ;鍦╯upervisord鍚姩鐨勬椂鍊欎篃鑷姩鍚姩

startsecs=10 ;鍚姩10绉掑悗娌℃湁寮傚父閫€鍑猴紝灏辫〃绀鸿繘绋嬫甯稿惎鍔ㄤ簡锛岄粯璁や负1绉?/p>

autorestart=true ;绋嬪簭閫€鍑哄悗鑷姩閲嶅惎,鍙€夊€硷細[unexpected,true,false]锛岄粯璁や负unexpected锛岃〃绀鸿繘绋嬫剰澶栨潃姝诲悗鎵嶉噸鍚?/p>

startretries=3 ;鍚姩澶辫触鑷姩閲嶈瘯娆℃暟锛岄粯璁ゆ槸3

user=tomcat ;鐢ㄥ摢涓敤鎴峰惎鍔ㄨ繘绋嬶紝榛樿鏄痳oot

priority=999 ;杩涚▼鍚姩浼樺厛绾э紝榛樿999锛屽€煎皬鐨勪紭鍏堝惎鍔?/p>

redirect_stderr=true ;鎶妔tderr閲嶅畾鍚戝埌stdout锛岄粯璁alse

stdout_logfile_maxbytes=200mb ;stdout 鏃ュ織鏂囦欢澶у皬锛岄粯璁?0mb

stdout_logfile_backups=20 ;stdout 鏃ュ織鏂囦欢澶囦唤鏁帮紝榛樿鏄?0

; stdout 鏃ュ織鏂囦欢锛岄渶瑕佹敞鎰忓綋鎸囧畾鐩綍涓嶅瓨鍦ㄦ椂鏃犳硶姝e父鍚姩锛屾墍浠ラ渶瑕佹墜鍔ㄥ垱寤虹洰褰曪紙supervisord 浼氳嚜鍔ㄥ垱寤烘棩蹇楁枃浠讹級

stdout_logfile=/var/log/supervisor/logs/catalina.out

stopasgroup=false ;榛樿涓篺alse锛岃繘绋嬭鏉€姝绘椂锛屾槸鍚﹀悜杩欎釜杩涚▼缁勫彂閫乻top淇″彿锛屽寘鎷瓙杩涚▼

killasgroup=false ;榛樿涓篺alse锛屽悜杩涚▼缁勫彂閫乲ill淇″彿锛屽寘鎷瓙杩涚▼

;鍖呭惈鍏跺畠閰嶇疆鏂囦欢

[include]

# files = /etc/supervisord.d/*.ini ;鍙互鎸囧畾涓€涓垨澶氫釜浠?ini缁撴潫鐨勯厤缃枃浠?/p>

files = /etc/supervisord.d/*.conf ;涔熷彲浠ュ啓鎴愪互.conf缁撳熬鐨勯厤缃枃浠?/p>

; sample supervisor config file.

[unix_http_server]

file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)

;chmod=0700 ; sockef file mode (default 0700)

chmod=0766

;chown=nobody:nogroup ; socket file uid:gid owner

;username=user ; (default is no username (open server))

;password=123 ; (default is no password (open server))

;[inet_http_server] ; inet (tcp) server disabled by default

;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)

;username=user ; (default is no username (open server))

;password=123 ; (default is no password (open server))

[supervisord]

logfile=/var/log/supervisor/supervisord.log ; (main log file;default $cwd/supervisord.log)

logfile_maxbytes=50mb ; (max main logfile bytes b4 rotation;default 50mb)

logfile_backups=10 ; (num of main logfile rotation backups;default 10)

loglevel=info ; (log level;default info; others: debug,warn,trace)

pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)

nodaemon=false ; (start in foreground if true;default false)

minfds=1024 ; (min. avail startup file descriptors;default 1024)

minprocs=200 ; (min. avail process descriptors;default 200)

;umask=022 ; (process file creation umask;default 022)

;user=chrism ; (default is current user, required if root)

;identifier=supervisor ; (supervisord identifier, default is 'supervisor')

;directory=/tmp ; (default is not to cd during start)

;nocleanup=true ; (don't clean up tempfiles at start;default false)

;childlogdir=/tmp ; ('auto' child log dir, default $temp)

;environment=key=value ; (key value pairs to add to environment)

;strip_ansi=false ; (strip ansi escape codes in logs; def. false)

; the below section must remain in the config file for rpc

; (supervisorctl/web interface) to work, additional interfaces may be

; added by defining them in separate rpcinterface: sections

[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// url for a unix socket

;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket

;username=chris ; should be same as http_username if set

;password=123 ; should be same as http_password if set

;prompt=mysupervisor ; cmd line prompt (default "supervisor")

;history_file=~/.sc_history ; use readline history if available

; the below sample program section shows all possible program subsection values,

; create one or more 'real' program: sections to be able to control them under

; supervisor.

;[program:theprogramname]

;command=/bin/cat ; the program (relative uses path, can take args)

;process_name=%(program_name)s ; process_name expr (default %(program_name)s)

;numprocs=1 ; number of processes copies to start (def 1)

;directory=/tmp ; directory to cwd to before exec (def no cwd)

;umask=022 ; umask for process (default none)

;priority=999 ; the relative start priority (default 999)

;autostart=true ; start at supervisord start (default: true)

;autorestart=true ; retstart at unexpected quit (default: true)

;startsecs=10 ; number of secs prog must stay running (def. 1)

;startretries=3 ; max # of serial start failures (default 3)

;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)

;stopsignal=quit ; signal used to kill process (default term)

;stopwaitsecs=10 ; max num secs to wait b4 sigkill (default 10)

;user=chrism ; setuid to this unix account to run the program

;redirect_stderr=true ; redirect proc stderr to stdout (default false)

;stdout_logfile=/a/path ; stdout log path, none for none; default auto

;stdout_logfile_maxbytes=1mb ; max # logfile bytes b4 rotation (default 50mb)

;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)

;stdout_capture_maxbytes=1mb ; number of bytes in 'capturemode' (default 0)

;stdout_events_enabled=false ; emit events on stdout writes (default false)

;stderr_logfile=/a/path ; stderr log path, none for none; default auto

;stderr_logfile_maxbytes=1mb ; max # logfile bytes b4 rotation (default 50mb)

;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)

;stderr_capture_maxbytes=1mb ; number of bytes in 'capturemode' (default 0)

;stderr_events_enabled=false ; emit events on stderr writes (default false)

;environment=a=1,b=2 ; process environment additions (def no adds)

;serverurl=auto ; override serverurl computation (childutils)

; the below sample eventlistener section shows all possible

; eventlistener subsection values, create one or more 'real'

; eventlistener: sections to be able to handle event notifications

; sent by supervisor.

;[eventlistener:theeventlistenername]

;command=/bin/eventlistener ; the program (relative uses path, can take args)

;process_name=%(program_name)s ; process_name expr (default %(program_name)s)

;numprocs=1 ; number of processes copies to start (def 1)

;events=event ; event notif. types to subscribe to (req'd)

;buffer_size=10 ; event buffer queue size (default 10)

;directory=/tmp ; directory to cwd to before exec (def no cwd)

;umask=022 ; umask for process (default none)

;priority=-1 ; the relative start priority (default -1)

;autostart=true ; start at supervisord start (default: true)

;autorestart=unexpected ; restart at unexpected quit (default: unexpected)

;startsecs=10 ; number of secs prog must stay running (def. 1)

;startretries=3 ; max # of serial start failures (default 3)

;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)

;stopsignal=quit ; signal used to kill process (default term)

;stopwaitsecs=10 ; max num secs to wait b4 sigkill (default 10)

;user=chrism ; setuid to this unix account to run the program

;redirect_stderr=true ; redirect proc stderr to stdout (default false)

;stdout_logfile=/a/path ; stdout log path, none for none; default auto

;stdout_logfile_maxbytes=1mb ; max # logfile bytes b4 rotation (default 50mb)

;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)

;stdout_events_enabled=false ; emit events on stdout writes (default false)

;stderr_logfile=/a/path ; stderr log path, none for none; default auto

;stderr_logfile_maxbytes=1mb ; max # logfile bytes b4 rotation (default 50mb)

;stderr_logfile_backups ; # of stderr logfile backups (default 10)

;stderr_events_enabled=false ; emit events on stderr writes (default false)

;environment=a=1,b=2 ; process environment additions

;serverurl=auto ; override serverurl computation (childutils)

; the below sample group section shows all possible group values,

; create one or more 'real' group: sections to create "heterogeneous"

; process groups.

;[group:thegroupname]

;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions

;priority=999 ; the relative start priority (default 999)

; the [include] section can just contain the "files" setting. this

; setting can list multiple files (separated by whitespace or

; newlines). it can also contain wildcards. the filenames are

; interpreted as relative to this file. included files *cannot*

; include files themselves.

[include]

;files = supervisord.d/*.ini

files = /etc/supervisord.d/*.conf

杩涚▼绠$悊閰嶇疆鍙傛暟锛屽簲璇ユ瘡涓繘绋嬪啓涓€涓厤缃枃浠舵斁鍦╥nclude鎸囧畾鐨勭洰褰曚笅鍖呭惈杩泂upervisord.conf鏂囦欢涓€?/p>

[program:squid]

command=/usr/local/squid/sbin/squid -n -d1

stdout_logfile=/var/log/supervisor/logs/run.log

stderr_logfile=/var/log/supervisor/logs/error.log

autostart=true

startsecs=10

autorestart=true

startretries=3

user=squid

priority=998

redirect_stderr=true

stdout_logfile_maxbytes=200mb

stdout_logfile_backups=20

stopasgroup=true

killasgroup=true

redirect_stderr=true

娉細supervisor涓嶈兘鐩戞帶鍚庡彴杩涚▼锛宑ommand涓嶈兘涓哄悗鍙拌繍琛屽懡浠?/p>

supervisor鏈嶅姟鍚姩

mkdir -p /var/log/supervisor/logs

systemctl start supervisord

systemctl enable supervisord

supervisord -c /etc/supervisor.conf

鍚姩鏈嶅姟鍓嶈鍏堝皢鎵€鏈夌殑绔彛鍙峰垹闄わ紝鍦ㄩ噸鏂板惎鍔?

5銆乻upervisor甯哥敤鍛戒护

supervisorctl鏄痵upervisord鐨勫懡浠よ宸ュ叿

- supervisorctl status 鏌ョ湅鎵€鏈夎繘绋嬬殑鐘舵€?/p>

- supervisorctl stop 鍋滄杩涚▼

- supervisorctl start 鍚姩杩涚▼

- supervisorctl restart 閲嶅惎杩涚▼

- supervisorctl update 閰嶇疆鏂囦欢淇敼鍚庡彲浠ヤ娇鐢ㄨ鍛戒护鍔犺浇鏂扮殑閰嶇疆

- supervisorctl reload 閲嶆柊鍚姩閰嶇疆涓殑鎵€鏈夌▼搴?/p>

supervisorctl status squid #nginx鐘舵€?/p>

supervisorctl stop squid #鍋滄nginx

supervisorctl start squid #鍚姩nginx

supervisorctl restart squid #閲嶅惎nginx

supervisorctl reload squid #閲嶈浇nginx

6銆佹瀯寤轰紶缁熶唬鐞嗘湇鍔″櫒

vim /etc/squid.conf

......

http_access allow all

http_access deny all

http_port 3128

cache_effective_user squid

cache_effective_group squid

--63琛?-鎻掑叆

cache_mem 1024 mb #鎸囧畾缂撳瓨鍔熻兘鎵€浣跨敤鐨勫唴瀛樼┖闂村ぇ灏忥紝渚夸簬淇濇寔璁块棶杈冮绻佺殑web瀵硅薄锛屽閲忔渶濂戒负4鐨勫€嶆暟锛屽崟浣嶄负mb锛屼竴鑸涓虹墿鐞嗗唴瀛樼殑1/3

cache_swap_high 95 #cache鐩綍浣跨敤閲忓ぇ浜?5%鏃讹紝寮€濮嬫竻鐞嗘棫鐨刢ache

cache_swap_low 90 #cache鐩綍娓呯悊鍒?0%鏃跺仠姝?/p>

reply_body_max_size 100 mb #鍏佽鐢ㄦ埛涓嬭浇鐨勬渶澶ф枃浠跺ぇ灏忥紝浠ュ瓧鑺備负鍗曚綅锛屽綋涓嬭浇瓒呰繃鎸囧畾澶у皬鐨刉eb瀵硅薄鏃讹紝娴忚鍣ㄧ殑鎶ラ敊椤甸潰涓細鍑虹幇鈥滆姹傛垨璁块棶澶ぇ鈥濈殑鎻愮ず榛樿璁剧疆0琛ㄧず涓嶈繘琛岄檺鍒?/p>

maximum_object_size 512000 kb #璁剧疆squid纾佺洏缂撳瓨鏈€澶ф枃浠讹紝浠b涓哄崟浣嶏紝瓒呰繃鐨勬枃浠朵笉淇濆瓨鍒扮‖鐩橈紝鑰屾槸鐩存帴杞彂缁欑敤鎴?/p>

maximum_object_size_in_memory 512 kb #璁剧疆squid鍐呭瓨缂撳瓨鏈€澶ф枃浠讹紝瓒呰繃鐨勬枃浠朵笉淇濆瓨鍒板唴瀛?/p>

minimum_object_size 0 kb 璁剧疆squid纾佺洏缂撳瓨鏈€灏忔枃浠?/p>

閲嶆柊鍚姩鏈嶅姟

supervisorctl restart squid

鐢熶骇鐜涓繕闇€瑕佷慨鏀归槻鐏瑙勫垯

iptables -t filter -a input -p tcp --dport 3128 -j accept

7銆佽缃畐eb鏈嶅姟鍣?/p>

浣跨敤yum瀹夎nginx鏈嶅姟杩涜缃戦〉璁块棶

systemctl stop firewalld.service

setenforce 0

yum install -y nginx

鍦╪ginx缃戦〉椤甸潰涓婃坊鍔犵綉椤靛唴瀹?

8銆佸鎴风閰嶇疆锛堟坊鍔犱唬鐞嗭級

鍦ㄦ祻瑙堝櫒涓婅缃唬鐞嗘湇鍔″櫒鐨刬p鍦板潃鍜岀鍙e彿

鍦╪ginx鏈嶅姟鍣ㄤ笂鐨勬棩蹇楄繘琛屾煡鐪?

浣跨敤鍙﹀涓€鍙颁富鏈鸿繘琛屾煡鐪嬬綉椤?鍙︿竴鍙颁富鏈轰篃璁剧疆涓€涓嬩唬鐞?/p>

涓夈€佹瀯寤洪€忔槑浠g悊鏈嶅姟鍣?/p>

1銆佺幆澧冮厤缃?/p>

涓绘満鎿嶄綔绯荤粺ip鍦板潃宸ュ叿squid-servercentos7ens33锛?92.168.102.10銆?ens35锛?2.0.0.10squid-3.5.28.tar.gzwebcentos712.0.0.100nginxclient2centos7192.168.102.30鈥揷lient1windows192.168.102.120鈥斺€?/p>

2銆佸湪浠g悊鏈嶅姟鍣ㄤ笂娣诲姞缃戝崱

娣诲姞鎴愬姛

3銆丼quid鏈嶅姟鍣ㄩ厤缃?/p>

cd /etc/sysconfig/network-scripts/

cp ifcfg-ens33 ifcfg-ens35

vim ifcfg-ens35

systemctl restart network

ifconfig

#60琛屼慨鏀规坊鍔犳彁渚涘唴缃戞湇鍔$殑ip鍦板潃锛屽拰鏀寔閫忔槑浠g悊閫夐」 transparent

vim /etc/squid.conf

......

http_access allow all

http_access deny all

http_port 192.168.102.10:3128 transparent

supervisorctl restart squid

netstat -natp | grep 3128

淇敼瀹岄厤缃枃浠跺悗杩涜閲嶆柊鍚姩鏈嶅姟锛屾煡鐪嬫湇鍔℃湁鏃犲惎鍔?/p>

4銆佸紑鍚矾鐢辫浆鍙戯紝瀹炵幇鏈満涓笉鍚岀綉娈电殑鍦板潃杞彂

echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf

sysctl -p

#淇敼闃茬伀澧欒鍒?/p>

iptables -f

iptables -t nat -f

iptables -t nat -i prerouting -i ens33 -s 192.168.102.0/24 -p tcp --dport 80 -j redirect --to-ports 3128 #鐢ㄤ簬杞彂http鍗忚

iptables -t nat -i prerouting -i ens33 -s 192.168.102.0/24 -p tcp --dport 443 -j redirect --to 3128 #鐢ㄤ簬杞彂https鍗忚

iptables -i input -p tcp --dport 3128 -j accept

5銆亀eb鏈嶅姟鍣ㄩ厤缃?/p>

6銆佸鎴风鍏抽棴浠g悊

瀹㈡埛鏈烘祴璇曪紙192.168.102.120锛?/p>

鍏抽棴瀹㈡埛鏈虹殑娴忚鍣ㄤ箣鍓嶈缃殑浠g悊鏈嶅姟鍣ㄧ殑鍔熻兘鍚庤闂?http://12.0.0.100

璁块棶web1鏈嶅姟鍣?/p>

浣跨敤娴忚鍣ㄨ闂畐eb鐨刬p鍦板潃

7銆佹煡鐪?squid 璁块棶鏃ュ織鐨勬柊澧炶褰?/p>

tail -f /usr/local/squid/var/logs/access.log

#squid浠g悊鏈嶅姟鍣ㄥ垯鏄娴嬪埌瀹㈡埛鏈鸿闂洰鏍囩綉绔?/p>

1688449614.276 0 192.168.102.120 tcp_inm_hit/304 257 get http://12.0.0.100/test.html - hier_none/- text/html

8銆乧lient1 涓绘満璁剧疆澶栫綉璁块棶

浣跨敤192.168.102.30涓绘満杩涜璁块棶

鏌ョ湅 web 璁块棶鏃ュ織鐨勬柊澧炶褰曪紝鏄剧ず鐨勬槸鐢变唬鐞嗘湇鍔″櫒鐨勫缃戝彛浠f浛瀹㈡埛鏈哄湪璁块棶

tail -f access.log

鍥涖€丄cl璁块棶鎺у埗

鍦ㄩ厤缃枃浠?squid.conf 涓紝acl 璁块棶鎺у埗閫氳繃浠ヤ笅涓や釜姝ラ鏉ュ疄鐜?

锛?锛変娇鐢?acl 閰嶇疆椤瑰畾涔夐渶瑕佹帶鍒剁殑鏉′欢 瀛楀彿4

锛?锛夐€氳繃 http_access 閰嶇疆椤瑰宸插畾涔夌殑鍒楄〃鍋氣€滃厑璁糕€濇垨鈥滄嫆缁濃€濊闂殑鎺у埗銆?瀛楀彿4

1锛庡畾涔夎闂帶鍒跺垪琛?/p>

鏍煎紡

acl 鍒楄〃鍚嶇о 鍒楄〃绫诲瀷 鍒楄〃鍐呭 鈥?/p>

vim /etc/squid.conf

acl localhost src 192.168.80.10/32 #婧愬湴鍧€涓?192.168.80.10

acl mylan src 192.168.80.0/24 192.168.1.0/24 #瀹㈡埛鏈虹綉娈?/p>

acl destinationhost dst 192.168.80.13/32 #鐩爣鍦板潃涓?192.168.80.13

acl mc20 maxconn 20 #鏈€澶у苟鍙戣繛鎺?20

acl port port 21 #鐩爣绔彛 21

acl dmblock dstdomain .qq.com #鐩爣鍩燂紝鍖归厤鍩熷唴鎵€鏈夌珯鐐?/p>

acl burl url_regex -i ^rtsp:// ^emule:// #浠?rtsp://銆乪mule:// 寮€澶寸殑鐩爣 url鍦板潃锛?i琛ㄧず蹇界暐澶у皬鍐?/p>

acl purl urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$ #浠?.mp3銆?mp4銆?rmvb 缁撳熬鐨勭洰鏍?url 璺緞

acl worktime time mtwhf 08:30-17:30 #鏃堕棿涓哄懆涓€鑷冲懆浜?8:30~17:30锛屸€淢twhf鈥濅负姣忎釜鏄熸湡鐨勮嫳鏂囬瀛楁瘝

淇敼瀹屽悗杩涜閲嶆柊鍚姩鏈嶅姟

璁剧疆鍚庡氨鍙互鐪嬭192.168.102.30涓绘満涓嶈兘璁块棶浜嗭紝鍏朵綑涓绘満鑳借闂?

2銆佸惎鍔ㄥ璞″垪琛ㄧ鐞?/p>

mkdir /etc/squid

vim /etc/squid/dest.list

acl destinationhost dst "/etc/squid/dest.list"

http_access deny(鎴朼llow) destinationhost

#娉ㄦ剰锛屽鏋滄槸鎷掔粷鍒楄〃锛岄渶瑕佹斁鍦╤ttp_access allow all鍓嶉潰

娴忚鍣ㄨ闂甒eb鏈嶅姟鍣?http://192.168.102.50 锛屾樉绀鸿璁块棶琚嫆缁濄€?/p>

浜斻€丼quid 鏃ュ織鍒嗘瀽

1銆佸畨瑁呭浘鍍忓鐞嗚蒋浠跺寘

yum install -y pcre-devel gd gd-devel

mkdir /usr/local/sarg

tar zxvf sarg-2.3.7.tar.gz -c /opt/

cd /opt/sarg-2.3.7

./configure --prefix=/usr/local/sarg \

--sysconfdir=/etc/sarg \ #閰嶇疆鏂囦欢鐩綍锛岄粯璁ゆ槸/usr/local/etc

--enable-extraprotection #棰濆瀹夊叏闃叉姢

./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection

make && make install

vim /etc/sarg/sarg.conf

--7琛?-鍙栨秷娉ㄩ噴

access_log /usr/local/squid/var/logs/access.log #鎸囧畾璁块棶鏃ュ織鏂囦欢

--25琛?-鍙栨秷娉ㄩ噴

title "squid user access reports" #缃戦〉鏍囬

--120琛?-鍙栨秷娉ㄩ噴锛屼慨鏀?/p>

output_dir /var/www/html/sarg #鎶ュ憡杈撳嚭鐩綍

--178琛?-鍙栨秷娉ㄩ噴

user_ip no #浣跨敤鐢ㄦ埛鍚嶆樉绀?/p>

--184琛?-鍙栨秷娉ㄩ噴锛屼慨鏀?/p>

topuser_sort_field connect reverse #top鎺掑簭涓紝鎸囧畾杩炴帴娆℃暟閲囩敤闄嶅簭鎺掑垪锛屽崌搴忔槸normal

--190琛?-鍙栨秷娉ㄩ噴锛屼慨鏀?/p>

user_sort_field connect reverse #瀵逛簬鐢ㄦ埛璁块棶璁板綍锛岃繛鎺ユ鏁版寜闄嶅簭鎺掑簭

--206琛?-鍙栨秷娉ㄩ噴锛屼慨鏀?/p>

exclude_hosts /usr/local/sarg/noreport #鎸囧畾涓嶈鍏ユ帓搴忕殑绔欑偣鍒楄〃鐨勬枃浠?/p>

--257琛?-鍙栨秷娉ㄩ噴

overwrite_report no #鍚屽悕鍚屾棩鏈熺殑鏃ュ織鏄惁瑕嗙洊

--289琛?-鍙栨秷娉ㄩ噴锛屼慨鏀?/p>

mail_utility mailq.postfix #鍙戦€侀偖浠舵姤鍛婂懡浠?/p>

--434琛?-鍙栨秷娉ㄩ噴锛屼慨鏀?/p>

charset utf-8 #鎸囧畾瀛楃闆哢tf-8

--518琛?-鍙栨秷娉ㄩ噴

weekdays 0-6 #top鎺掕鐨勬槦鏈熷懆鏈?/p>

--525琛?-鍙栨秷娉ㄩ噴

hours 0-23 #top鎺掕鐨勬椂闂村懆鏈?/p>

--633琛?-鍙栨秷娉ㄩ噴

www_document_root /var/www/html #鎸囧畾缃戦〉鏍圭洰褰?/p>

2銆佹坊鍔犱笉璁″叆绔欑偣鏂囦欢锛屾坊鍔犵殑鍩熷悕灏嗕笉琚樉绀哄湪鎺掑簭涓?/p>

touch /usr/local/sarg/noreport

ln -s /usr/local/sarg/bin/sarg /usr/local/bin/

3銆侀獙璇?/p>

瀹夎涓€涓樋甯曞鐨勭綉椤佃繘琛岃闂?/p>

yum install httpd -y

systemctl start httpd

4銆佽繍琛?/p>

sarg #鍚姩涓€娆¤褰?/p>

浣跨敤浠g悊鏈嶅姟鍣ㄧ殑ip鍦板潃杩涜璁块棶锛屾煡鐪媠arg鎶ュ憡缃戦〉銆?/p>

http://192.168.102.10/squid-reports/

5銆佹坊鍔犺鍒掍换鍔★紝鎵ц姣忓ぉ鐢熸垚鎶ュ憡

vim /usr/local/sarg/report.sh

#/bin/bash

#get current date

today=$(date %d/%m/%y)

#get one week ago today

yesterday=$(date -d "1 day ago" %d/%m/%y)

/usr/local/sarg/bin/sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/squid-reports -z -d $yesterday-$today &> /dev/null

find ./ -type d -a -name "$(env lang=en_us.utf-8 date -d "30 day ago" %y%b%d)-$(env lang=en_us.utf-8 date -d "29 day ago" %y%b%d)" | xargs rm -rf

exit 0

chmod x /usr/local/sarg/report.sh

鍒版墽琛屾瘡鏃ヤ换鍔$殑鏂囦欢閲岃繘琛岄厤缃?/p>

crontab -e

0 0 * * * /usr/local/sarg/report.sh

鍏€丼quid 鍙嶅悜浠g悊

濡傛灉 squid 鍙嶅悜浠g悊鏈嶅姟鍣ㄤ腑缂撳瓨浜嗚璇锋眰鐨勮祫婧愶紝鍒欏皢璇ヨ姹傜殑璧勬簮鐩存帴杩斿洖缁欏鎴风锛涘惁鍒欏弽鍚戜唬鐞嗘湇鍔″櫒灏嗗悜鍚庡彴鐨?web 鏈嶅姟鍣ㄨ姹傝祫婧愶紝鐒跺悗灏嗚姹傜殑搴旂瓟杩斿洖缁欏鎴风锛屽悓鏃朵篃灏嗚搴旂瓟缂撳瓨鍦ㄦ湰鍦帮紝渚涗笅涓€涓姹傝€呬娇鐢ㄣ€?/p>

1銆佸伐浣滄満鍒?/p>

鈼忕紦瀛樼綉椤靛璞★紝鍑忓皯閲嶅璇锋眰 鈼忓皢浜掕仈缃戣姹傝疆璁垨鎸夋潈閲嶅垎閰嶅埌鍐呯綉web鏈嶅姟鍣?鈼忎唬鐞嗙敤鎴疯姹傦紝閬垮厤鐢ㄦ埛鐩存帴璁块棶web鏈嶅姟鍣紝鎻愰珮瀹夊叏

vim /etc/squid.conf

http_port 192.168.102.10:80 accel vhost vport

cache_peer 192.168.102.20 parent 80 0 no-query originserver round-rob in max_conn=5000 weight=1 name=web1

cache_peer 192.168.102.30 parent 80 0 no-query originserver round-rob in max_conn=5000 weight=1 name=web2

cache_peer_domain web1 web2 www.dznb.com

http_port 80 accel vhost vport #squid浠庝竴涓紦瀛樺彉鎴愪簡涓€涓猈eb鏈嶅姟鍣ㄥ弽鍚戜唬鐞嗗姞閫熸ā寮忥紝杩欎釜鏃跺€檚quid鍦?0绔彛鐩戝惉璇锋眰锛屽悓鏃跺拰web server鐨勮姹傜鍙?vhost vport)缁戝畾锛岃繖涓椂鍊欒姹傚埌浜唖quid锛宻quid鏄笉鐢ㄨ浆鍙戣姹傜殑锛岃€屾槸鐩存帴瑕佷箞浠庣紦瀛樹腑鎷挎暟鎹涔堝悜缁戝畾鐨勭鍙g洿鎺ヨ姹傛暟鎹€?/p>

accel 锛氬弽鍚戜唬鐞嗗姞閫熸ā寮?/p>

vhost 锛氭敮鎸佸煙鍚嶆垨涓绘満鍚嶆潵琛ㄧず浠g悊鑺傜偣

vport 锛氭敮鎸両p鍜岀鍙f潵琛ㄧず浠g悊鑺傜偣

parent 锛氫唬琛ㄤ负鐖惰妭鐐癸紝涓婁笅鍏崇郴锛岄潪骞崇骇鍏崇郴

80 锛氫唬鐞嗗唴閮╳eb鏈嶅姟鍣ㄧ殑80绔彛

0 锛氭病鏈変娇鐢╥cp锛堢數淇¤繍钀ュ晢锛夛紝琛ㄧず灏变竴鍙皊quid鏈嶅姟鍣?/p>

no-query 锛氫笉鍋氭煡璇㈡搷浣滐紝鐩存帴鑾峰彇鏁版嵁

originserver 锛氭寚瀹氭槸婧愭湇鍔″櫒

round-robin 锛氭寚瀹?squid 閫氳繃杞鏂瑰紡灏嗚姹傚垎鍙戝埌鍏朵腑涓€鍙扮埗鑺傜偣

max_conn 锛氭寚瀹氭渶澶ц繛鎺ユ暟

weight 锛?鎸囧畾鏉冮噸

name 锛氳缃埆鍚?/p>

閲嶆柊鍚姩鏈嶅姟锛屽苟鏌ョ湅绔彛鏈夋病鏈夋墦寮€

supervisorctl restart squid

netstat -natp | grep squid

2銆佹竻绌轰箣鍓嶉€忔槑妯″紡閰嶇疆鐨?iptables 瑙勫垯

iptables -f

iptables -t nat -f

妫€鏌ヨ娉曟槸鍚﹂敊璇?/p>

squid -k parse

鍒濆鍖栫紦瀛樼洰褰?/p>

squid -z #-z 閫夐」鐢ㄦ潵鍒濆鍖栫紦瀛樼洰褰?/p>

squid #鍚姩 squid 鏈嶅姟

netstat -anpt | grep "squid"

3銆佸悗绔妭鐐规湇鍔″櫒璁剧疆

yum install -y httpd

systemctl start httpd

鍦ㄤ袱涓妭鐐逛笂杩涜璁剧疆椤甸潰

4銆佸鎴锋満鐨勫煙鍚嶆槧灏勯厤缃?/p>

浣跨敤鏂扮殑涓绘満娴忚鍣ㄤ笉寮€鍚唬鐞嗚闂?/p>

鏌ョ湅缂撳瓨鍛戒腑鎯呭喌

#鍦ㄤ唬鐞嗘湇鍔″櫒涓婅繘琛屾煡鐪嬪懡涓儏鍐?/p>

tailf /usr/local/squid/var/logs/access.log

鏂囩珷鐩綍

mysql杩涢樁瀛︿範鍓嶈█1銆佸瓨鍌ㄥ紩鎿?.1 mysql浣撶郴缁撴瀯姒傝1.2 瀛樺偍寮曟搸浠嬬粛1.3 甯歌瀛樺偍寮曟搸鐨勭壒鐐?.3.1 innodb鐨勭壒鐐?.3.2 myisam1.3.3 memory1.3.4 鎬荤粨

2銆佺储寮?.1 绱㈠紩浠嬬粛2.2 绱㈠紩鐨勭粨鏋?.2.1 鍓嶇疆鐭ヨ瘑2.2.2 绱㈠紩缁撴瀯鐨勭绫?/p>

2.3 绱㈠紩鐨勫垎绫?.4 绱㈠紩鐨勪娇鐢?.4.1 绱㈠紩鐨勫熀鏈娇鐢?.4.2 楠岃瘉绱㈠紩鐨勬晥鐜?.4.3 鑱斿悎绱㈠紩2.4.4 绱㈠紩鐨勫け鏁堟儏鍐?.4.5 sql鎻愮ず2.4.6 瑕嗙洊绱㈠紩2.4.7 鍓嶇紑绱㈠紩2.4.8 鍗曞垪绱㈠紩涓庤仈鍚堢储寮?/p>

2.5 绱㈠紩鐨勮璁″師鍒?/p>

3銆丼ql浼樺寲3.1 sql鎬ц兘鍒嗘瀽3.2 insert浼樺寲3.2.1 鏅€氭暟鎹彃鍏?.2.2 澶ф暟鎹彃鍏?/p>

3.3 涓婚敭浼樺寲3.3.1 鏁版嵁鐨勭粍缁囨柟寮?.3.2 椤靛垎瑁?.3.3 椤靛悎骞?.3.4 涓婚敭鐨勮璁″師鍒?/p>

3.4 order by浼樺寲3.5 group by浼樺寲3.6 limit浼樺寲3.7 count浼樺寲3.8 update浼樺寲

4銆佽鍥?瀛樺偍杩囩▼/瑙﹀彂鍣?.1 瑙嗗浘4.1.1 瑙嗗浘姒傝堪4.1.2 瑙嗗浘鐨勫熀鏈娇鐢?.1.3 閫夐」閰嶇疆璇﹁в

4.2 瀛樺偍杩囩▼4.2.1 瀛樺偍杩囩▼姒傝堪4.2.2 瀛樺偍杩囩▼鐨勫熀鏈娇鐢?.2.3 鍙橀噺4.2.4 if4.2.5 case4.2.6 while4.2.7 repeat4.2.8 loop4.2.9 娓告爣

4.3 瀛樺偍鍑芥暟4.4 瑙﹀彂鍣?.4.1 瑙﹀彂鍣ㄦ杩?.4.2 瑙﹀彂鍣ㄧ殑鍩烘湰浣跨敤

5銆侀攣5.1 閿佹杩?.2 鍏ㄥ眬閿?.3 琛ㄧ骇閿?.3.1 琛ㄩ攣5.3.2 鍏冩暟鎹攣5.3.3 鎰忓悜閿?/p>

5.4 琛岀骇閿?.4.1 琛岄攣5.4.2 闂撮殭閿?涓撮敭閿?/p>

6銆両nnodb寮曟搸6.1 innodb姒傝堪6.2 innodb鐨勬灦鏋?.2.1 鍐呭瓨缁撴瀯6.2.2 纾佺洏缁撴瀯6.2.3 鍚庡彴绾跨▼

6.3 浜嬪姟鍘熺悊6.3.1 redo log6.3.2 undo log6.3.3 mvcc

7銆丮ysql绠$悊7.1 mysql绯荤粺鏁版嵁搴?.2 mysql甯哥敤宸ュ叿

鎬荤粨鐩稿叧闈㈣瘯棰?/p>

mysql杩涢樁瀛︿範

鍓嶈█

鏈瘒鏂囩珷鏄mysql杩涢樁绡囧涔犳墍鍐欑殑绗旇锛岄€氳繃杩涢樁绡囩殑瀛︿範锛屾垜浜嗚в浜咺nnodb搴曞眰鐨勫ぇ鑷寸粨鏋勶紝甯歌绱㈠紩缁撴瀯浠ュ強鍚勭绱㈠紩缁撴瀯鐨勪紭缂虹偣锛岃繕鏈夊緢澶氬父瑙佺殑sql浼樺寲鏂规硶锛屼互鍙婂浣曡繘琛孲ql璋冧紭锛岃繕鏈変竴浜汳ysql楂樼骇鐭ヨ瘑姣斿瑙﹀彂鍣ㄣ€佸瓨鍌ㄥ嚱鏁帮紝mysql涓殑鍚勭閿佺殑鐗圭偣锛屼簨鍔$殑瀹炵幇鍘熺悊绛夌瓑锛屽緢澶氫笢瑗块兘鏄湪瀛︽牎璇惧爞涓涓嶅埌鐨勪笢瑗匡紝鍦ㄦ涔熷崄鍒嗘劅璋绔欓粦椹?/p>

瀛︿範瑙嗛锛歁ysql浠庡叆闂ㄥ埌绮鹃€歁ysql瀹樻柟鏂囨。锛歁ysql8

1銆佸瓨鍌ㄥ紩鎿?/p>

1.1 mysql浣撶郴缁撴瀯姒傝

杩炴帴灞傦細鏈€涓婂眰鏄竴浜涘鎴风鍜岄摼鎺ユ湇鍔★紝鍖呭惈鏈湴 sock 閫氫俊鍜屽ぇ澶氭暟鍩轰簬瀹㈡埛绔?鏈嶅姟绔伐鍏峰疄鐜扮殑绫讳技浜?tcp/ip鐨勯€氫俊銆備富瑕佸畬鎴愪竴浜涚被浼间簬杩炴帴澶勭悊銆佹巿鏉冭璇併€佸強鐩稿叧鐨勫畨鍏ㄦ柟妗堛€傚湪璇ュ眰涓婂紩鍏ヤ簡绾跨▼ 姹犵殑姒傚康锛屼负閫氳繃璁よ瘉瀹夊叏鎺ュ叆鐨勫鎴风鎻愪緵绾跨▼銆傚悓鏍峰湪璇ュ眰涓婂彲浠ュ疄鐜板熀浜?ssl 鐨勫畨鍏ㄩ摼鎺ャ€傛湇鍔?鍣ㄤ篃浼氫负瀹夊叏鎺ュ叆鐨勬瘡涓鎴风楠岃瘉瀹冩墍鍏锋湁鐨勬搷浣滄潈闄愩€傛湇鍔″眰锛?绗簩灞傛灦鏋勪富瑕佸畬鎴愬ぇ澶氭暟鐨勬牳蹇冩湇鍔″姛鑳斤紝濡係ql鎺ュ彛锛屽苟瀹屾垚缂撳瓨鐨勬煡璇紝sql鐨勫垎鏋愬拰浼樺寲锛岄儴 鍒嗗唴缃嚱鏁扮殑鎵ц銆傛墍鏈夎法瀛樺偍寮曟搸鐨勫姛鑳戒篃鍦ㄨ繖涓€灞傚疄鐜帮紝濡?杩囩▼銆佸嚱鏁扮瓑銆傚湪璇ュ眰锛屾湇鍔″櫒浼氳в 鏋愭煡璇㈠苟鍒涘缓鐩稿簲鐨勫唴閮ㄨв鏋愭爲锛屽苟瀵瑰叾瀹屾垚鐩稿簲鐨勪紭鍖栧纭畾琛ㄧ殑鏌ヨ鐨勯『搴忥紝鏄惁鍒╃敤绱㈠紩绛夛紝 鏈€鍚庣敓鎴愮浉搴旂殑鎵ц鎿嶄綔銆傚鏋滄槸select璇彞锛屾湇鍔″櫒杩樹細鏌ヨ鍐呴儴鐨勭紦瀛橈紝濡傛灉缂撳瓨绌洪棿瓒冲澶э紝 杩欐牱鍦ㄨв鍐冲ぇ閲忚鎿嶄綔鐨勭幆澧冧腑鑳藉寰堝ソ鐨勬彁鍗囩郴缁熺殑鎬ц兘銆傚紩鎿庡眰锛?瀛樺偍寮曟搸灞傦紝 瀛樺偍寮曟搸鐪熸鐨勮礋璐d簡mysql涓暟鎹殑瀛樺偍鍜屾彁鍙栵紝鏈嶅姟鍣ㄩ€氳繃api鍜屽瓨鍌ㄥ紩鎿庤繘琛岄€?淇°€備笉鍚岀殑瀛樺偍寮曟搸鍏锋湁涓嶅悓鐨勫姛鑳斤紝杩欐牱鎴戜滑鍙互鏍规嵁鑷繁鐨勯渶瑕侊紝鏉ラ€夊彇鍚堥€傜殑瀛樺偍寮曟搸銆傛暟鎹簱 涓殑绱㈠紩鏄湪瀛樺偍寮曟搸灞傚疄鐜扮殑銆傚瓨鍌ㄥ眰锛?鏁版嵁瀛樺偍灞傦紝 涓昏鏄皢鏁版嵁(濡? redolog銆乽ndolog銆佹暟鎹€佺储寮曘€佷簩杩涘埗鏃ュ織銆侀敊璇棩蹇椼€佹煡璇?鏃ュ織銆佹參鏌ヨ鏃ュ織绛?瀛樺偍鍦ㄦ枃浠剁郴缁熶箣涓婏紝骞跺畬鎴愪笌瀛樺偍寮曟搸鐨勪氦浜掋€?鍜屽叾浠栨暟鎹簱鐩告瘮锛孧ysql鏈夌偣涓庝紬涓嶅悓锛屽畠鐨勬灦鏋勫彲浠ュ湪澶氱涓嶅悓鍦烘櫙涓簲鐢ㄥ苟鍙戞尌鑹ソ浣滅敤銆備富瑕?浣撶幇鍦ㄥ瓨鍌ㄥ紩鎿庝笂锛屾彃浠跺紡鐨勫瓨鍌ㄥ紩鎿庢灦鏋勶紝灏嗘煡璇㈠鐞嗗拰鍏朵粬鐨勭郴缁熶换鍔′互鍙婃暟鎹殑瀛樺偍鎻愬彇鍒嗙銆?杩欑鏋舵瀯鍙互鏍规嵁涓氬姟鐨勯渶姹傚拰瀹為檯闇€瑕侀€夋嫨鍚堥€傜殑瀛樺偍寮曟搸銆?/p>

1.2 瀛樺偍寮曟搸浠嬬粛

澶у鍙兘娌℃湁鍚杩囧瓨鍌ㄥ紩鎿庯紝浣嗘槸涓€瀹氬惉杩囧紩鎿庤繖涓瘝锛屽紩鎿庡氨鏄彂鍔ㄦ満锛屾槸涓€涓満鍣ㄧ殑鏍稿績缁勪欢銆?姣斿锛屽浜庤埌杞芥満銆佺洿鍗囨満銆佺伀绠潵璇达紝浠栦滑閮芥湁鍚勮嚜鐨勫紩鎿庯紝鏄粬浠渶涓烘牳蹇冪殑缁勪欢銆傝€屾垜浠湪閫夋嫨 寮曟搸鐨勬椂鍊欙紝闇€瑕佸湪鍚堥€傜殑鍦烘櫙锛岄€夋嫨鍚堥€傜殑瀛樺偍寮曟搸锛屽氨鍍忓湪鐩村崌鏈轰笂锛屾垜浠笉鑳介€夋嫨鑸拌浇鏈虹殑寮曟搸 涓€鏍枫€?鑰屽浜庡瓨鍌ㄥ紩鎿庯紝涔熸槸涓€鏍凤紝浠栨槸mysql鏁版嵁搴撶殑鏍稿績锛屾垜浠篃闇€瑕佸湪鍚堥€傜殑鍦烘櫙閫夋嫨鍚堥€傜殑瀛樺偍寮?鎿庛€傛帴涓嬫潵灏辨潵浠嬬粛涓€涓嬪瓨鍌ㄥ紩鎿庛€?/p>

浠€涔堟槸瀛樺偍寮曟搸锛?/p>

鍦∕ysql涓紝瀛樺偍寮曟搸鏄竴绉嶈礋璐g鐞嗚〃鐨勭墿鐞嗗瓨鍌ㄥ拰妫€绱㈢殑杞欢缁勪欢銆傛崲瑷€涔嬶細瀛樺偍寮曟搸灏辨槸瀛樺偍鏁版嵁銆佸缓绔嬬储寮曘€佹洿鏂?鏌ヨ鏁版嵁绛夋妧鏈殑瀹炵幇鏂瑰紡銆?/p>

娉ㄦ剰锛氬瓨鍌ㄥ紩鎿庢槸鍩轰簬琛ㄧ殑锛岃€屼笉鏄熀浜庡簱鐨勶紝鎵€浠ュ瓨鍌ㄥ紩鎿庝篃鍙绉颁负琛ㄧ被鍨嬨€傛垜浠彲浠ュ湪鍒涘缓琛ㄧ殑鏃跺€欙紝鏉ユ寚瀹氶€夋嫨鐨勫瓨鍌ㄥ紩鎿庯紝濡傛灉 娌℃湁鎸囧畾灏嗚嚜鍔ㄩ€夋嫨榛樿鐨勫瓨鍌ㄥ紩鎿庛€?/p>

瀛樺偍寮曟搸鐨勪綔鐢ㄦ槸浠€涔堬紵

鏁版嵁瀛樺偍鍜屾绱細瀛樺偍寮曟搸璐熻矗灏嗘暟鎹瓨鍌ㄥ湪纾佺洏鎴栧唴瀛樹腑锛屽苟涓斿湪鏌ヨ鏃舵牴鎹储寮曞拰鏌ヨ鏉′欢蹇€熸绱㈡暟鎹€傛暟鎹殑璇诲啓鎬ц兘锛氬瓨鍌ㄥ紩鎿庣殑瀹炵幇鏂瑰紡涓嶅悓锛屽浜庢暟鎹殑璇诲啓鎬ц兘涔熸湁杈冨ぇ鐨勫奖鍝嶃€傛瘮濡傦紝innodb瀛樺偍寮曟搸鏀寔浜嬪姟澶勭悊鍜岃绾ч攣瀹氾紝閫傚悎楂樺苟鍙戠殑璇诲啓鍦烘櫙锛涜€孧yisam瀛樺偍寮曟搸鍒欓€傚悎澶ч噺璇诲彇銆佽緝灏戞洿鏂扮殑搴旂敤銆傛暟鎹畬鏁存€э細瀛樺偍寮曟搸璐熻矗缁存姢鏁版嵁鐨勫畬鏁存€э紝纭繚鏁版嵁鐨勪竴鑷存€у拰姝g‘鎬с€傛瘮濡傦紝innodb瀛樺偍寮曟搸鏀寔浜嬪姟澶勭悊锛屽彲浠ヤ繚璇佹暟鎹殑acid灞炴€э紝閬垮厤鏁版嵁鐨勪涪澶卞拰涓嶄竴鑷淬€傚瓨鍌ㄥ紩鎿庣殑鐗规€э細涓嶅悓鐨勫瓨鍌ㄥ紩鎿庢彁渚涗簡涓嶅悓鐨勭壒鎬э紝渚嬪myisam瀛樺偍寮曟搸鏀寔鍏ㄦ枃鏈储寮曞拰鍘嬬缉绛夊姛鑳斤紝innodb瀛樺偍寮曟搸鏀寔澶栭敭鍜岃绾ч攣瀹氱瓑鐗规€э紝memory瀛樺偍寮曟搸鎻愪緵闈炲父蹇殑鏁版嵁璁块棶閫熷害绛夈€傚簲鐢ㄧ▼搴忕殑鍙墿灞曟€э細閫氳繃閫夋嫨鍚堥€傜殑瀛樺偍寮曟搸锛屽彲浠ヤ负搴旂敤绋嬪簭鎻愪緵鏇村ソ鐨勫彲鎵╁睍鎬с€備緥濡傦紝innodb瀛樺偍寮曟搸鏀寔闆嗙兢妯″紡锛屽彲浠ユ墿灞曞埌澶氬彴鏈嶅姟鍣紝浠ユ弧瓒充笉鏂闀跨殑鏁版嵁闇€姹傘€? mysql涓父瑙佺殑瀛樺偍寮曟搸鏈夊摢浜涳紵

innodb锛欼nnodb鏄疢ysql鐨勯粯璁ゅ瓨鍌ㄥ紩鎿庯紝鏀寔浜嬪姟澶勭悊銆佽绾ч攣瀹氥€佸閿瓑楂樼骇鐗规€э紝閫傚悎浜庡鐞嗛珮骞跺彂銆侀珮鍙潬鎬с€侀珮鎵╁睍鎬х殑搴旂敤銆侻yisam锛歁yisam鏄疢ysql鏃╂湡鐨勪竴绉嶅瓨鍌ㄥ紩鎿庯紝涓嶆敮鎸佷簨鍔″鐞嗗拰琛岀骇閿佸畾锛屼絾鏀寔鍏ㄦ枃鏈悳绱㈢瓑鐗规€э紝閫傚悎浜庡ぇ閲忚鍙栥€佽緝灏戞洿鏂扮殑搴旂敤銆侻emory锛歁emory瀛樺偍寮曟搸灏嗚〃鏁版嵁瀛樺偍鍦ㄥ唴瀛樹腑锛屾彁渚涗簡闈炲父蹇殑鏁版嵁璁块棶閫熷害锛屼絾涓嶆敮鎸佷簨鍔″鐞嗗拰鎸佷箙鍖栧瓨鍌紝閫傚悎浜庣紦瀛樸€佷細璇濇暟鎹瓑闇€瑕佸揩閫熻鍙栫殑鍦烘櫙銆侫rchive锛欰rchive瀛樺偍寮曟搸閫傜敤浜庨渶瑕佸瓨鍌ㄥぇ閲忓巻鍙叉暟鎹殑鍦烘櫙锛屽畠鏀寔楂樺害鍘嬬缉銆佸揩閫熸彃鍏ュ拰鏌ヨ锛屼絾涓嶆敮鎸佺储寮曘€佹洿鏂板拰鍒犻櫎鎿嶄綔銆侰sv锛欳sv瀛樺偍寮曟搸鐢ㄤ簬澶勭悊浠ラ€楀彿鍒嗛殧鐨勫€硷紙csv锛夋牸寮忕殑鏁版嵁锛屽彲浠ュ皢csv鏂囦欢浣滀负mysql琛ㄦ潵澶勭悊銆? 瀛樺偍寮曟搸甯歌鎿嶄綔锛?# 寤鸿〃鏃舵寚瀹氬瓨鍌ㄥ紩鎿?/p>

# 澶囨敞锛氭寚瀹氬悗璇ヨ〃灏遍噰鐢ㄦ寚瀹氱殑瀛樺偍寮曟搸锛孧ysql5.5涔嬪悗涓嶆寚瀹氶粯璁ゅ氨閲囩敤innodb瀛樺偍寮曟搸

create table 琛ㄥ悕(

瀛楁1 瀛楁1绫诲瀷 [ comment 瀛楁1娉ㄩ噴 ] ,

......

瀛楁n 瀛楁n绫诲瀷 [comment 瀛楁n娉ㄩ噴 ]

) engine = innodb [ comment 琛ㄦ敞閲?] ;

# 鏌ョ湅褰撳墠鏁版嵁搴撴敮鎸佺殑瀛樺偍寮曟搸

show engines;

# 鏌ョ湅寤鸿〃璇彞锛堝彲浠ラ€氳繃寤鸿〃璇彞鏌ョ湅璇ヨ〃浣跨敤鐨勫瓨鍌ㄥ紩鎿庯級

show create table 琛ㄥ悕;

1.3 甯歌瀛樺偍寮曟搸鐨勭壒鐐?/p>

涓婇潰鎴戜滑浠嬬粛浜嗕粈涔堟槸瀛樺偍寮曟搸锛屼互鍙婂浣曞湪寤鸿〃鏃跺浣曟寚瀹氬瓨鍌ㄥ紩鎿庯紝鎺ヤ笅鏉ユ垜浠氨鏉ヤ粙缁嶄笅鏉ヤ笂闈?閲嶇偣鎻愬埌鐨勪笁绉嶅瓨鍌ㄥ紩鎿?innodb銆丮yisam銆丮emory鐨勭壒鐐广€?/p>

1.3.1 innodb鐨勭壒鐐?/p>

innodb锛欼nnodb鏄竴绉嶅吋椤鹃珮鍙潬鎬у拰楂樻€ц兘鐨勯€氱敤瀛樺偍寮曟搸锛屽湪 mysql 5.5 涔嬪悗锛孖nnodb鏄粯璁ょ殑 mysql 瀛樺偍寮曟搸

鐗圭偣锛?/p>

鏀寔浜嬪姟锛欼nnodb鏄竴绉嶆敮鎸佷簨鍔$殑瀛樺偍寮曟搸锛屽畠閬靛惊acid锛堝師瀛愭€с€佷竴鑷存€с€侀殧绂绘€с€佹寔涔呮€э級浜嬪姟妯″瀷锛屽彲浠ヤ繚璇佹暟鎹殑瀹屾暣鎬у拰涓€鑷存€с€? 琛岀骇閿佸畾锛欼nnodb瀛樺偍寮曟搸鏀寔琛岀骇閿佸畾锛屽畠鍙互璁╁涓簨鍔″悓鏃跺鍚屼竴琛ㄨ繘琛岃鎿嶄綔锛岃€屼笉浼氫骇鐢熼攣鍐茬獊锛屾彁楂樹簡绯荤粺鐨勫苟鍙戞€ц兘銆? 澶栭敭绾︽潫锛欼nnodb瀛樺偍寮曟搸鏀寔澶栭敭绾︽潫锛屽彲浠ラ€氳繃瀹氫箟澶栭敭鍏崇郴锛屼繚璇佽〃涔嬮棿鐨勬暟鎹畬鏁存€с€? 楂樺彲闈犳€э細innodb瀛樺偍寮曟搸閲囩敤浜嗗鐗堟湰骞跺彂鎺у埗锛圡vcc锛夋満鍒讹紝鍙互淇濊瘉鏁版嵁鐨勪竴鑷存€у拰鍙潬鎬с€傚悓鏃讹紝瀹冭繕鎻愪緵浜嗚嚜鍔ㄥ穿婧冩仮澶嶅拰鏁呴殰杞щ绛夊姛鑳斤紝淇濊瘉浜嗙郴缁熺殑楂樺彲鐢ㄦ€с€? 鏀寔鐑浠斤細innodb瀛樺偍寮曟搸鏀寔鍦ㄧ嚎鐑浠斤紝鍙互鍦ㄧ郴缁熻繍琛屾湡闂磋繘琛屽浠芥搷浣滐紝鑰屼笉浼氬绯荤粺浜х敓褰卞搷銆? 楂樻€ц兘锛欼nnodb瀛樺偍寮曟搸閲囩敤浜嗕竴绯诲垪浼樺寲鎶€鏈紝渚嬪缂撳啿姹犮€佽嚜閫傚簲鍝堝笇绱㈠紩绛夛紝鍙互鎻愰珮绯荤粺鐨勬煡璇㈡€ц兘鍜屽搷搴旈€熷害銆? 瀛樺偍鏂囦欢锛氫娇鐢↖nnodb鐨勮〃閮芥槸浠xx.ibd鐨勫舰寮忓瓨鍌ㄥ湪纾佺洏锛寈xx浠h〃鐨勬槸琛ㄥ悕锛宨nnodb寮曟搸鐨勬瘡寮犺〃閮戒細瀵瑰簲杩欐牱涓€涓〃绌洪棿鏂囦欢锛屽瓨鍌ㄨ琛ㄧ殑琛ㄧ粨鏋勶紙 frm 鏃╂湡鐨?銆?.0鍚庨兘鏄?sdi 锛夈€佹暟鎹拰绱㈠紩 澶囨敞锛氬彲浠ュ湪c:\programdata\mysql\mysql server 8.0\data\锛堣繖鏄垜c鐩樹笅mysql鐨勫瓨鍌ㄨ矾寰勶級涓嬬殑琛ㄤ腑杩涜鏌ョ湅锛屾瘮濡傛垜鎵撳紑璇ョ洰褰曚笅鐨刯avaee_test琛紝浼氱湅鍒?tb_user.ibd杩欏紶琛ㄧ粨鏋?锛屽湪褰撳墠鐩綍涓嬫墦寮€ cmd 绐楀彛锛岀劧鍚庤兘澶熻緭鍏?ibd2sdi tb_user.ibd 鎸囦护锛屽氨鑳藉鏌ョ湅璇ヨ〃鐨勮〃缁撴瀯浜? # 鏌ョ湅mysql鏄惁寮€鍚?innodb_file_per_table

show variables like 'innodb_file_per_table';

innodb_file_per_table鏄疢ysql鐨勪竴涓厤缃€夐」锛岀敤浜庢帶鍒禝nnodb瀛樺偍寮曟搸涓〃鐨勬暟鎹拰绱㈠紩鏄惁瀛樺偍鍦ㄥ崟鐙殑琛ㄧ┖闂存枃浠朵腑銆?.0鐗堟湰鐨凪ysql榛樿閮芥槸閰嶇疆涓?on 鐨勶紝鍗矷nnodb瀛樺偍寮曟搸涓烘瘡涓〃鍗曠嫭鍒涘缓涓€涓〃绌洪棿鏂囦欢锛屾瘡涓枃浠跺寘鍚琛ㄧ殑鏁版嵁鍜岀储寮曪紝杩欐牱鍋氭湁涓€涓嬪嚑鐐瑰ソ澶勶細

鍙互鏇村ソ鍦扮鐞嗗拰缁存姢鏁版嵁搴擄細鍗曠嫭鐨勮〃绌洪棿鏂囦欢鍙互鏂逛究鍦板浠姐€佹仮澶嶃€佺щ鍔ㄥ拰鍒犻櫎銆傛敼鍠勬€ц兘锛氬崟鐙殑琛ㄧ┖闂存枃浠跺彲浠ュ噺灏忓叡浜〃绌洪棿鐨勫ぇ灏忥紝鎻愰珮鏁版嵁搴撶殑鎬ц兘銆傛洿濂界殑纰庣墖鏁寸悊鍜屼紭鍖栵細褰撳垹闄ゆ垨鏇存柊鏁版嵁鏃讹紝鍗曠嫭鐨勮〃绌洪棿鏂囦欢鍙互鏇村ソ鍦版暣鐞嗗拰浼樺寲纰庣墖 娉ㄦ剰锛氬惎鐢╥nnodb_file_per_table閫夐」浼氬鍔犵鐩樼┖闂寸殑浣跨敤锛屽洜涓烘瘡涓〃閮介渶瑕佷竴涓崟鐙殑琛ㄧ┖闂存枃浠躲€傚洜姝わ紝鍦ㄨ缃閫夐」鏃堕渶瑕佷粩缁嗚€冭檻绯荤粺鐨勭鐩樼┖闂村拰鎬ц兘闇€姹傘€俰nnodb_file_per_table鐨勫叧闂槸off锛屽鏋滃皢鍏惰缃负on銆?銆乊es銆乀rue銆丒nabled绛夌瓑锛岄兘琛ㄧず鍚敤璇ラ€夐」 閫昏緫瀛樺偍缁撴瀯

琛ㄧ┖闂?: innodb瀛樺偍寮曟搸閫昏緫缁撴瀯鐨勬渶楂樺眰锛宨bd鏂囦欢鍏跺疄灏辨槸琛ㄧ┖闂存枃浠讹紝鍦ㄨ〃绌洪棿涓彲浠?鍖呭惈澶氫釜 segment 娈点€傛 : 琛ㄧ┖闂存槸鐢卞悇涓缁勬垚鐨勶紝 甯歌鐨勬鏈夋暟鎹銆佺储寮曟銆佸洖婊氭绛夈€侷nnodb涓浜庢鐨勭 鐞嗭紝閮芥槸寮曟搸鑷韩瀹屾垚锛屼笉闇€瑕佷汉涓哄鍏舵帶鍒讹紝涓€涓涓寘鍚涓尯銆傚尯 : 鍖烘槸琛ㄧ┖闂寸殑鍗曞厓缁撴瀯锛屾瘡涓尯鐨勫ぇ灏忎负1m銆?榛樿鎯呭喌涓嬶紝 innodb瀛樺偍寮曟搸椤靛ぇ灏忎负 16k锛?鍗充竴涓尯涓竴鍏辨湁64涓繛缁殑椤点€傞〉 : 椤垫槸缁勬垚鍖虹殑鏈€灏忓崟鍏冿紝椤典篃鏄疘nnodb 瀛樺偍寮曟搸纾佺洏绠$悊鐨勬渶灏忓崟鍏冿紝姣忎釜椤电殑澶у皬榛?璁や负 16kb銆備负浜嗕繚璇侀〉鐨勮繛缁€э紝innodb 瀛樺偍寮曟搸姣忔浠庣鐩樼敵璇?4-5 涓尯銆傝 : innodb 瀛樺偍寮曟搸鏄潰鍚戣鐨勶紝涔熷氨鏄鏁版嵁鏄寜琛岃繘琛屽瓨鏀剧殑锛屽湪姣忎竴琛屼腑闄や簡瀹氫箟琛ㄦ椂 鎵€鎸囧畾鐨勫瓧娈典互澶栵紝杩樺寘鍚袱涓殣钘忓瓧娈?鍚庨潰浼氳缁嗕粙缁?銆?

1.3.2 myisam

myisam锛氭槸mysql鏃╂湡鐨勯粯璁ゅ瓨鍌ㄥ紩鎿?

鐗圭偣锛?

绠€鍗曞揩閫燂細myisam瀛樺偍寮曟搸鐨勬暟鎹枃浠跺拰绱㈠紩鏂囦欢鏄垎寮€鐨勶紝鏁版嵁鏂囦欢鍙繚瀛樻暟鎹紝绱㈠紩鏂囦欢鍙繚瀛樼储寮曪紝鍥犳鏌ヨ閫熷害蹇€備笉鏀寔浜嬪姟锛歁yisam瀛樺偍寮曟搸涓嶆敮鎸佷簨鍔″鐞嗭紝涔熶笉鏀寔澶栭敭绾︽潫锛屽洜姝ゅ浜庨渶瑕佸ぇ閲忕殑鎻掑叆銆佹洿鏂板拰鏌ヨ鎿嶄綔锛屼絾瀵规暟鎹殑涓€鑷存€ц姹備笉鏄緢楂樼殑搴旂敤锛屼娇鐢∕yisam瀛樺偍寮曟搸鍙互鑾峰緱鏇村ソ鐨勬€ц兘銆傚鏄撴崯鍧忥細myisam瀛樺偍寮曟搸鍦ㄩ亣鍒扮‖浠舵晠闅滄垨鎿嶄綔绯荤粺宕╂簝绛夐棶棰樻椂瀹规槗鍑虹幇鎹熷潖锛屼笖闅句互淇锛屽洜姝ら渶瑕佸畾鏈熻繘琛屾暟鎹浠姐€傛敮鎸佸叏鏂囩储寮曪細myisam瀛樺偍寮曟搸鏀寔鍏ㄦ枃绱㈠紩锛屽彲浠ョ敤浜庡揩閫熺殑鏂囨湰鎼滅储銆傛敮鎸佸帇缂╄〃鏍硷細myisam瀛樺偍寮曟搸鏀寔鍘嬬缉琛ㄦ牸锛屽彲浠ユ樉钁楀噺灏忔暟鎹枃浠剁殑澶у皬锛屼粠鑰岃妭鐪佺鐩樼┖闂淬€?瀛樺偍鏂囦欢锛歺xx.sdi锛氬瓨鍌ㄨ〃缁撴瀯淇℃伅锛?xxx.myd: 瀛樺偍鏁版嵁锛?xxx.myi: 瀛樺偍绱㈠紩

1.3.3 memory

memory锛歁emory寮曟搸涔熺о锛圚eap锛夌殑琛ㄦ暟鎹椂瀛樺偍鍦ㄥ唴瀛樹腑鐨勶紝鐢变簬鍙楀埌纭欢闂銆佹垨鏂數闂鐨勫奖鍝嶏紝鍙兘灏嗚繖浜涜〃浣滀负 涓存椂琛ㄦ垨缂撳瓨浣跨敤

鐗圭偣锛?/p>

鏁版嵁瀛樺偍鍦ㄥ唴瀛樹腑锛歁emory瀛樺偍寮曟搸鐨勬暟鎹槸瀛樺偍鍦ㄥ唴瀛樹腑鐨勶紝鍥犳璇诲彇鍜屽啓鍏ユ暟鎹殑閫熷害闈炲父蹇紝鐗瑰埆閫傚悎浜庨渶瑕侀珮閫熻鍐欑殑搴旂敤銆備笉鏀寔鎸佷箙鍖栵細鐢变簬鏁版嵁瀛樺偍鍦ㄥ唴瀛樹腑锛屽洜姝ゅ湪mysql鏈嶅姟鍣ㄥ叧闂垨宕╂簝鏃讹紝鏁版嵁灏嗕細涓㈠け锛屽洜姝emory瀛樺偍寮曟搸涓嶆敮鎸佹暟鎹殑鎸佷箙鍖栵紝闇€瑕佸畾鏈熷浠芥暟鎹€備笉鏀寔blob鍜孴ext瀛楁锛歁emory瀛樺偍寮曟搸涓嶆敮鎸丅lob鍜孴ext瀛楁锛屽洜姝ゅ浜庨渶瑕佸瓨鍌ㄥぇ鏂囨湰鎴栦簩杩涘埗鏁版嵁鐨勫簲鐢紝涓嶉€傚悎浣跨敤memory瀛樺偍寮曟搸銆備笉鏀寔浜嬪姟锛歁emory瀛樺偍寮曟搸涓嶆敮鎸佷簨鍔★紝鍥犳涓嶈兘浣跨敤start transaction銆丆ommit鎴朢ollback绛夎鍙ャ€備笉鏀寔澶栭敭锛歁emory瀛樺偍寮曟搸涓嶆敮鎸佸閿害鏉燂紝鍥犳闇€瑕佸簲鐢ㄧ▼搴忚嚜琛岀‘淇濇暟鎹殑涓€鑷存€с€? 瀛樺偍鏂囦欢锛歺xx.sdi锛屽瓨鍌ㄨ〃缁撴瀯淇℃伅

1.3.4 鎬荤粨

涓夎€呯殑姣旇緝锛? innodb鍜孧yisam鐨勫尯鍒?/p>

浜嬪姟鏀寔锛欼nnodb鏀寔浜嬪姟澶勭悊锛屽彲浠ヤ娇鐢ˋcid鐗规€х‘淇濇暟鎹殑涓€鑷存€у拰鍙潬鎬э紝鑰孧yisam涓嶆敮鎸佷簨鍔″鐞嗐€傞攣绾у埆锛欼nnodb閲囩敤琛岀骇閿侊紝鍙互鍦ㄥ苟鍙戣闂椂閬垮厤閿佸啿绐侊紝鎻愰珮骞跺彂鎬ц兘锛涜€孧yisam閲囩敤琛ㄧ骇閿侊紝鍙兘瀵规暣涓〃杩涜鍔犻攣锛屽綋澶氫釜鐢ㄦ埛骞跺彂璁块棶鏃跺鏄撳嚭鐜伴攣鍐茬獊銆傚閿害鏉燂細innodb鏀寔澶栭敭绾︽潫锛屽彲浠ョ‘淇濇暟鎹殑瀹屾暣鎬э紝鑰孧yisam涓嶆敮鎸佸閿害鏉熴€傚叏鏂囩储寮曪細myisam鏀寔鍏ㄦ枃绱㈠紩锛屽彲浠ョ敤浜庡揩閫熺殑鏂囨湰鎼滅储锛岃€孖nnodb鍦∕ysql 5.6鐗堟湰涔嬪墠涓嶆敮鎸佸叏鏂囩储寮曘€傜储寮曠紦瀛橈細innodb鐨勭储寮曠紦瀛橀噰鐢ㄤ簡缂撳瓨姹犵殑鏈哄埗锛屽彲浠ュ噺灏戠鐩業/o鎿嶄綔锛屾彁楂樻煡璇㈡€ц兘锛涜€孧yisam鐨勭储寮曠紦瀛樼洿鎺ヤ娇鐢ㄧ郴缁熺紦瀛橈紝瀵瑰ぇ鍨嬫暟鎹噺鐨勮〃鎬ц兘褰卞搷杈冨ぇ銆傛暟鎹鏍煎紡锛欼nnodb鐨勬暟鎹鏍煎紡杈僊yisam鏇翠负澶嶆潅锛屽寘鎷涓増鏈殑鏁版嵁琛屽拰鍥炴粴娈电瓑缁撴瀯锛岃繖涔熶娇寰桰nnodb鍦ㄤ竴浜涚壒瀹氬満鏅笅鎬ц兘涓嶅myisam銆?濡傛灉搴旂敤闇€瑕佹敮鎸佷簨鍔″鐞嗘垨澶栭敭绾︽潫锛屾垨闇€瑕佸湪楂樺苟鍙戞儏鍐典笅鑾峰緱鏇村ソ鐨勬€ц兘锛屽缓璁娇鐢↖nnodb瀛樺偍寮曟搸锛涘鏋滃簲鐢ㄥ鏁版嵁涓€鑷存€ц姹備笉楂橈紝浣嗛渶瑕佸揩閫熸煡璇紝鍙互鑰冭檻浣跨敤myisam瀛樺偍寮曟搸銆? 瀛樺偍寮曟搸鐨勯€夋嫨

innodb: 鏄疢ysql鐨勯粯璁ゅ瓨鍌ㄥ紩鎿庯紝鏀寔浜嬪姟銆佸閿€傚鏋滃簲鐢ㄥ浜嬪姟鐨勫畬鏁存€ф湁姣旇緝楂樼殑瑕佹眰锛屽湪骞跺彂鏉′欢涓嬭姹傛暟鎹殑涓€鑷存€э紝鏁版嵁鎿嶄綔闄や簡鎻掑叆鍜屾煡璇箣澶栵紝杩樺寘鍚緢澶氱殑鏇存柊銆佸垹闄ゆ搷 浣滐紝閭d箞innodb瀛樺偍寮曟搸鏄瘮杈冨悎閫傜殑閫夋嫨銆侻yisam 锛?濡傛灉搴旂敤鏄互璇绘搷浣滃拰鎻掑叆鎿嶄綔涓轰富锛屽彧鏈夊緢灏戠殑鏇存柊鍜屽垹闄ゆ搷浣滐紝骞朵笖瀵逛簨鍔$殑瀹?鏁存€с€佸苟鍙戞€ц姹備笉鏄緢楂橈紝閭d箞閫夋嫨杩欎釜瀛樺偍寮曟搸鏄潪甯稿悎閫傜殑銆侻emory锛氬皢鎵€鏈夋暟鎹繚瀛樺湪鍐呭瓨涓紝璁块棶閫熷害蹇紝閫氬父鐢ㄤ簬涓存椂琛ㄥ強缂撳瓨銆侻emory鐨勭己闄峰氨鏄?瀵硅〃鐨勫ぇ灏忔湁闄愬埗锛屽お澶х殑琛ㄦ棤娉曠紦瀛樺湪鍐呭瓨涓紝鑰屼笖鏃犳硶淇濋殰鏁版嵁鐨勫畨鍏ㄦ€с€?

鐩稿叧闈㈣瘯棰?/p>

2銆佺储寮?/p>

2.1 绱㈠紩浠嬬粛

浠€涔堟槸绱㈠紩锛?/p>

鈥冣€冪储寮曟槸涓€绉嶆暟鎹粨鏋勶紝鐢ㄤ簬蹇€熷畾浣嶆暟鎹簱琛ㄤ腑鐗瑰畾鏁版嵁鐨勪綅缃€傜储寮曞彲浠ョ湅浣滄槸涓€鏈功鐨勭洰褰曪紝瀹冭褰曚簡涔︿腑鍏抽敭瀛楀嚭鐜扮殑椤电爜锛岃鑰呭彲浠ラ€氳繃鏌ラ槄鐩綍蹇€熷畾浣嶅埌鎵€闇€鍐呭锛岃€屼笉蹇呬竴涓竴涓湴缈婚槄鏁存湰涔︺€?/p>

鈥冣€冨湪鏁版嵁涔嬪锛屾暟鎹簱绯荤粺杩樼淮鎶ょ潃婊¤冻 鐗瑰畾鏌ユ壘绠楁硶鐨勬暟鎹粨鏋勶紝杩欎簺鏁版嵁缁撴瀯浠ユ煇绉嶆柟寮忓紩鐢紙鎸囧悜锛夋暟鎹紝 杩欐牱灏卞彲浠ュ湪杩欎簺鏁版嵁缁撴瀯 涓婂疄鐜伴珮绾ф煡鎵剧畻娉曪紝杩欑鏁版嵁缁撴瀯灏辨槸绱㈠紩銆?/p>

鈥冣€冨湪鏁版嵁搴撲腑锛岀储寮曞彲浠ユ彁楂樻煡璇㈡晥鐜囷紝鍔犲揩鏁版嵁鐨勬绱㈤€熷害銆傚畠浠彲浠ュ噺灏戞暟鎹簱闇€瑕佹壂鎻忕殑鏁版嵁閲忥紝浠庤€岄檷浣庢煡璇㈡墍闇€鐨勬椂闂村拰璧勬簮鎴愭湰銆備娇鐢ㄧ储寮曞彲浠ヤ娇寰楁煡璇㈡搷浣滅殑閫熷害澶уぇ鎻愰珮锛屽挨鍏舵槸鍦ㄥ鐞嗗ぇ閲忔暟鎹椂鏇翠负鏄庢樉銆?/p>

鈥冣€冪储寮曞彲浠ュ垱寤哄湪琛ㄧ殑涓€涓垨澶氫釜鍒椾笂锛岄€氬父浣跨敤b-tree鎴栧搱甯岀瓑鏁版嵁缁撴瀯瀹炵幇銆傚湪鏌ヨ鏁版嵁鏃讹紝鏁版嵁搴撲細鍏堟煡鎵剧储寮曪紝鏍规嵁绱㈠紩涓殑鎸囧悜鏁版嵁鐨勬寚閽堬紝鐒跺悗鍐嶅幓琛ㄤ腑鏌ユ壘鐩稿簲鐨勬暟鎹€?/p>

鈥冣€冮渶瑕佹敞鎰忕殑鏄紝铏界劧绱㈠紩鍙互鎻愰珮鏌ヨ鏁堢巼锛屼絾鏄繃澶氭垨涓嶆伆褰撶殑绱㈠紩涔熶細褰卞搷鏁版嵁搴撶殑鎬ц兘锛屽洜姝ら渶瑕佹牴鎹叿浣撶殑搴旂敤鍦烘櫙鍜屾煡璇㈤渶姹傛潵閫夋嫨鍚堥€傜殑绱㈠紩锛岄伩鍏嶅嚭鐜扮储寮曡繃澶氭垨閲嶅鍒涘缓鐨勬儏鍐点€?/p>

绱㈠紩鐨勪綔鐢ㄦ槸浠€涔堬紵

鎻愰珮鏌ヨ閫熷害锛氶€氳繃浣跨敤绱㈠紩锛屽彲浠ュ揩閫熷畾浣嶆暟鎹簱琛ㄤ腑鐨勭壒瀹氭暟鎹紝浠庤€屾彁楂樻煡璇㈡晥鐜囥€傚姞閫熸帓搴忥細濡傛灉鏌ヨ娑夊強鍒癘rder by璇彞锛岀储寮曞彲浠ュ姞閫熸帓搴忔搷浣滐紝浣垮緱鏌ヨ缁撴灉鏇村揩鍦拌繑鍥炪€傚姞閫熻繛鎺ワ細濡傛灉鏌ヨ娑夊強鍒癑oin鎿嶄綔锛岀储寮曞彲浠ュ姞閫熻繛鎺ユ搷浣滐紝鎻愰珮鏌ヨ鏁堢巼銆傜害鏉熷敮涓€鎬э細閫氳繃鍦ㄥ垪涓婂垱寤哄敮涓€绱㈠紩锛屽彲浠ョ‘淇濆垪涓殑鏁版嵁鍞竴鎬э紝閬垮厤閲嶅鏁版嵁鐨勬彃鍏ャ€傜害鏉熷閿細閫氳繃鍦ㄨ〃涔嬮棿鍒涘缓澶栭敭绱㈠紩锛屽彲浠ョ‘淇濇暟鎹殑瀹屾暣鎬у拰涓€鑷存€э紝閬垮厤鍑虹幇鏁版嵁涓嶄竴鑷寸殑鎯呭喌銆? 绱㈠紩鐨勪紭缂虹偣锛?/p>

浼樼偣锛?

鍔犻€熸帓搴忥細濡傛灉鏌ヨ娑夊強鍒癘rder by璇彞锛岀储寮曞彲浠ュ姞閫熸帓搴忔搷浣滐紝浣垮緱鏌ヨ缁撴灉鏇村揩鍦拌繑鍥炪€傚姞閫熻繛鎺ワ細濡傛灉鏌ヨ娑夊強鍒癑oin鎿嶄綔锛岀储寮曞彲浠ュ姞閫熻繛鎺ユ搷浣滐紝鎻愰珮鏌ヨ鏁堢巼銆傜害鏉熷敮涓€鎬э細閫氳繃鍦ㄥ垪涓婂垱寤哄敮涓€绱㈠紩锛屽彲浠ョ‘淇濆垪涓殑鏁版嵁鍞竴鎬э紝閬垮厤閲嶅鏁版嵁鐨勬彃鍏ャ€傜害鏉熷閿細閫氳繃鍦ㄨ〃涔嬮棿鍒涘缓澶栭敭绱㈠紩锛屽彲浠ョ‘淇濇暟鎹殑瀹屾暣鎬у拰涓€鑷存€э紝閬垮厤鍑虹幇鏁版嵁涓嶄竴鑷寸殑鎯呭喌銆?缂虹偣锛?

澧炲姞瀛樺偍绌洪棿锛氱储寮曚細鍗犵敤涓€瀹氱殑瀛樺偍绌洪棿锛屽鏋滅储寮曡繃澶氭垨鑰呬笉鎭板綋锛屼細澧炲姞鏁版嵁搴撶殑瀛樺偍绌洪棿鍜岀淮鎶ゆ垚鏈€傞檷浣庡啓鍏ユ€ц兘锛氬湪鎻掑叆銆佹洿鏂板拰鍒犻櫎鎿嶄綔鏃讹紝鏁版嵁搴撻渶瑕佸悓姝ユ洿鏂扮储寮曪紝杩欏彲鑳戒細闄嶄綆鍐欏叆鎬ц兘銆備笉閫傜敤浜庡皬琛細瀵逛簬灏忚〃鑰岃█锛屼娇鐢ㄧ储寮曞彲鑳戒細闄嶄綆鏌ヨ鏁堢巼锛屽洜涓烘暟鎹簱闇€瑕佽鍙栫储寮曟暟鎹紝杩欎細澧炲姞棰濆鐨勫紑閿€銆備笉閫傜敤浜庨绻佹洿鏂扮殑鍒楋細濡傛灉鍒楃粡甯搁渶瑕佹洿鏂帮紝閭d箞绱㈠紩浼氬鍔犳暟鎹簱鐨勭淮鎶ゆ垚鏈紝骞朵笖浼氶檷浣庢洿鏂版搷浣滅殑鎬ц兘 浣跨敤绱㈠紩鍜屼笉浣跨敤绱㈠紩杩涜鏌ユ壘

鏃犵储寮曟椂锛屾煡璇竴鏉¤褰曪紝鏌ユ壘鏂瑰紡鏄叏琛ㄦ壂鎻忥紙涓€鑸槸鐩存帴鏍规嵁涓婚敭 id 浠庡皬鍒板ぇ杩涜閬嶅巻锛岀洿鍒版壘鍒板甫鏌ユ壘鐨勮褰曪級锛屾晥鐜囩壒鍒綆锛涙湁绱㈠紩鏃讹紝濡傛灉鎴戜滑閽堝浜庤繖寮犺〃寤虹珛浜嗙储寮曪紝鍋囪绱㈠紩缁撴瀯灏辨槸浜屽弶鏍戯紝閭d箞涔熷氨鎰忓懗鐫€锛屼細瀵?age 杩欎釜瀛楁寤?绔嬩竴涓簩鍙夋爲鐨勭储寮曠粨鏋勩€傛鏃舵垜浠湪杩涜鏌ヨ鏃讹紝鍙渶瑕佹壂鎻忎笁娆″氨鍙互鎵惧埌鏁版嵁浜嗭紝鏋佸ぇ鐨勬彁楂樼殑鏌ヨ鐨勬晥鐜囥€? 澶囨敞锛?杩欓噷鎴戜滑鍙槸鍋囪绱㈠紩鐨勭粨鏋勬槸浜屽弶鏍戯紝浠嬬粛涓€涓嬬储寮曠殑澶ф鍘熺悊锛屽彧鏄竴涓ず鎰忓浘锛屽苟 涓嶆槸绱㈠紩鐨勭湡瀹炵粨鏋勶紝绱㈠紩鐨勭湡瀹炵粨鏋勬槸 b-tree 鎴?b tree 鏁版嵁缁撴瀯瀹炵幇

2.2 绱㈠紩鐨勭粨鏋?/p>

2.2.1 鍓嶇疆鐭ヨ瘑

涓昏鏄涔犱竴涓嬫暟鎹粨鏋勬爲鐨勭浉鍏崇煡璇?/p>

鏍?

浜屽弶鏍?

浜屽垎鎼滅储鏍?

骞宠 鏍?

绾㈤粦鏍?

澶氳矾鎼滅储鏍?

b鏍?

b 鏍?

b-鏍?

鏍?/p>

浠€涔堟槸鏍戯紵

鏍戯紙tree锛夋槸涓€绉嶉潪绾挎€ф暟鎹粨鏋勶紝瀹冪敱n锛坣>=0锛変釜鑺傜偣缁勬垚涓€涓湁灞傛鍏崇郴鐨勯泦鍚堬紝鍏朵腑涓€涓妭鐐硅瀹氫箟涓烘牴鑺傜偣锛屽叾浣欒妭鐐瑰彲鍒嗕负m涓簰涓嶇浉浜ょ殑瀛愰泦锛屾瘡涓瓙闆嗘湰韬篃鏄竴涓爲锛屽苟绉颁负璇ユ牴鑺傜偣鐨勫瓙鏍戙€?/p>

鏍戠殑鐗圭偣锛氭瘡涓妭鐐规渶澶氭湁m涓瓙鑺傜偣锛涙爲涓笉瀛樺湪鐜矾锛堝洖璺級锛涙牴鑺傜偣鍒颁换鎰忚妭鐐圭殑璺緞鍞竴锛涙墍鏈夎妭鐐归兘鍙互浠庢牴鑺傜偣鍒拌揪銆? 浜屽弶鏍?/p>

浠€涔堟槸浜屽弶鏍戯紵

浜屽弶鏍戯紙binary tree锛夋槸涓€绉嶇壒娈婄殑鏍戠粨鏋勶紝瀹冪殑姣忎釜鑺傜偣鏈€澶氬彧鏈変袱涓瓙鑺傜偣锛屽垎鍒о涓哄乏瀛愯妭鐐瑰拰鍙冲瓙鑺傜偣銆傚乏瀛愯妭鐐瑰拰鍙冲瓙鑺傜偣鐨勯『搴忔槸鍥哄畾鐨勶紝涓嶅彲浠ヤ氦鎹€傚鏋滄煇涓妭鐐规病鏈夊瓙鑺傜偣锛岄偅涔堝畠鐨勫乏瀛愯妭鐐瑰拰鍙冲瓙鑺傜偣閮戒负 null銆?/p>

浜屽弶鏍戠殑鐗圭偣锛氭瘡涓妭鐐规渶澶氭湁涓や釜瀛愯妭鐐癸紝宸﹀瓙鑺傜偣鍜屽彸瀛愯妭鐐广€傚乏瀛愯妭鐐瑰湪鏍戜腑鐨勪綅缃瘮鍙冲瓙鑺傜偣鍏堛€傛瘡涓妭鐐归兘鏈変竴涓埗鑺傜偣锛岄櫎浜嗘牴鑺傜偣娌℃湁鐖惰妭鐐广€傚鏋滄煇涓妭鐐规病鏈夊瓙鑺傜偣锛岄偅涔堝畠鐨勫乏瀛愯妭鐐瑰拰鍙冲瓙鑺傜偣閮戒负 null銆? 浜屽垎鎼滅储鏍?/p>

浠€涔堟槸浜屽垎鎼滅储鏍戯紵

浜屽垎鎼滅储鏍戯紙binary search tree锛夛紝涔熺о涓轰簩鍙夋煡鎵炬爲锛圔inary sort tree锛夋垨浜屽弶鎼滅储鏍戯紙bst锛夛紝鏄竴绉嶅熀浜庝簩鍙夋爲鐨勬暟鎹粨鏋勶紝鑳藉楂樻晥鍦板疄鐜版暟鎹殑鏌ユ壘銆佹彃鍏ュ拰鍒犻櫎绛夋搷浣溿€?/p>

浜屽垎鎼滅储鏍戠殑鐗圭偣锛氭瘡涓妭鐐规渶澶氭湁涓や釜瀛愯妭鐐癸紝宸﹀瓙鑺傜偣鐨勫€煎皬浜庣埗鑺傜偣鐨勫€硷紝鍙冲瓙鑺傜偣鐨勫€煎ぇ浜庣埗鑺傜偣鐨勫€笺€? 浜屽垎鎼滅储鏍戠殑浼樼己鐐?/p>

浼樼偣锛氭暟鎹彲浠ユ寜鐓уぇ灏忛『搴忔帓鍒楋紝鍙互楂樻晥鍦拌繘琛屾煡鎵惧拰鎺掑簭鎿嶄綔锛涘悓鏃讹紝浜屽垎鎼滅储鏍戠殑缁撴瀯绠€鍗曪紝鏄撲簬瀹炵幇鍜岀悊瑙c€傜己鐐癸細鍦ㄧ壒瀹氭儏鍐典笅锛屽彲鑳戒細瀵艰嚧鏍戠殑涓嶅钩琛★紝浠庤€屽奖鍝嶆搷浣滄晥鐜囷紱姝ゅ锛屽鏋滄暟鎹垎甯冭緝涓虹鏁o紝鍙兘浼氬鑷存爲鐨勯珮搴﹁緝楂橈紝褰卞搷鎿嶄綔鏁堢巼 骞宠 鏍?/p>

浠€涔堟槸骞宠 鏍戯紵

骞宠 鏍戯紙balanced tree锛夛紝涔熺о涓鸿嚜骞宠 浜屽弶鏌ユ壘鏍戯紙self-balancing binary search tree锛夛紝鏄竴绉嶈兘澶熻嚜鍔ㄤ繚鎸佸钩琛$殑浜屽弶鏌ユ壘鏍戯紝閫氳繃鏃嬭浆鍜岄噸鏂板垎閰嶈妭鐐规潵淇濇寔鏍戠殑骞宠 锛屼粠鑰屼繚璇佹爲鐨勯珮搴﹁緝灏忥紝鏌ユ壘銆佹彃鍏ュ拰鍒犻櫎绛夋搷浣滅殑鏃堕棿澶嶆潅搴﹂兘鑳藉淇濇寔鍦ㄨ緝浣庣殑姘村钩銆?/p>

骞宠 鏍戠殑鐗圭偣锛氬钩琛℃爲鏄竴绉嶇壒娈婄殑浜屽弶鏍戯紝瀹冪殑宸﹀彸瀛愭爲鐨勯珮搴﹀樊涓嶈秴杩?1锛屼篃灏辨槸璇达紝浠讳綍涓€涓妭鐐圭殑宸﹀彸瀛愭爲鐨勯珮搴︿箣宸兘涓嶈秴杩?1銆? 骞宠 鏍戠殑浼樼己鐐?/p>

浼樼偣锛氳兘澶熷揩閫熷湴杩涜鏌ユ壘銆佹彃鍏ュ拰鍒犻櫎绛夋搷浣滐紝骞朵笖鍙互淇濊瘉鍦ㄦ渶鍧忔儏鍐典笅鐨勬椂闂村鏉傚害涓嶈秴杩?o(log n)銆傚湪澶勭悊澶ч噺鏁版嵁鏃讹紝骞宠 鏍戣兘澶熸湁鏁堝湴鎻愰珮鏁版嵁鐨勬煡璇㈡晥鐜囷紝閬垮厤浜嗕簩鍙夋爲閫€鍖栨垚閾捐〃鐨勬儏鍐电己鐐癸細瀹炵幇鐩稿澶嶆潅锛涚┖闂村崰鐢ㄨ緝楂橈紱鍙兘瀛樺湪杩囧害璋冩暣浠庤€岄檷浣庢€ц兘锛涗笉閫傜敤浜庨绻佺殑鎻掑叆鍜屽垹闄? 绾㈤粦鏍?/p>

浠€涔堟槸绾㈤粦鏍戯紵

绾㈤粦鏍戯紙red-black tree锛夋槸涓€绉嶈嚜骞宠 浜屽弶鏌ユ壘鏍戯紝瀹冨叿鏈夎壇濂界殑骞宠 鎬у拰鎼滅储鎬ц兘锛岃骞挎硾搴旂敤浜庡悇绉嶆暟鎹粨鏋勫拰绠楁硶涓紝渚嬪c stl搴撲腑鐨刴ap鍜宻et锛孞ava鐨凾reemap鍜孴reeset绛夈€?/p>

绾㈤粦鏍戠殑鐗圭偣锛氭瘡涓妭鐐归兘鏄孩鑹叉垨榛戣壊锛涙牴鑺傜偣鏄粦鑹茬殑锛涙瘡涓彾瀛愯妭鐐归兘鏄粦鑹茬殑锛涘鏋滀竴涓妭鐐规槸绾㈣壊鐨勶紝鍒欏畠鐨勪袱涓瓙鑺傜偣閮芥槸榛戣壊鐨勶紱浠绘剰涓€鏉′粠鏍硅妭鐐瑰埌鍙跺瓙鑺傜偣鐨勮矾寰勯兘鍖呭惈鐩稿悓鏁扮洰鐨勯粦鑹茶妭鐐癸紱绾㈤粦鏍戠殑鏈€闀胯矾寰勪笉浼氳秴杩囨渶鐭矾寰勭殑涓ゅ€? 绾㈤粦鏍戠殑浼樼己鐐?/p>

浼樼偣锛氭彃鍏ャ€佸垹闄ゅ拰鏌ユ壘鎿嶄綔鐨勬椂闂村鏉傚害閮芥槸 o(log n)锛屽叾涓?n 鏄爲涓妭鐐圭殑鏁伴噺銆傝繖浣垮緱绾㈤粦鏍戝湪鍚勭鎿嶄綔涓兘鍏锋湁寰堝ソ鐨勬晥鐜囷紱绾㈤粦鏍戝叿鏈夌浉瀵瑰钩琛$殑鎬ц川锛屼娇寰楁爲鐨勯珮搴︿笉浼氬お楂橈紝鍥犳鍦ㄦ渶鍧忔儏鍐典笅浠嶇劧鍏锋湁杈冨ソ鐨勬椂闂村鏉傚害锛涚孩榛戞爲鐩稿浜庡叾浠栧钩琛℃爲锛堝avl鏍戯級鏉ヨ锛屽畠鐨勬棆杞搷浣滄洿灏戯紝鍥犳鍦ㄥ疄鐜颁笂姣旇緝绠€鍗曘€傜己鐐癸細绾㈤粦鏍戠浉瀵逛簬鍏朵粬绠€鍗曠殑鏁版嵁缁撴瀯鏉ヨ锛屽畠鐨勫疄鐜拌緝涓哄鏉傦紝闇€瑕佽€冭檻璁稿鐗规畩鎯呭喌锛屽洜姝ゅ湪瀹炵幇鏃舵瘮杈冨洶闅撅紱绾㈤粦鏍戠浉瀵逛簬鍏朵粬鏁版嵁缁撴瀯鏉ヨ锛屽畠鐨勫瓨鍌ㄧ┖闂存瘮杈冨ぇ锛屽洜涓烘瘡涓妭鐐归兘闇€瑕佸瓨鍌ㄩ澶栫殑棰滆壊淇℃伅 澶氳矾鎼滅储鏍?/p>

浠€涔堟槸澶氳矾鎼滅储鏍戯紵

澶氳矾骞宠 鏌ユ壘鏍戞槸涓€绉嶆暟鎹粨鏋勶紝瀹冨彲浠ョ敤鏉ラ珮鏁堝湴缁存姢涓€缁勫姩鎬佸彉鍖栫殑鏈夊簭鏁版嵁銆傚畠鏄钩琛℃爲鐨勪竴绉嶅彉绉嶏紝鐩告瘮浜庝紶缁熺殑浜屽弶骞宠 鏍戯紙濡傜孩榛戞爲銆丄vl鏍戠瓑锛夛紝瀹冨厑璁镐竴涓妭鐐规嫢鏈夊涓瓙鑺傜偣锛屼粠鑰屽彲浠ュ噺灏戞爲鐨勯珮搴︼紝鎻愰珮鏌ユ壘銆佹彃鍏ャ€佸垹闄ょ瓑鎿嶄綔鐨勬晥鐜囥€?/p>

澶氳矾鎼滅储鏍戠殑鐗圭偣锛氫竴涓妭鐐瑰彲浠ユ嫢鏈夊涓瓙鑺傜偣锛岄€氬父绉颁负鈥滃害鈥濄€傛瘡涓妭鐐瑰彲浠ュ寘鍚涓叧閿瓧锛岄€氬父鎸夌収浠庡皬鍒板ぇ鐨勯『搴忔帓鍒椼€備负浜嗕繚鎸佸钩琛★紝鍚勪釜鑺傜偣涔嬮棿鐨勫叧閿瓧鑼冨洿鏄浉閭讳笖涓嶉噸鍙犵殑銆傛瘡涓妭鐐圭殑瀛愯妭鐐规寜鐓т竴瀹氱殑椤哄簭鎺掑垪锛屽彲浠ュ揩閫熷湴杩涜浜屽垎鏌ユ壘銆? 澶氳矾鎼滅储鏍戠殑浼樼己鐐癸細

浼樼偣锛氬彲浠ラ珮鏁堝湴澶勭悊澶ч噺鏁版嵁锛岄伩鍏嶉绻佺殑纾佺洏i/o鎿嶄綔銆傛彃鍏ャ€佸垹闄ゃ€佹煡鎵剧瓑鎿嶄綔鐨勬椂闂村鏉傚害閮芥瘮杈冪ǔ瀹氾紝閫氬父涓篛(log n)銆傚浜庤寖鍥存煡璇㈢瓑楂樼骇鎿嶄綔锛屽璺钩琛℃煡鎵炬爲姣斾紶缁熺殑骞宠 鏍戞洿鍔犻€傚悎銆傜己鐐癸細姣斾紶缁熺殑骞宠 鏍戞洿鍔犲鏉傦紝瀹炵幇闅惧害杈冨ぇ銆傚浜庡ぇ閲忛殢鏈烘彃鍏ョ殑鏁版嵁锛屽彲鑳戒細瀵艰嚧鏍戠殑楂樺害澧炲姞锛屽奖鍝嶆煡璇㈡€ц兘銆傝妭鐐圭殑搴︽暟澧炲姞浼氬甫鏉ラ澶栫殑瀛樺偍绌洪棿寮€閿€銆? b鏍?/p>

浠€涔堟槸b鏍戯紵

b鏍戯紙b-tree锛夋槸涓€绉嶅璺悳绱㈡爲锛屽父鐢ㄤ簬缁勭粐鍜岀鐞嗙鐩樻垨鍏朵粬鐩存帴瀛樺彇杈呭姪璁惧鐨勬枃浠剁郴缁熷拰鏁版嵁搴?/p>

b鏍戠殑鐗圭偣锛氳兘澶熻嚜閫傚簲鍦拌皟鏁磋妭鐐圭殑澶у皬锛屼娇寰楁爲鐨勯珮搴︽洿浣庯紝浠庤€屽噺灏戜簡纾佺洏i/o鎿嶄綔鐨勬鏁帮紝鎻愰珮浜嗘暟鎹殑鏌ヨ鏁堢巼 b鏍戠殑浼樼己鐐癸細

浼樼偣锛氭敮鎸侀珮鏁堢殑闅忔満璁块棶鍜岃寖鍥存煡璇紱閫傚悎纾佺洏鎴栧叾浠栫洿鎺ュ瓨鍙栬緟鍔╄澶囩殑鏂囦欢绯荤粺鍜屾暟鎹簱锛涜兘澶熻嚜閫傚簲鍦拌皟鏁磋妭鐐瑰ぇ灏忕己鐐癸細瀹炵幇鐩稿澶嶆潅锛涘唴瀛樻秷鑰楄緝楂橈紱閫傚悎澶ч噺鏁版嵁 b 鏍?/p>

浠€涔堟槸b 鏍戯紵

b tree 鏄竴绉嶅璺钩琛℃煡鎵炬爲锛屽畠鏄疊鏍戠殑涓€绉嶅彉浣擄紝甯哥敤浜庢枃浠剁郴缁熷拰鏁版嵁搴撶储寮曠瓑搴旂敤涓?/p>

b 鏍戠殑鐗圭偣锛氭墍鏈夊叧閿瓧閮藉湪鍙跺瓙鑺傜偣涓婂嚭鐜帮紝鍐呴儴鑺傜偣鍙寘鍚叧閿瓧鐨勭储寮曚俊鎭紝浣垮緱鏌ヨ鏃惰兘澶熸洿蹇湴瀹氫綅鍒板彾瀛愯妭鐐癸紱鎵€鏈夊彾瀛愯妭鐐归兘鍖呭惈浜嗙浉鍚岀殑淇℃伅锛屼笖鎸夌収鍏抽敭瀛楃殑澶у皬椤哄簭閾炬帴鍦ㄤ竴璧凤紝鏂逛究鎸夎寖鍥存煡鎵惧拰閬嶅巻锛涘唴閮ㄨ妭鐐逛笌鍙跺瓙鑺傜偣鐨勭粨鏋勭浉鍚岋紝涓旀瘡涓妭鐐圭殑鍏抽敭瀛楁暟閲忛兘浠嬩簬

n

/

2

n/2

n/2 鍜?

n

n

n 涔嬮棿锛屽叾涓?

n

n

n 鏄妭鐐圭殑鏈€澶у叧閿瓧鏁伴噺锛岃繖淇濊瘉浜嗘爲鐨勫钩琛℃€у拰鎬ц兘鐨勭ǔ瀹氭€с€? b 鏍戠殑浼樼己鐐癸細

浼樼偣锛欱 鏍戣兘澶熸洿蹇湴瀹氫綅鍒板彾瀛愯妭鐐癸紝鍥犳鏌ヨ鎬ц兘鏇村ソ锛汢 鏍戝唴閮ㄨ妭鐐瑰彧瀛樺偍绱㈠紩淇℃伅锛屽洜姝ょ浉瀵逛簬b鏍戯紝瀹冨叿鏈夋洿濂界殑绌洪棿鍒╃敤鐜囷紱b 鏍戠殑鍙跺瓙鑺傜偣閮芥寜鐓у叧閿瓧澶у皬椤哄簭閾炬帴鍦ㄤ竴璧凤紝鏂逛究鑼冨洿鏌ユ壘鍜岄亶鍘嗐€傜己鐐癸細鎻掑叆鍜屽垹闄ゆ搷浣滈渶瑕佺淮鎶ゆ寚閽堝拰閾捐〃锛屽洜姝ょ浉瀵逛簬b鏍戯紝瀹冪殑瀹炵幇鏇村姞澶嶆潅锛汢 鏍戦渶瑕佽緝澶х殑鑺傜偣锛屽洜姝ゅ浜庡皬瑙勬ā鐨勬暟鎹泦鏉ヨ锛屽彲鑳戒笉濡傚叾浠栨暟鎹粨鏋勬潵寰楅珮鏁? b-鏍?/p>

浠€涔堟槸b-鏍戯紵

b-tree 鏄竴绉嶅璺钩琛℃煡鎵炬爲锛屾槸b 鏍戠殑鍙樹綋涔嬩竴銆備笌b 鏍戠被浼硷紝b-鏍戜篃鏄湪姣忎釜鑺傜偣涓鍔犱簡鏇村鐨勫叧閿瓧锛屼互鍑忓皯鏍戠殑楂樺害锛屾彁楂樻煡鎵炬晥鐜囥€?/p>

b-鏍戠殑鐗圭偣锛氭瘡涓妭鐐瑰瓨鍌ㄧ殑鍏抽敭瀛椾釜鏁颁负m-1涓紝瀛愯妭鐐规暟鐩湪2~m涔嬮棿锛涙墍鏈夊彾瀛愯妭鐐归兘鍦ㄥ悓涓€灞傦紝涓斾笉鍖呭惈浠讳綍淇℃伅锛屽彧璧峰埌鎵胯浇鍏抽敭瀛楃殑浣滅敤锛涢潪鍙跺瓙鑺傜偣瀛樺偍鐨勫叧閿瓧鏁伴噺姣擝 鏍戞洿灏戯紱鐩稿浜嶣 鏍戯紝姣忎釜鑺傜偣鐨勫瓨鍌ㄧ┖闂村埄鐢ㄧ巼鏇撮珮锛汢-鏍戝彲浠ヤ綔涓哄閮ㄥ瓨鍌ㄧ殑鏁版嵁缁撴瀯锛屽洜涓哄叾鍦ㄥ瓨鍌ㄦ椂鍙互灏嗚妭鐐瑰瓨鍌ㄥ湪纾佺洏涓娿€? b-鏍戠殑浼樼己鐐癸細

浼樼偣锛欱-鏍戠殑鑺傜偣鏇村ぇ锛屼娇寰楁爲鐨勯珮搴︽洿浣庯紝璁块棶閫熷害鏇村揩锛汢-鏍戣兘澶熷湪涓嶅悓鑺傜偣闂磋繘琛岃寖鍥存煡鎵撅紱b-鏍戞敮鎸佽緝楂樼殑骞跺彂鎬у拰浜嬪姟鎬ф搷浣溿€傜己鐐癸細b-鏍戠殑鏌ヨ鎬ц兘姣擝 鏍戠暐浣庯紱鎻掑叆鍜屽垹闄ゆ搷浣滄椂闇€瑕佺淮鎶よ妭鐐逛箣闂寸殑骞宠 锛屽洜姝-鏍戠殑缁存姢鎴愭湰姣擝 鏍戦珮锛汢-鏍戠殑鑺傜偣鏇村ぇ锛屽瓨鍌ㄧ┖闂寸殑鍒╃敤鐜囦笉濡侭 鏍戙€?

2.2.2 绱㈠紩缁撴瀯鐨勭绫?/p>

鏈皬鑺備富瑕佷簡瑙d簩鍒嗘悳绱㈡爲銆佺孩榛戞爲銆丅 鏍戝疄鐜扮储寮曟椂鍚勮嚜鐨勭壒鐐癸紝浠ュ強hash绱㈠紩

绱㈠紩缁撴瀯鐨勭绫?mysql鐨勭储寮曟槸鍦ㄥ瓨鍌ㄥ紩鎿庡眰瀹炵幇鐨勶紝涓嶅悓鐨勫瓨鍌ㄥ紩鎿庢湁涓嶅悓鐨勭储寮曠粨鏋勶紝涓昏鍖呭惈浠ヤ笅鍑犵锛? 涓嶅悓瀛樺偍寮曟搸鎵€鏀寔鐨勭储寮曠粨鏋勶細

娉ㄦ剰锛?鎴戜滑骞冲父鎵€璇寸殑绱㈠紩锛屽鏋滄病鏈夌壒鍒寚鏄庯紝閮芥槸鎸嘊 鏍戠粨鏋勭粍缁囩殑绱㈠紩

涓嶅悓鏁版嵁缁撴瀯鐨勭储寮?/p>

濡傛灉鎴戜滑浣跨敤浜屽垎鎼滅储鏍戜綔涓虹储寮曠粨鏋?鐞嗘兂鎯呭喌涓嬬殑浜屽垎鎼滅储鏍戯細 鐪嬬潃杩樿锛屼絾鏄亣濡傜储寮曢兘鏄寜鐓ч『搴忕殑锛堟瘮濡傜储寮曟槸鑷鐨?锛屾鏃朵簩鍙夋爲灏卞彉鎴愯繖鏍蜂簡锛? 鍍忎笂闈㈤偅鏍凤紝浜屽弶鏍戝氨鍙樻垚浜嗕竴涓崟鍚戦摼琛紝杩欐牱鏋佸ぇ鍦伴檷浣庝簡鏌ヨ鐨勬€ц兘 濡傛灉鎴戜滑浣跨敤绾㈤粦鏍戜綔涓虹储寮曠粨鏋勶細 浣嗘槸浠嶇劧瀛樺湪涓€瀹氱殑闂锛岄偅灏辨槸褰撴暟鎹噺鐗瑰埆澶х殑鏃跺€欙紝浜屽弶鏍戠殑灞傛浼氱壒鍒繁锛屼細瀵艰嚧鏌ユ壘鏁堢巼鐗瑰埆浣? 鎵€浠ュ湪mysql鐨勭储寮曠粨鏋勪腑锛屽苟娌℃湁閫夋嫨浜屽弶鏍戞垨鑰呯孩榛戞爲锛岃€岄€夋嫨鐨勬槸b 鏍? 鎵€鏈夊厓绱犻兘浼氬嚭鐜板湪鍙跺瓙鑺傜偣涓紝鍙跺瓙鑺傜偣褰㈡垚浜嗕竴涓崟鍚戦摼琛紝浠庤€屾彁楂樹簡璁块棶鏁堢巼 涓嶅悓鏁版嵁缁撴瀯瀹炵幇鐨勭储寮曚箣闂寸殑姣旇緝

浜屽垎鎼滅储鏍戯細

鏁版嵁閲忓皬鐨勬椂鍊欙紝鍙互杩涜楂樻晥鍦版煡鎵惧拰鎺掑簭鎿嶄綔锛屽悓鏃剁粨鏋勭畝鍗曪紝鏄撲簬瀹炵幇鍜岀悊瑙eぇ鏁版嵁閲忔儏鍐典笅锛屽眰绾ц緝娣憋紝妫€绱㈤€熷害鎱紝鐗瑰埆鏄储寮曟湁搴忕殑鎯呭喌涓嬶紝浼氬鑷存暟鎹垎甯冪壒鍒暎锛岀敋鑷冲舰鎴愮敤杩囧崟鍚戦摼琛?绾㈤粦鏍戯細

鎻掑叆銆佸垹闄ゃ€佹煡鎵剧瓑鎿嶄綔鐨勬椂闂村鏉傚害閮芥槸

o

(

l

o

g

n

)

o(logn)

o(logn)锛岀浉瀵硅€岃█鏌ユ壘鏁堢巼姣旇緝楂橈紝骞朵笖姣旇緝绋冲畾绾㈤粦鏍戠浉瀵瑰叾瀹冩暟鎹粨鏋勫疄鐜版瘮杈冨鏉傦紝鍗犵敤鍐呭瓨涔熸瘮杈冨锛岀壒鍒槸鏁版嵁閲忕壒鍒ぇ鐨勬椂鍊欙紝鏍戠殑灞傛浼氭瘮杈冩繁锛屼粠鑰屽ぇ骞呴檷浣庢煡璇㈡晥鐜?b 鏁帮細

鎵€鏈夊厓绱犻兘鍑虹幇鍦ㄥ彾瀛愯妭鐐逛笂锛屽苟涓斿舰鎴愪竴涓弻鍚戦摼琛紝b 鏍戠殑闈炲彾瀛愯妭鐐瑰彧瀛樺偍绱㈠紩锛屾墍浠ュ嵆浣垮湪澶ф暟鎹儏鍐典笅锛屾煡璇㈡€ц兘渚濇棫姣旇緝濂戒絾鏄秷鑰楃殑鍐呭瓨姣旇緝澶э紝涓嶉€傚悎瀛樺偍灏忚妯℃暟鎹紝瀹炵幇璧锋潵涔熺浉瀵规洿鍔犲鏉? 鐩稿叧闈㈣瘯棰? 杩樻湁鍏跺畠鐨勬暟鎹粨鏋勫疄鐜扮储寮曪紝姣斿hash缁撴瀯 鍝堝笇绱㈠紩灏辨槸閲囩敤涓€瀹氱殑hash绠楁硶锛屽皢閿€兼崲绠楁垚鏂扮殑hash鍊硷紝鏄犲皠鍒板搴旂殑妲戒綅涓婏紝鐒跺悗瀛樺偍鍦?hash琛ㄤ腑銆傚鏋滀袱涓?鎴栧涓?閿€硷紝鏄犲皠鍒颁竴涓浉鍚岀殑妲戒綅涓婏紝浠栦滑灏变骇鐢熶簡hash鍐茬獊锛堜篃绉颁负hash纰版挒锛夛紝鍙互閫氳繃閾捐〃鏉ヨв鍐炽€?/p>

鐗圭偣锛?

hash绱㈠紩鍙兘鐢ㄤ簬瀵圭瓑姣旇緝(=锛宨n)锛屼笉鏀寔鑼冨洿鏌ヨ锛坆etween锛?锛? 锛屸€︼級鏃犳硶鍒╃敤绱㈠紩瀹屾垚鎺掑簭鎿嶄綔鏌ヨ鏁堢巼楂橈紝閫氬父(涓嶅瓨鍦╤ash鍐茬獊鐨勬儏鍐?鍙渶瑕佷竴娆℃绱㈠氨鍙互浜嗭紝鏁堢巼閫氬父瑕侀珮浜嶣 tree绱?寮?瀛樺偍寮曟搸鏀寔锛氬湪mysql涓紝鏀寔hash绱㈠紩鐨勬槸memory瀛樺偍寮曟搸銆?鑰孖nnodb涓叿鏈夎嚜閫傚簲hash鍔熻兘锛宧ash绱㈠紩鏄?innodb瀛樺偍寮曟搸鏍规嵁b tree绱㈠紩鍦ㄦ寚瀹氭潯浠朵笅鑷姩鏋勫缓鐨勩€?

2.3 绱㈠紩鐨勫垎绫?/p>

绱㈠紩鐨勫垎绫?/p>

鍞竴绱㈠紩锛氬湪鍒椾笂鍒涘缓鍞竴绱㈠紩锛岀‘淇濆垪涓殑鏁版嵁鍞竴鎬э紝閬垮厤閲嶅鏁版嵁鐨勬彃鍏ャ€備富閿储寮曪細涓婚敭鏄竴绉嶇壒娈婄殑鍞竴绱㈠紩锛岀敤浜庡敮涓€鏍囪瘑琛ㄤ腑鐨勬瘡涓€琛屾暟鎹紝涓婚敭绱㈠紩涔熸槸涓€绉嶈仛绨囩储寮曘€傜粍鍚堢储寮曪細鍦ㄥ涓垪涓婂垱寤鸿仈鍚堢储寮曪紝鍙互鎻愰珮鏌ヨ鏁堢巼锛屼絾闇€瑕佹敞鎰忕储寮曢『搴忓拰鏌ヨ鏉′欢鐨勫尮閰嶅害銆傚叏鏂囩储寮曪細鍦ㄦ枃鏈垪涓婂垱寤哄叏鏂囩储寮曪紝鍙互鍔犲揩鏂囨湰鎼滅储鐨勯€熷害銆傜┖闂寸储寮曪細鍦ㄥ湴鐞嗕綅缃暟鎹被鍨嬬殑鍒椾笂鍒涘缓绌洪棿绱㈠紩锛屽彲浠ュ姞閫熷湴鐞嗕綅缃悳绱㈢殑閫熷害銆傝仛绨囩储寮曪紙鑱氶泦绱㈠紩锛夛細鍦ㄨ〃涓婂垱寤鸿仛绨囩储寮曪紝鍙互灏嗘暟鎹寜鐓х储寮曢『搴忓瓨鍌ㄥ湪纾佺洏涓婏紝鎻愰珮鏌ヨ鏁堢巼銆傞潪鑱氱皣绱㈠紩锛堥潪鑱氶泦绱㈠紩锛夛細鍦ㄨ〃涓婂垱寤洪潪鑱氱皣绱㈠紩锛屽彲浠ュ湪绱㈠紩涓瓨鍌ㄦ寚鍚戞暟鎹綅缃殑鎸囬拡锛屾煡璇㈡椂闇€瑕佸厛鏌ユ壘绱㈠紩锛屽啀鏍规嵁鎸囬拡鏌ユ壘鏁版嵁銆?绱㈠紩涓昏鍒嗕负鍥涘ぇ绫伙細涓婚敭绱㈠紩銆佸敮涓€绱㈠紩銆佸父瑙勭储寮曘€佸叏鏂囩储寮? 鍦↖nnodb涓紝鏍规嵁绱㈠紩鐨勫瓨鍌ㄥ舰寮忥紝鍙堝彲浠ュ垎涓轰互涓嬩袱绉嶏細 鍥剧ず锛? 澶囨敞锛?/p>

鑱氶泦绱㈠紩鐨勫彾瀛愯妭鐐逛笅鎸傜殑鏄繖涓€琛岀殑鏁版嵁 浜岀骇绱㈠紩鐨勫彾瀛愯妭鐐逛笅鎸傜殑鏄瀛楁鍊煎搴旂殑涓婚敭鍊? 鑱氶泦绱㈠紩閫夊彇瑙勫垯

濡傛灉瀛樺湪涓婚敭锛屼富閿储寮曞氨鏄仛闆嗙储寮曘€? 濡傛灉涓嶅瓨鍦ㄤ富閿紝灏嗕娇鐢ㄧ涓€涓敮涓€锛圲nique锛夌储寮曚綔涓鸿仛闆嗙储寮曘€? 濡傛灉琛ㄦ病鏈変富閿紝鎴栨病鏈夊悎閫傜殑鍞竴绱㈠紩锛屽垯innodb浼氳嚜鍔ㄧ敓鎴愪竴涓猺owid浣滀负闅愯棌鐨勮仛闆嗙储寮曘€? 鍥炶〃鏌ヨ锛氳繖绉嶅厛鍒颁簩绾х储寮曚腑鏌ユ壘鏁版嵁锛屾壘鍒颁富閿€硷紝鐒跺悗鍐嶅埌鑱氶泦绱㈠紩涓牴鎹富閿€硷紝鑾峰彇鏁版嵁鐨勬柟寮忥紝灏辩о涔嬩负鍥炶〃鏌ヨ 鍏蜂綋杩囩▼濡備笅锛?鈶? 鐢变簬鏄牴鎹畁ame瀛楁杩涜鏌ヨ锛屾墍浠ュ厛鏍规嵁name='arm鈥欏埌name瀛楁鐨勪簩绾х储寮曚腑杩涜鍖归厤鏌ユ壘銆備絾鏄湪浜岀骇绱㈠紩涓彧鑳芥煡鎵惧埌 arm 瀵瑰簲鐨勪富閿€?10銆?鈶? 鐢变簬鏌ヨ杩斿洖鐨勬暟鎹槸*锛屾墍浠ユ鏃讹紝杩橀渶瑕佹牴鎹富閿€?0锛屽埌鑱氶泦绱㈠紩涓煡鎵?0瀵瑰簲鐨勮褰曪紝鏈€缁堟壘鍒?0瀵瑰簲鐨勮row銆?鈶? 鏈€缁堟嬁鍒拌繖涓€琛岀殑鏁版嵁锛岀洿鎺ヨ繑鍥炲嵆鍙€?鐩稿叧闈㈣瘯棰?

2.4 绱㈠紩鐨勪娇鐢?/p>

2.4.1 绱㈠紩鐨勫熀鏈娇鐢?/p>

鍒涘缓绱㈠紩 create [ unique | fulltext ] index index_name on table_name (index_col_name,... ) ;

鏌ョ湅绱㈠紩 show index from table_name ;

鍒犻櫎绱㈠紩 drop index index_name on table_name ;

绀轰緥锛?/p>

绀轰緥1锛氬垱寤轰竴涓櫘閫氱储寮?/p>

mysql> create idx_user_name on tb_user(name);

mysql> show index from tb_user;

绀轰緥2锛氬垱寤轰竴涓敮涓€绱㈠紩

mysql> create unique index idx_user_phone on tb_user(phone);

mysql> show index from tb_user;

绀轰緥3锛氬垱寤鸿仈鍚堢储寮?/p>

mysql> create index idx_user_pro_age_sta on tb_user(profession, age, status);

mysql> show index from tb_user;

2.4.2 楠岃瘉绱㈠紩鐨勬晥鐜?/p>

浜嬪厛鍑嗗涓€寮犲瓨鏀?000w鏉¤褰曠殑琛?tb_sku

select * from tb_sku where id = 1\g;

# 澶囨敞锛歕g 灏嗘煡璇㈢殑缁撴灉锛屽皢姣忎釜瀛楁鎸夎灞曠ず锛屽鏋滄寜鍒楀睍绀轰笉澶柟渚挎煡鐪嬶紝鍥犱负瀛楁澶浜?/p>

1锛夋寜鍒楀睍绀猴細

\g鎸夎灞曠ず锛?/p>

鍙互鐪嬪埌杩欐潯sql鐨勬墽琛岄€熷害绾︿负0.00sec锛屽彲浠ヨ鏄浉褰撳潡浜嗭紝杩欐槸鐩稿綋蹇簡锛岃繖鏄洜涓烘垜浠槸浣跨敤涓婚敭鏌ヨ鐨勶紝涓婚敭鏄湁绱㈠紩鐨勩€?锛夌幇鍦ㄦ垜浠娇鐢ㄤ互涓嬬殑sql鏉ユ煡璇?/p>

select * from tb_sku where sn = '100000003145001';

鍙互鐪嬪埌杩欎釜浣跨敤 sn杩欎釜瀛楁杩涜鏌ヨ锛岃€楁椂12.38sec锛堟亹鎬栧鏂煒憋級锛岃繖鏄敱浜巗n鏄病鏈変娇鐢ㄧ储寮曠殑缂樻晠锛?/p>

3锛夌幇鍦ㄦ垜浠粰瀹冩坊鍔犵紭鏁呯储寮曪紝鐒跺悗鍐嶆潵娴嬭瘯涓€涓?/p>

create index idx_sku_sn on tb_sku(sn);

鍙互鐪嬪埌鏋勫缓绱㈠紩鐨勮繃绋嬫洿鍔犺€楁椂锛岃€楁椂楂樿揪 1min 13.67ses锛岃繖鏄敱浜庡垱寤虹储寮曟槸鏋勫缓涓€涓狟 鏍戯紝鍒涘缓涓€涓暟鎹粨鏋勶紝鐗瑰埆杩樻槸b 鏍戣繖绉嶆瘮杈冨鏉傜殑鏁版嵁缁撴瀯鑲畾鏄壒鍒€楁椂鐨勶紝鎵€浠ュ湪姝ゅ缓璁储寮曞敖閲忓湪寤鸿〃鐨勬椂鍊欏氨鏋勫缓

鍙互鐪嬪埌鏋勫缓绱㈠紩鍚庯紝鑰楁椂鐩存帴鐢变箣鍓嶇殑12.38sec闄嶄綆鑷?.07sec浜嗭紝浠庤繖閲屽彲浠ョ湅鍑虹储寮曠殑浣滅敤鏄涔堝法澶т簡鍚ю煒?/p>

鎬荤粨

浣跨敤绱㈠紩姣斾笉浣跨敤绱㈠紩瑕佸揩寰楀涓婚敭绱㈠紩瑕佹瘮鏅€氱储寮曞揩

2.4.3 鑱斿悎绱㈠紩

鏈皬鑺備富瑕佸涔犺仈鍚堢储寮曚腑鐨勬湁鍏崇壒鍒噸瑕佺殑娉曞垯鏈€宸﹀墠缂€娉曞垯锛屼互鍙婅寖鍥存煡璇㈡椂鐨勬渶宸﹀墠缂€娉曞垯

鏈€宸﹀墠缂€娉曞垯

濡傛灉绱㈠紩浜嗗鍒楋紙鑱斿悎绱㈠紩锛夛紝瑕侀伒瀹堟渶宸﹀墠缂€娉曞垯銆傛渶宸﹀墠缂€娉曞垯鎸囩殑鏄煡璇粠绱㈠紩鐨勬渶宸﹀垪寮€濮嬶紝 骞朵笖涓嶈烦杩囩储寮曚腑鐨勫垪銆傚鏋滆烦璺冩煇涓€鍒楋紝绱㈠紩灏嗕細閮ㄥ垎澶辨晥(鍚庨潰鐨勫瓧娈电储寮曞け鏁?銆?/p>

绀轰緥锛?浠?tb_user 琛ㄤ负渚嬶紝鎴戜滑鍏堟潵鏌ョ湅涓€涓嬩箣鍓?tb_user 琛ㄦ墍鍒涘缓鐨勭储寮曘€?1锛変娇鐢╯how from tb_user;鍛戒护鏌ョ湅绱㈠紩锛? 鍙互鐪嬪埌 tb_user 琛ㄦ湁涓€涓仈鍚堢储寮昳dx_user_pro_age_sta锛岃繖涓仈鍚堢储寮曟秹鍙婂埌涓変釜瀛楁锛岄『搴忓垎鍒负锛歱rofession锛?age锛宻tatus銆傚浜庢渶宸﹀墠缂€娉曞垯鎸囩殑鏄紝鏌ヨ鏃讹紝鏈€宸﹀彉鐨勫垪锛屼篃灏辨槸profession蹇呴』瀛樺湪锛屽惁鍒欑储寮曞叏閮ㄥけ鏁堛€?鑰屼笖涓棿涓嶈兘璺宠繃鏌愪竴鍒楋紝鍚﹀垯璇ュ垪鍚庨潰鐨勫瓧娈电储寮曞皢澶辨晥銆?鎺ヤ笅鏉ワ紝鎴戜滑鏉ユ紨绀哄嚑缁勬渚嬶紝鐪嬩竴涓?鍏蜂綋鐨勬墽琛岃鍒?2锛変娇鐢╡xplain鍛戒护鏌ョ湅 涓嬮潰杩欐潯 select 璇彞鐨勬墽琛岃鍒掞細 鍙互鐪嬪埌鍙互鐢ㄥ埌鑱斿悎绱㈠紩锛屽疄闄呬篃鏄敤鍒拌仈鍚堢储寮曪紝鑱斿悎绱㈠紩鐨勯暱搴︿负54 3锛夌幇鍦ㄥ幓鎺?status 瀛楁锛屽啀浣跨敤explain鍛戒护鏌ョ湅 涓嬮潰杩欐潯 select 璇彞鐨勬墽琛岃鍒掞細 鍙互鐪嬪埌鍙互浣跨敤鑱斿悎绱㈠紩锛屽疄闄呯敤鍒颁簡鑱斿悎绱㈠紩锛屼絾鏄仈鍚堢储寮曠殑闀垮害涓?9锛堢敱姝ゅ彲浠ユ帹鏂嚭status瀛楁鐨勭储寮曢暱搴︿负5锛?4锛夌幇鍦ㄥ幓鎺?age銆乻tatus 瀛楁锛屽啀浣跨敤explain鍛戒护鏌ョ湅涓嬮潰杩欐潯 select 璇彞鐨勬墽琛岃鍒掞細 鍙互鐪嬪埌浣跨敤浜嗚仈鍚堢储寮曪紝鑱斿悎绱㈠紩鐨勯暱搴︿负47锛屾墍浠ュ彲浠ユ帹鏂嚭prosession瀛楁鐨勭储寮曢暱搴︿负47锛宎ge瀛楁鐨勭缉褰遍暱搴︿负2 5锛夌幇鍦ㄥ幓鎺?age 瀛楁锛屽啀浣跨敤explain鍛戒护鏌ョ湅 涓嬮潰杩欐潯 select 璇彞鐨勬墽琛岃鍒掞細 鍙互鐪嬪埌鍙互浣跨敤鑱斿悎绱㈠紩锛屽疄闄呯敤鍒颁簡鑱斿悎绱㈠紩锛屼絾鏄仈鍚堢储寮曠殑闀垮害涓?7锛屾墍浠ュ苟娌℃湁浣跨敤鍒皊tatus瀛楁鐨勭储寮曪紝杩欐槸鍥犱负涓棿璺宠繃浜哸ge瀛楁锛屾墍浠ヨ涓嶈兘璺冲瓧娈?6锛夌幇鍦ㄥ幓鎺?profession 瀛楁锛屽啀浣跨敤explain鍛戒护鏌ョ湅 涓嬮潰杩欐潯 select 璇彞鐨勬墽琛岃鍒掞細 鍙互鐪嬪埌鍘绘帀鏈€宸﹀瓧娈靛悗锛岀洿鎺ユ病鏈変娇鐢ㄧ储寮曚簡锛岃€屽彉鎴愪簡鍏ㄨ〃鎵弿 7锛夌幇鍦ㄨ皟鏁翠竴涓嬩笁缁欏瓧娈电殑椤哄簭 鍙互鐪嬪埌璋冩暣瀹屼綅缃悗锛屼笁涓储寮曢兘鐢ㄥ埌浜嗭紝鎵€浠ヨ锛岀储寮曠殑浣跨敤涓庝綅缃棤鍏筹紝鍙窡绱㈠紩鐨勫瓧娈垫槸鍚﹀瓨鍦ㄦ湁鍏?

鎬荤粨

鑱斿悎绱㈠紩閬典粠鏈€宸﹀墠缂€娉曞垯鍙窡瀛楁鏄惁瀛樺湪鏈夊叧锛屼笌缂栧啓sql鏃剁殑椤哄簭鏃犲叧褰撶储寮曢兘瀛樺湪鏃讹紝绱㈠紩闀垮害鏈€闀匡紝鏁堢巼鏈€楂樹腑闂存湁涓€涓储寮曞瓧娈典笉瀛樺湪鏃讹紝鍚庨潰鎵€鏈夌殑绱㈠紩瀛楁涔熶笉浼氳浣跨敤鏈€宸﹁竟鐨勭储寮曞瓧娈典笉瀛樺湪鏃讹紝璇ヨ仈鍚堢储寮曚笉浼氳浣跨敤锛岃€屾槸閲囩敤鍏ㄨ〃鎵弿

鑼冨洿鏌ヨ

鑱斿悎绱㈠紩涓紝鍑虹幇鑼冨洿鏌ヨ>鎴?锛岃寖鍥存煡璇㈠彸渚х殑鍒楃储寮曞け鏁堬紙鍙拡瀵硅仈鍚堢储寮曪級

鍙互鐪嬪埌绱㈠紩闀垮害涓?9锛岃鏄庢病鏈夌敤鍒?status 绱㈠紩锛岃繖鏄洜涓鸿仈鍚堢储寮曠殑椤哄簭鐨?profession銆乤ge銆乻tatus锛岀幇鍦╝ge鏄噰鐢ㄨ寖鍥存煡璇紝鎵€浠?age 鍚庨潰鐨勭储寮曞瓧娈电洿鎺ュけ鏁堜簡 娉ㄦ剰 锛氬拰涔嬪墠涓€鏍凤紝鍙窡瀛楁鏄惁瀛樺湪鏈夊叧锛屼笌缂栧啓sql鏃剁殑椤哄簭鏃犲叧 瑙勯伩鏂规硶锛氳寖鍥存煡璇㈡椂灏介噺浣跨敤>=鎴?=

2.4.4 绱㈠紩鐨勫け鏁堟儏鍐?/p>

鎯呭喌涓€锛氫笉瑕佸湪绱㈠紩鍒椾笂杩涜杩愮畻鎿嶄綔锛?绱㈠紩灏嗗け鏁?1锛変笉浣跨敤杩愮畻鎿嶄綔锛堣繍绠楁搷浣滃寘鎷姞鍑忎箻闄わ紝鍑芥暟璋冪敤锛? 鍙互鐪嬪埌浣跨敤浜嗙储寮曪紝type涓篶onst锛屾晥鐜囨瘮杈冮珮 2锛変娇鐢ㄨ繍绠楁搷浣? 鍙互鐪嬪埌key涓簄ull锛岃〃绀烘牴鏈病鏈夌敤鍒扮储寮曪紝type涓篈ll锛岃〃绀烘湰娆℃煡璇㈡槸閲囩敤鍏ㄨ〃鎵弿鐨勬柟寮忥紝鏁堢巼鏋佷綆 鎯呭喌浜岋細瀛楃涓茬被鍨嬪瓧娈典娇鐢ㄦ椂锛屼笉鍔犲紩鍙凤紝绱㈠紩灏嗗け鏁?1锛夊姞寮曞彿 鍙互鐪嬪埌浣跨敤浜嗙储寮曪紝绱㈠紩涓洪暱搴︿负54 2锛変笉鍔犲紩鍙? 鍙互鐪嬪埌绱㈠紩闀垮害涓?9锛屾樉鐒剁储寮曞瓧娈祍tatus娌℃湁琚娇鐢紝杩欐槸鍥犱负status瀛楁娌℃湁娣诲姞寮曞彿 鎯呭喌涓夛細濡傛灉浠呬粎鏄熬閮ㄦā绯婂尮閰嶏紝绱㈠紩涓嶄細澶辨晥銆傚鏋滄槸澶撮儴妯$硦鍖归厤锛岀储寮曞け鏁?1锛夐潪澶撮儴妯$硦鏌ヨ 鍙互鐪嬪埌浣跨敤浜嗙储寮?2锛夊ご閮ㄦā绯婃煡璇? 鍙互鐪嬪埌绱㈠紩澶辨晥浜嗭紝type涓篈ll锛岃〃绀鸿鏌ヨ鏄€氳繃鍏ㄨ〃鎵弿寰楀埌鐨勭粨鏋? 鎯呭喌鍥涳細鐢╫r鍒嗗壊寮€鐨勬潯浠讹紝 濡傛灉or鍓嶇殑鏉′欢涓殑鍒楁湁绱㈠紩锛岃€屽悗闈㈢殑鍒椾腑娌℃湁绱㈠紩锛岄偅涔堟秹鍙婄殑绱㈠紩閮戒笉浼?琚敤鍒?1锛塷r涓や晶鐨勫瓧娈甸兘鏈夌储寮? 鍙互鐪嬪埌锛屼娇鐢ㄤ簡id鐨勭储寮曞拰name鐨勭储寮?2锛塷r涓€渚у瓧娈垫湁绱㈠紩锛屼竴渚у瓧娈垫病鏈夌储寮? 鍙互鐪嬪埌鏌ヨ缁撴灉娌℃湁浣跨敤绱㈠紩锛岃€屾槸鐩存帴杩涜鍏ㄨ〃鎵弿锛坅ge鏄湁鑱斿悎绱㈠紩鐨勶級 3锛塷r涓€渚ф槸澶氫釜瀛楁锛屼竴渚ф湁绱㈠紩 澶氫釜瀛楁閮芥病鏈夌储寮曪紙age鍜宻tatus鏄湁鑱斿悎绱㈠紩鐨勶級锛屽彲浠ョ湅鍒颁娇鐢ㄤ簡name绱㈠紩锛屼絾鏄痶ype鍗存槸all锛屾煡璇㈢粨鏋滄槸閫氳繃鍏ㄨ〃鎵弿鐨勬柟寮忥細 澶氫釜瀛楁涓湁涓€涓湁绱㈠紩锛坅ge娌℃湁绱㈠紩锛宨d鏈変富閿储寮曪級锛宯ame鏈夌储寮曪紝鍙互鐪嬪埌浣跨敤浜嗙储寮? 鎯呭喌鍥涙€荤粨锛?/p>

or涓や晶鏄崟涓瓧娈垫椂锛屼袱渚у繀椤婚兘瑕佹湁绱㈠紩锛堣仈鍚堢储寮曚笉绠楋紝鍗曞垪绱㈠紩鎵嶇畻锛夛紝杩欐牱绱㈠紩鎵嶄笉浼氬け鏁坥r涓や晶锛屼竴渚у涓瓧娈碉紝涓€渚ф槸鍗曚釜瀛楁锛屽崟涓瓧娈佃鏈夌储寮曪紝澶氫釜瀛楁涓鑷冲皯鏈変竴涓湁绱㈠紩锛岃繖鏍风储寮曟墠涓嶄細澶辨晥 鎯呭喌浜旓細濡傛灉mysql璇勪及浣跨敤绱㈠紩姣斿叏琛ㄦ洿鎱紝鍒欎笉浣跨敤绱㈠紩锛堟暟鎹垎甯冨奖鍝嶏級 tb_user琛ㄤ腑鐨勬暟鎹ぇ閲忛兘绗﹀悎鎴戜滑鐨勬煡璇㈡潯浠讹紝鍒橫ysql鐨勬悳绱㈠紩鎿庝細鐩存帴閫夋嫨鍏ㄨ〃鎵弿鑰屼笉鏄敤绱㈠紩銆倀b_user琛ㄤ腑绗竴涓暟鎹殑phone鏄?7799990000锛屽鏋滄垜浠煡璇㈢殑鏁版嵁鏄?=1799990005鐨勶紝澶ч噺鏁版嵁閮界鍚堟垜浠殑鏌ヨ鏉′欢锛岀洿鎺ラ噰鐢ㄥ叏琛ㄦ壂鎻忥紝鍥犱负姝ゆ椂閲囩敤绱㈠紩鍙嶈€屼細鏇存參 鎴戜滑鏌ヨ鐨勬潯浠讹紝琛ㄤ腑鍙湁閮ㄥ垎鏁版嵁绗﹀悎锛屽垯璇ュ瓧娈靛瓨鍦ㄧ储寮曞氨鐩存帴閲囩敤绱㈠紩

2.4.5 sql鎻愮ず

sql鎻愮ず锛屾槸浼樺寲鏁版嵁搴撶殑涓€涓噸瑕佹墜娈碉紝绠€鍗曟潵璇达紝灏辨槸鍦⊿ql璇彞涓坊鍔犱竴浜涜涓虹殑鎻愮ず鏉ヨ揪鍒颁紭鍖栨搷浣滅殑鐩殑锛屾瘮濡傚湪鎵ц涓€涓潯浠舵煡璇㈡椂锛屾潯浠朵腑鏈夊涓储寮曞彲浠ヨ浣跨敤锛屼竴鑸儏鍐典笅閮芥槸mysql鑷姩閫夋嫨浣跨敤鍝竴涓储寮曪紝姝ゆ椂鎴戜滑鍙互浜轰负璁㎝ysql閫夋嫨浣跨敤鍝竴涓储寮曪紝杩欎釜杩囩▼灏辫绉颁负sql鎻愮ず

user index()锛氬缓璁甅ysql浣跨敤鍝竴涓储寮曞畬鎴愭娆℃煡璇紙浠呬粎鏄缓璁紝mysql鍐呴儴杩樹細鍐嶆杩涜璇勪及锛? 杩欓噷profession瀛楁鏈変袱涓储寮曪紝涓€涓仈鍚堢储寮曪紝涓€涓崟鍒楃储寮曪紝鍙互鐪嬪埌鎴戜滑寤鸿mysql浣跨敤profession瀛楁鐨勫崟鍒楃储寮曪紝浣嗘槸mysql鑷繁杩樹細璇勪及涓€涓嬫槸浣犲缓璁殑蹇繕鏄嚜宸辩殑蹇紝鏈€缁堢殑浣跨敤鍝竴涓储寮曡繕鏄緱鐪婱ysql锛屾渶缁堝彲浠ュ彂鐜板崟鍒楃储寮曡姣旇仈鍚堢储寮曞揩锛屾渶缁圡ysql閫夋嫨閬典粠鎴戜滑鐨勫缓璁? ignore index()锛氬拷鐣ユ寚瀹氱殑绱㈠紩 杩欓噷鎴戜滑蹇界暐浜唒rofession鐨勫崟鍒楃储寮曪紝鎵€浠ysql鍙兘浣跨敤瀹冪殑鑱斿悎绱㈠紩 force index()锛氬己鍒朵娇鐢ㄦ寚瀹氱殑绱㈠紩 杩欓噷鎴戜滑寮哄埗mysql浣跨敤profession鐨勮仈鍚堢储寮曪紝鍙互鐪嬪埌mysql鏈€缁堝眻鏈嶄簡(鈼徦団垁藝鈼?

2.4.6 瑕嗙洊绱㈠紩

灏介噺浣跨敤瑕嗙洊绱㈠紩锛屽噺灏憇elect *銆?閭d箞浠€涔堟槸瑕嗙洊绱㈠紩鍛紵 瑕嗙洊绱㈠紩鏄寚鏌ヨ浣跨敤浜嗙储寮曪紝骞朵笖闇€瑕佽繑鍥炵殑鍒楋紝鍦ㄨ绱㈠紩涓凡缁忓叏閮ㄨ兘澶熸壘鍒?/p>

瑕嗙洊绱㈠紩锛?/p>

鍥炶〃鏌ヨ锛堟€ц兘鏇翠綆锛夛細

1锛塻elect *

using index condition琛ㄧず鏌ユ壘浣跨敤浜嗙储寮曪紝浣嗘槸闇€瑕佸洖琛ㄦ煡璇㈡暟鎹?/p>

2锛塻elect 鏌ヨ鏈夊叧鑱氶泦绱㈠紩瀛楁锛屾湁鍏充簩绾х储寮曞瓧娈?/p>

using where; using index琛ㄧず鏌ユ壘浣跨敤浜嗙储寮曪紝浣嗘槸闇€瑕佺殑鏁版嵁閮藉湪绱㈠紩鍒椾腑鑳芥壘鍒帮紝鎵€浠ヤ笉闇€ 瑕佸洖琛ㄦ煡璇㈡暟鎹€俰d鏄仛闆嗙储寮曪紝鑰宲rofession銆乤ge鏄仈鍚堢储寮曪紝鑱斿悎绱㈠紩灞炰簬浜岀骇绱㈠紩锛屼簩绾х储寮曟槸鎸傚湪鑱氶泦绱㈠紩涓嬮潰鐨勶紝鎵€浠ラ渶瑕佹煡璇㈢殑缁撴灉鎴戜滑鍙互鐩存帴寰楀埌

3锛塻elect 鏌ヨ涓や釜鑱氶泦绱㈠紩瀛楁锛屼竴涓簩绾х储寮曞瓧娈?/p>

id鏄仛闆嗙储寮曪紝鑰宲rofession銆乤ge銆乻tatus鏄仈鍚堢储寮曪紝杩欎竴閮ㄥ垎涓嶉渶瑕佸洖琛紝浣嗘槸杩橀渶瑕佹煡鎵緉ame锛宯ame鏄垪绱㈠紩锛屽垪绱㈠紩灞炰簬鑱氶泦绱㈠紩锛屾墍浠ヨ繕闇€瑕佽繘琛屽洖琛ㄦ煡璇紙鍥炶〃鏌ヨ鍙互鍙傝€冨墠闈級

鐩稿叧闈㈣瘯棰?/p>

2.4.7 鍓嶇紑绱㈠紩

褰撳瓧娈电被鍨嬩负瀛楃涓诧紙varchar锛宼ext锛宭ongtext绛夛級鏃讹紝鏈夋椂鍊欓渶瑕佺储寮曞緢闀跨殑瀛楃涓诧紝杩欎細璁╃储寮曞彉寰楀緢澶э紝鏌ヨ鏃讹紝娴垂澶ч噺鐨勭鐩業o锛?褰卞搷鏌ヨ鏁堢巼銆傛鏃跺彲浠ュ彧灏嗚鍒楀€肩殑涓€閮ㄥ垎鍓嶇紑锛屽缓绔嬬储寮曪紝杩欐牱鍙互澶уぇ鑺傜害绱㈠紩绌洪棿锛屼粠鑰屾彁楂樼储寮曟晥鐜囥€?/p>

鍩烘湰璇硶锛?/p>

create index 绱㈠紩鍚?on 琛ㄥ悕(鍒楀悕(n));

澶囨敞锛歯琛ㄧず鐢ㄤ娇鐢ㄥ墠澶氬皯涓瓧绗︼紝瀹冨喅瀹氫簡鍓嶇紑绱㈠紩鐨勯暱搴?/p>

鍓嶇紑闀垮害濡備綍閫夊彇锛?/p>

鍙互鏍规嵁绱㈠紩鐨勯€夋嫨鎬ф潵鍐冲畾锛岃€岄€夋嫨鎬ф槸鎸囦笉閲嶅鐨勭储寮曞€硷紙鍩烘暟锛夊拰鏁版嵁琛ㄧ殑璁板綍鎬绘暟鐨勬瘮鍊硷紝 绱㈠紩閫夋嫨鎬ц秺楂樺垯鏌ヨ鏁堢巼瓒婇珮锛?鍞竴绱㈠紩鐨勯€夋嫨鎬ф槸1锛岃繖鏄渶濂界殑绱㈠紩閫夋嫨鎬э紝鎬ц兘涔熸槸鏈€濂界殑

绀轰緥锛氳绠梕mail鐨勯€夋嫨鎬?# 鏌ユ壘璇ヨ〃鐨勬€昏褰曟暟

select count(*) from tb_user; -- 鏌ヨ缁撴灉涓?4

# 鏌ユ棭email鐨勬€昏褰曟暟锛堣鍘婚噸锛?/p>

select count(distinct email) from tb_user; -- 鏌ヨ缁撴灉涓?4

鎵€浠ユ垜浠彲浠ヨ绠楀嚭email瀛楁鐨勯€夋嫨鎬т负锛?

鎬昏褰曟暟

/

e

m

a

i

l

涓嶉噸澶嶇殑璁板綍鏁?

=

24

/

24

=

1

鎬昏褰曟暟/email涓嶉噸澶嶇殑璁板綍鏁?24/24=1

鎬昏褰曟暟/email涓嶉噸澶嶇殑璁板綍鏁?24/24=1 涔熷彲浠ヨ绠梕mail瀛楁鐨勫墠5涓瓧绗︾殑閫夋嫨鎬э細 select count(distinct substring(email,1,5)) / count(*) from tb_user ; -- 缁撴灉鏄?

鎵€浠ユ鏃舵垜浠彲浠ョ洿鎺ヤ娇鐢╡mail鍓嶄簲涓瓧绗︿綔涓哄墠缂€绱㈠紩 鍓嶇紑绱㈠紩鐨勬煡璇㈡祦绋嬶細

2.4.8 鍗曞垪绱㈠紩涓庤仈鍚堢储寮?/p>

鍗曞垪绱㈠紩锛氬嵆涓€涓储寮曞彧鍖呭惈鍗曚釜鍒楄仈鍚堢储寮曪細鍗充竴涓储寮曞寘鍚簡澶氫釜鍒?/p>

鍦ㄤ笟鍔″満鏅腑锛屽鏋滃瓨鍦ㄥ涓煡璇㈡潯浠讹紝鑰冭檻閽堝浜庢煡璇㈠瓧娈靛缓绔嬬储寮曟椂锛屽缓璁缓绔嬭仈鍚堢储寮曪紝 鑰岄潪鍗曞垪绱㈠紩

绀轰緥

绀轰緥1锛歛nd鏉′欢鏌ヨ锛宎nd涓や晶閮芥槸鍗曞垪绱㈠紩

鍙互鐪嬪埌鍙蛋浜唒hone瀛楁鐨勭储寮曪紝娌℃湁璧皀ame瀛楁鐨勭储寮曪紝姝ゆ椂蹇呯劧浼氳繘琛屽洖琛ㄦ煡璇?/p>

绀轰緥2锛氬垱寤鸿仈鍚堢储寮曪紝鐒跺悗鍦ㄨ繘琛屾煡璇?/p>

create unique index idx_user_phone_name on tb_user(phone,name);

explain select id,phone,name from tb_user where phone = '17799990010' and name = '闊╀俊';

姝ゆ椂mysql杩樻槸鑷姩閫夋嫨浣跨敤phone瀛楁鐨勭储寮曪紝骞舵病鏈夌敤鍒版垜浠垱寤虹殑鑱斿悎绱㈠紩锛屼細杩涜鍥炶〃鏌ヨ

姝ゆ椂鎴戜滑浣跨敤 use index 寤鸿mysql浣跨敤鎴戜滑鍒涘缓鐨勮仈鍚堢储寮曪紝鎻愪氦鏌ヨ鏁堢巼

鑱斿悎绱㈠紩鏌ヨ娴佺▼锛?/p>

2.5 绱㈠紩鐨勮璁″師鍒?/p>

绱㈠紩鐨勮璁″師鍒欙紙灏介噺閬靛惊锛岃兘澶熻缁欑储寮曟洿鍔犻珮鏁堬級

閽堝浜庢暟鎹噺杈冨ぇ锛堟暟鎹噺瓒呰繃100w锛夛紝涓旀煡璇㈡瘮杈冮绻佺殑琛ㄥ缓绔嬬储寮曢拡瀵逛簬甯镐綔涓烘煡璇㈡潯浠讹紙where锛夈€佹帓搴忥紙order by锛夈€佸垎缁勶紙group by锛夋搷浣滅殑瀛楁寤虹珛绱㈠紩灏介噺閫夋嫨鍖哄垎搴﹂珮鐨勫垪浣滀负绱㈠紩锛屽敖閲忓缓绔嬪敮涓€绱㈠紩锛屽尯鍒嗗害瓒婇珮锛屼娇鐢ㄧ储寮曠殑鏁堢巼瓒婇珮濡傛灉鏄瓧绗︿覆绫诲瀷鐨勫瓧娈碉紝瀛楁鐨勯暱搴﹁緝闀匡紝鍙互閽堝浜庡瓧娈电殑鐗圭偣锛屽缓绔嬪墠缂€绱㈠紩灏介噺浣跨敤鑱斿悎绱㈠紩锛屽噺灏戝崟鍒楃储寮曪紝鏌ヨ鏃讹紝鑱斿悎绱㈠紩寰堝鏃跺€欏彲浠ヨ鐩栫储寮曪紝鑺傜渷瀛樺偍绌洪棿锛?閬垮厤鍥炶〃锛屾彁楂樻煡璇㈡晥鐜囪鎺у埗绱㈠紩鐨勬暟閲忥紝绱㈠紩骞朵笉鏄澶氱泭鍠勶紝绱㈠紩瓒婂锛岀淮鎶ょ储寮曠粨鏋勭殑浠d环涔熷氨瓒婂ぇ锛屼細褰卞搷澧炲垹鏀圭殑鏁堢巼濡傛灉绱㈠紩鍒椾笉鑳藉瓨鍌∟ull鍊硷紝璇峰湪鍒涘缓琛ㄦ椂浣跨敤not null绾︽潫瀹冦€傚綋浼樺寲鍣ㄧ煡閬撴瘡鍒楁槸鍚﹀寘鍚?null鍊兼椂锛屽畠鍙互鏇村ソ鍦扮‘瀹氬摢涓储寮曟渶鏈夋晥鍦扮敤浜庢煡璇€? 浼樺寲绱㈠紩鐨勫缓璁細

绱㈠紩鏌ヨ鏁堢巼锛氫富閿储寮?鍞竴绱㈠紩>鍗曞垪绱㈠紩>澶氬垪绱㈠紩锛堣仛鍚堢储寮曪級銆傝繘琛屾潯浠舵煡璇㈡椂锛屽敖閲忎娇鐢ㄨ仈鍚堢储寮曪紝铏界劧鍗曞垪绱㈠紩鐨勬晥鐜囬珮锛屼絾鏄細鍦ㄨ繘琛屽鏉′欢鏌ヨ鏃讹紝瀹规槗鍙戠敓鍥炶〃鏌ヨ锛岄檷浣庢晥鐜囥€傛墍浠ュ皯鐢?select * 鑰屽簲璇ラ€夋嫨鎵嬪姩鏄剧ず鍦版寚鍑烘煡璇㈠瓧娈典娇鐢ㄨ仈鍚堢储寮曟椂锛岄伒浠庢渶宸﹀墠缂€娉曞垯浣跨敤鑱斿悎绱㈠紩鏃讹紝濡傛灉绱㈠紩瀛楁澶暱锛屽簲璇ヤ娇鐢ㄥ墠缂€绱㈠紩锛屽悓鏃惰纭繚鍓嶇紑閫夋嫨鎬ц楂樿兘瑕嗙洊绱㈠紩灏辫鐩栫储寮曪紝閬垮厤鍥炶〃闄嶄綆鏌ヨ鏁堢巼鏉′欢鏌ヨ鏃讹紝瀛樺湪澶氫釜绱㈠紩锛屽簲璇ヤ娇鐢⊿ql鎻愮ず锛屼繚闅滀娇鐢ㄩ珮鏁堢巼鐨勭储寮曡鐔熸倝绱㈠紩鐨勫け鏁堟儏鍐碉紝閬垮厤绱㈠紩澶辨晥鑰屽鑷村叏琛ㄦ煡璇?鈥︹€﹁繕鏈夊摢浜涘缓璁憿锛熸杩庤ˉ鍏呰鏄?鈼忊€欌棥鈥欌棌)

3銆丼ql浼樺寲

3.1 sql鎬ц兘鍒嗘瀽

鏈皬鑺備富瑕佸涔狅細鏌ョ湅sql鐨勬墽琛岄鐜囥€佹煡鐪嬫參鏌ヨ鏃ュ織銆佹煡鐪婼ql鐨勬墽琛屾椂闂达紙profile锛夈€佹煡鐪婼ql鐨勬墽琛岃鍒掞紙explain锛?/p>

鏌ョ湅sql鐨勬墽琛岄鐜?/p>

閫氳繃涓嬮潰鐨勬寚浠わ紝鎴戜滑鍙互鏌ョ湅鍒板綋鍓嶆暟鎹簱鍒板簳鏄互鏌ヨ涓轰富锛岃繕鏄互澧炲垹鏀逛负涓伙紝浠庤€屼负鏁版嵁 搴撲紭鍖栨彁渚涘弬鑰冧緷鎹€?濡傛灉鏄互澧炲垹鏀逛负涓伙紝鎴戜滑鍙互鑰冭檻涓嶅鍏惰繘琛岀储寮曠殑浼樺寲銆?濡傛灉鏄互 鏌ヨ涓轰富锛岄偅涔堝氨瑕佽€冭檻瀵规暟鎹簱鐨勭储寮曡繘琛屼紭鍖栦簡銆?/p>

show [session|global] status

status鍙互浣跨敤浠ヤ笅鍑犱釜鍊硷細

com_______锛氭煡璇㈠悇绉嶆搷浣滅殑浣跨敤娆℃暟com_insert锛氭煡璇㈡彃鍏ユ搷浣滅殑娆℃暟com_delete锛氭煡璇㈠垹闄ゆ搷浣滅殑娆℃暟com_update锛氭煡璇㈡洿鏂版搷浣滅殑娆℃暟com_select锛氭煡璇㈡煡璇㈡搷浣滅殑娆℃暟 show global status like 'com_______'; -- 澶囨敞: com鍚庨潰鏍?涓笅鍒掔嚎

鏌ョ湅鎱㈡煡璇㈡棩蹇?/p>

鎱㈡煡璇㈡棩蹇楄褰曚簡鎵€鏈夋墽琛屾椂闂磋秴杩囨寚瀹氬弬鏁帮紙long_query_time锛屽崟浣嶏細绉掞紝榛樿10绉掞級鐨勬墍鏈?sql璇彞鐨勬棩蹇椼€?/p>

鏌ョ湅鎱㈡煡璇㈡棩蹇楁槸鍚﹀紑鍚紙mysql榛樿鎱㈡煡璇㈡棩蹇楁槸off锛屾病鏈夊紑鍚殑锛孫n琛ㄧず寮€鍚級 show variables like 'slow_query_log';

寮€鍚參鏌ヨ鏃ュ織

step1锛氳繘鍏ysql鐨勯厤缃枃浠?vi /etc/my.cnf

step2锛氱紪鍐欐參鏌ヨ閰嶇疆 # 寮€鍚疢ysql鎱㈡棩蹇楁煡璇㈠紑鍏?/p>

slow_query_log=1

# 璁剧疆鎱㈡棩蹇楃殑鏃堕棿涓?绉掞紝sql璇彞鎵ц鏃堕棿瓒呰繃2绉掞紝灏变細瑙嗕负鎱㈡煡璇紝璁板綍鎱㈡煡璇㈡棩蹇?/p>

long_query_time=2

step3锛氶噸鍚疢ysql systemctl restart mysqld

# 閲嶅惎涔嬪悗鍙互浣跨敤涓嬮潰鐨勬寚浠ゆ煡鐪嬫參鏌ヨ鏄惁宸茬粡寮€鍚?/p>

show variables like 'slow_query_log';

澶囨敞锛歁ysql鐨勬參鏌ヨ鏃ュ織榛樿鍦ㄨ璺緞涓?var/lib/mysql/锛岃繘鍏ヤ笅闈㈢殑鐩綍锛岀劧鍚庝娇鐢╨l | grep slow.log鏌ョ湅鍒颁竴涓?*-slow.log 鐨勬枃浠?*鏄綘鐨勪富鏈哄悕)锛岀劧鍚庝娇鐢╟at鍛戒护鍙互鏌ョ湅鍒板叾涓殑鐩稿簲淇℃伅 tail -f node1-slow.log鍛戒护鍙互鍗虫椂鏌ョ湅鎱㈡煡璇㈡棩蹇楃殑鍐呭鍙樺寲 profile锛氭煡鐪婼ql鐨勬墽琛屾椂闂?/p>

show profiles 鑳藉鍦ㄥ仛sql浼樺寲鏃跺府鍔╂垜浠簡瑙f椂闂撮兘鑰楄垂鍒板摢閲屽幓浜?/p>

# 鏌ョ湅褰撳墠鏁版嵁搴撴槸鍚︽敮鎸乸rofile

select @@have_profiling ;

# 鏌ョ湅褰撳墠鏁版嵁搴撴槸鍚﹀紑鍚痯rofile锛堥粯璁ゆ槸0琛ㄧず鍏抽棴鐨勶紝1琛ㄧず寮€鍚級

select @@profiling;

# 寮€鍚痯rofiling

set profiling = 1;

# 鏌ョ湅姣忎竴鏉ql鐨勮€楁椂鍩烘湰鎯呭喌

show profiles;

# 鏌ョ湅鎸囧畾query_id鐨凷ql璇彞鍚勪釜闃舵鐨勮€楁椂鎯呭喌

show profile for query query_id;

# 鏌ョ湅鎸囧畾query_id鐨凷ql璇彞cpu鐨勪娇鐢ㄦ儏鍐?/p>

show profile cpu for query query_id;

explain锛氭煡鐪婼ql鐨勬墽琛岃鍒?/p>

explain 鎴栬€?desc 鍛戒护鑾峰彇 mysql 濡備綍鎵ц select 璇彞鐨勪俊鎭紝鍖呮嫭鍦?select 璇彞鎵ц 杩囩▼涓〃濡備綍杩炴帴鍜岃繛鎺ョ殑椤哄簭

explain select 瀛楁鍒楄〃 from 琛ㄥ悕 where 鏉′欢 ;

澶囨敞锛?/p>

type鏄紭鍖栫殑涓€涓噸瑕佹寚鏍囷紝涓嶆煡璇换浣曡〃锛坰elect 鈥楢鈥欙紝鏌ヨ涓€涓瓧绗锛岀洿鎺ヨ繑鍥濧锛夛紝瀹冪殑鎬ц兘鏄渶濂界殑涓篘ull锛屾煡璇㈢郴缁熻〃锛宼ype涓€鑸负system锛屾牴鎹富閿垨鑰呭敮涓€绱㈠紩鏌ヨtype涓€鑸槸const锛屾牴鎹潪鍞竴鎬х储寮曟煡璇竴鑸瑃ype涓篹q_ref鎴杛ef锛宨ndex琛ㄧず鐢ㄨ繃绱㈠紩浣嗘槸鏄储寮曟壂鎻忥紝all涓€鑸槸瀵瑰叏琛ㄦ壂鎻忥紝鎬ц兘鏈€宸甧xtra琛ㄧず棰濆淇℃伅锛屾煡璇㈠埌鐨勫€兼渶缁堟病鏈夊睍绀哄氨浼氬湪杩欒繘琛屽睍绀?

3.2 insert浼樺寲

3.2.1 鏅€氭暟鎹彃鍏?/p>

濡傛灉鎴戜滑闇€瑕佷竴娆℃€у線鏁版嵁搴撹〃涓彃鍏ュ鏉¤褰曪紝鍙互浠庝互涓嬩笁涓柟闈㈣繘琛屼紭鍖栥€?/p>

浼樺寲鍓嶏細 insert into tb_test values(2,'tom');

insert into tb_test values(1,'cat');

insert into tb_test values(3,'jerry');

浼樺寲涓€锛氭壒閲忔彃鍏ユ暟鎹?insert into tb_test values(2,'tom'),(1,'cat'),(3,'jerry');

鎵归噺鎻掑叆姣斾竴娆′竴娆$殑鎻掑叆鎿嶄綔鏁堢巼瑕侀珮涓昏鏈変互涓嬪嚑涓師鍥狅細

鍑忓皯浜嗕氦浜掓鏁帮細涓€娆′竴娆$殑鎻掑叆鎿嶄綔浼氫骇鐢熷ぇ閲忕殑鏁版嵁搴撴搷浣滐紝姣忔鎿嶄綔閮介渶瑕佷笌鏁版嵁搴撹繘琛屼竴娆′氦浜掞紝鍖呮嫭寤虹珛杩炴帴銆佸彂閫佽姹傘€佹帴鏀跺搷搴旂瓑绛夈€傝€屾壒閲忔彃鍏ュ彲浠ュ皢澶氫釜鏁版嵁琛屼竴娆℃€ф彁浜ゅ埌鏁版嵁搴撲腑锛屼粠鑰屽噺灏戜簡浜や簰娆℃暟锛屾彁楂樹簡鏁堢巼銆傛彁楂樹簡鏁版嵁鍘嬬缉鐜囷細鎵归噺鎻掑叆鍙互灏嗗涓暟鎹涓€娆℃€ф彁浜ゅ埌鏁版嵁搴撲腑锛岃繖鏍峰彲浠ユ彁楂樻暟鎹帇缂╃巼锛屼粠鑰屽噺灏戜簡鏁版嵁鍦ㄧ綉缁滀紶杈撹繃绋嬩腑鐨勫ぇ灏忥紝闄嶄綆浜嗙綉缁滃甫瀹界殑娑堣€椼€傚噺灏戜簡绱㈠紩鏇存柊娆℃暟锛氬湪澶ч噺鏁版嵁鎻掑叆鏃讹紝濡傛灉姣忔鎻掑叆涓€琛屾暟鎹紝閭d箞鏁版嵁搴撻渶瑕佸绱㈠紩杩涜澶氭鏇存柊锛岃繖浼氬鑷存€ц兘涓嬮檷銆傝€屾壒閲忔彃鍏ュ彲浠ュ皢澶氫釜鏁版嵁琛屼竴娆℃€ф彁浜ゅ埌鏁版嵁搴撲腑锛屼粠鑰屽噺灏戜簡绱㈠紩鏇存柊娆℃暟锛屾彁楂樹簡鏁堢巼銆? 浼樺寲浜岋細鎵嬪姩鎺у埗浜嬪姟 start transaction;

insert into tb_test values(2,'tom'),(1,'cat'),(3,'jerry');

commit;

mysql榛樿鏄嚜鍔ㄦ帶鍒朵簨鍔$殑锛屼竴鏉ql鎵ц鍚庡氨浼氳嚜鍔ㄦ彁浜や簨鍔★紝棰戠箒鍦版彁浜や簨鍔′細澶ч噺娑堣€楃郴缁熻祫婧愶紝浠庤€岄檷浣庣郴缁熸€ц兘 娉ㄦ剰锛氭墜鍔ㄦ帶鍒朵簨鍔′笌鑷姩鎺у埗浜嬪姟鐨勬晥鐜囬珮浣庡苟涓嶆槸鍥哄畾鐨勶紝鑰屾槸鍙栧喅浜庡叿浣撶殑搴旂敤鍦烘櫙鍜屽疄鐜版柟寮忋€?/p>

鍦ㄤ竴浜涚壒瀹氱殑鍦烘櫙涓嬶紝鎵嬪姩鎺у埗浜嬪姟鍙兘浼氭瘮鑷姩鎺у埗浜嬪姟鏁堢巼鏇撮珮銆傝繖閫氬父鏄洜涓烘墜鍔ㄦ帶鍒朵簨鍔″彲浠ユ洿绮剧‘鍦版帶鍒朵簨鍔$殑鑼冨洿鍜岀敓鍛藉懆鏈燂紝浠庤€岄伩鍏嶄簡涓嶅繀瑕佺殑閿佸拰绛夊緟銆傛墜鍔ㄦ帶鍒朵簨鍔″彲浠ュ皢澶氫釜鎿嶄綔鍚堝苟涓轰竴涓簨鍔★紝閬垮厤浜嗗涓簨鍔′箣闂寸殑寮€閿€鍜岄殧绂荤骇鍒殑鍒囨崲銆備緥濡傦紝鍦ㄦ壒閲忔暟鎹鐞嗙殑鍦烘櫙涓紝鎵嬪姩鎺у埗浜嬪姟鍙互灏嗗涓暟鎹搷浣滃寘瑁呭湪涓€涓簨鍔′腑锛屼粠鑰屽噺灏戜簡浜嬪姟鐨勫紑閿€鍜岄殧绂荤骇鍒殑鍒囨崲銆傚湪楂樺苟鍙戝満鏅笅锛屾墜鍔ㄦ帶鍒朵簨鍔″彲浠ユ洿鍔犵簿缁嗗湴鎺у埗閿佺殑鑼冨洿鍜屾椂闂达紝閬垮厤浜嗕笉蹇呰鐨勭瓑寰呭拰闃诲锛屾彁楂樹簡骞跺彂鑳藉姏鍜屽搷搴旈€熷害銆傜劧鑰岋紝鍦ㄤ竴浜涘満鏅笅锛岃嚜鍔ㄦ帶鍒朵簨鍔″彲鑳戒細鏇村姞楂樻晥銆傝嚜鍔ㄦ帶鍒朵簨鍔″彲浠ラ€氳繃鏁版嵁搴撳紩鎿庤嚜鍔ㄧ鐞嗕簨鍔$殑鑼冨洿鍜岀敓鍛藉懆鏈燂紝浠庤€岄伩鍏嶄簡鎵嬪姩鎺у埗浜嬪姟涓彲鑳藉嚭鐜扮殑閿欒鍜岄闄┿€傝嚜鍔ㄦ帶鍒朵簨鍔″彲浠ユ洿鍔犵伒娲诲湴澶勭悊寮傚父鍜屽洖婊氾紝淇濊瘉浜嬪姟鐨勪竴鑷存€у拰鍙潬鎬с€?/p>

浼樺寲涓夛細涓婚敭椤哄簭鎻掑叆锛屾€ц兘瑕侀珮浜庝贡搴忔彃鍏?start transaction;

insert into tb_test values(1,'tom'),(2,'cat'),(3,'jerry');

commit;

涓婚敭鎸夐『搴忔彃鍏ユ€ц兘姣斾贡搴忔彃鍏ラ珮鐨勫師鍥犱富瑕佹湁涓や釜锛?/p>

鍑忓皯浜嗙储寮曞垎瑁傦細涓婚敭鎸夐『搴忔彃鍏ュ彲浠ヤ繚璇佹柊鎻掑叆鐨勬暟鎹€绘槸鍦ㄧ储寮曠殑鏈熬锛岃繖鏍峰彲浠ラ伩鍏嶇储寮曞垎瑁傘€傚綋绱㈠紩鍙戠敓鍒嗚鏃讹紝鏁版嵁搴撻渶瑕侀噸鏂扮粍缁囩储寮曠粨鏋勶紝杩欎細瀵艰嚧鎬ц兘涓嬮檷銆傝€屾寜椤哄簭鎻掑叆鍙互鍑忓皯绱㈠紩鍒嗚鐨勫彂鐢燂紝浠庤€屾彁楂樻€ц兘銆? 鍑忓皯浜嗛殢鏈虹鐩樿闂細涓婚敭鎸夐『搴忔彃鍏ュ彲浠ヨ鏂版彃鍏ョ殑鏁版嵁涓庡凡鏈夌殑鏁版嵁鍦ㄧ鐩樹笂鐨勭墿鐞嗕綅缃浉閭伙紝杩欐牱鍙互鍑忓皯纾佺洏瀵诲潃鐨勬鏁帮紝浠庤€屾彁楂樻€ц兘銆傜浉鍙嶏紝濡傛灉鎻掑叆鏁版嵁鐨勪富閿槸闅忔満鐢熸垚鐨勶紝閭d箞鏂版彃鍏ョ殑鏁版嵁鍙兘浼氬垎鏁e湪纾佺洏涓婄殑涓嶅悓浣嶇疆锛岃繖鏍峰氨闇€瑕佽繘琛屽娆¢殢鏈虹鐩樿闂紝浠庤€岄檷浣庝簡鎬ц兘銆? 娉ㄦ剰锛氬鏋滀富閿槸鑷闀跨殑锛屾寜椤哄簭鎻掑叆鍜屾寜涔卞簭鎻掑叆鐨勬€ц兘宸窛鍙兘涓嶅お鏄庢樉銆傚洜涓鸿嚜澧為暱涓婚敭涔熷彲浠ヤ繚璇佹柊鎻掑叆鐨勬暟鎹€绘槸鍦ㄧ储寮曠殑鏈熬锛屼粠鑰岄伩鍏嶇储寮曞垎瑁傦紝鑰屼笖鑷闀夸富閿殑鍊间篃鍙互鎸夐『搴忕敓鎴愶紝浠庤€屽噺灏戦殢鏈虹鐩樿闂€備絾鏄紝鍦ㄤ竴浜涚壒瀹氱殑鍦烘櫙涓嬶紝鎸夐『搴忔彃鍏ヤ粛鐒跺彲鑳芥瘮鎸変贡搴忔彃鍏ユ洿楂樻晥锛屼緥濡傚湪浣跨敤innodb寮曟搸鏃讹紝鍥犱负innodb浣跨敤鑱氱皣绱㈠紩锛屾寜椤哄簭鎻掑叆鍙互閬垮厤椤靛垎瑁傦紝鎻愰珮鎬ц兘銆?

3.2.2 澶ф暟鎹彃鍏?/p>

濡傛灉涓€娆℃€ч渶瑕佹彃鍏ュぇ鎵归噺鏁版嵁(姣斿: 鍑犵櫨涓囩殑璁板綍)锛屼娇鐢╥nsert璇彞鎻掑叆鎬ц兘杈冧綆锛屾鏃跺彲浠ヤ娇 鐢∕ysql鏁版嵁搴撴彁渚涚殑load鎸囦护杩涜鎻掑叆锛堜箣鍓嶉偅寮?1000w鏁版嵁閲忕殑 tb_sku琛ㄥ氨鏄噰鐢ㄨ繖绉嶆柟寮忔彃鍏ョ殑锛夈€傛搷浣滃涓嬶細

step1锛氫娇鐢╨ocal-infile鍙傛暟鐧诲綍mysql mysql -u 鐢ㄦ埛鍚?-p --local-infile

澶囨敞锛歭ocal-infile琛ㄧず璧嬩簣褰撳墠鐧诲綍鐢ㄦ埛瀵煎叆鏁版嵁鐨勬潈闄? step2锛氬紑鍚暟鎹鍏ユ潈闄?set global local_infile=1;

澶囨敞锛歁ysql榛樿鏄叧闂暟鎹鍏ュ姛鑳界殑 step3锛氬缓琛?鍋囪寤虹珛 tb_sku琛紝鐢ㄤ簬鏁版嵁瀵煎叆 step4锛氬鍏ユ暟鎹?load data local infile '/root/sql/tb_sku1.sql' into table `tb_sku` fields terminated by ',' lines terminated by '\n';

3.3 涓婚敭浼樺寲

鍦ㄤ笂涓€灏忚妭锛屾垜浠彁鍒帮紝涓婚敭椤哄簭鎻掑叆鐨勬€ц兘鏄楂樹簬涔卞簭鎻掑叆鐨勩€?杩欎竴灏忚妭锛屽氨鏉ヤ粙缁嶄竴涓嬪叿浣撶殑 鍘熷洜锛岀劧鍚庡啀鍒嗘瀽涓€涓嬩富閿張璇ュ浣曡璁°€?/p>

3.3.1 鏁版嵁鐨勭粍缁囨柟寮?/p>

鏁版嵁鐨勭粍缁囨柟寮忥細鍦↖nnodb瀛樺偍寮曟搸涓紝琛ㄦ暟鎹兘鏄牴鎹富閿『搴忕粍缁囧瓨鏀剧殑锛岃繖绉嶅瓨鍌ㄦ柟寮忕殑琛ㄧо涓虹储寮曠粍缁囪〃 (index organized table锛孖ot) 琛屾暟鎹紝閮芥槸瀛樺偍鍦ㄨ仛闆嗙储寮曠殑鍙跺瓙鑺傜偣涓婄殑銆傝€屾垜浠箣鍓嶄篃璁茶в杩嘔nnodb鐨勯€昏緫缁撴瀯鍥撅細 鍦↖nnodb寮曟搸涓紝鏁版嵁琛屾槸璁板綍鍦ㄩ€昏緫缁撴瀯 page 椤典腑鐨勶紝鑰屾瘡涓€涓〉鐨勫ぇ灏忔槸鍥哄畾鐨勶紝榛樿16k銆傞偅涔熷氨鎰忓懗鐫€锛?涓€涓〉涓墍瀛樺偍鐨勮涔熸槸鏈夐檺鐨勶紝濡傛灉鎻掑叆鐨勬暟鎹row鍦ㄨ椤靛瓨鍌ㄤ笉灏忥紝灏嗕細瀛樺偍鍒颁笅涓€涓〉涓紝椤典笌椤典箣闂翠細閫氳繃鎸囬拡杩炴帴銆?

3.3.2 椤靛垎瑁?/p>

椤靛垎瑁傦細椤靛彲浠ヤ负绌猴紝涔熷彲浠ュ~鍏呬竴鍗婏紝涔熷彲浠ュ~鍏?00%銆傛瘡涓〉鍖呭惈浜?-n琛屾暟鎹?濡傛灉涓€琛屾暟鎹繃澶э紝浼氳婧㈠嚭)锛屾牴鎹富閿帓鍒?/p>

涓婚敭椤哄簭鎻掑叆鏁堟灉锛?鈶犱粠纾佺洏涓敵璇烽〉锛?涓婚敭椤哄簭鎻掑叆 鈶?绗竴涓〉娌℃湁婊★紝缁х画寰€绗竴椤垫彃鍏? 鈶㈠綋绗竴涓篃鍐欐弧涔嬪悗锛屽啀鍐欏叆绗簩涓〉锛岄〉涓庨〉涔嬮棿浼氶€氳繃鎸囬拡杩炴帴 鈶e綋绗簩椤靛啓婊′簡锛屽啀寰€绗笁椤靛啓鍏? 涓婚敭涔卞簭鎻掑叆鏁堟灉锛?鈶犲亣濡?#,2#椤甸兘宸茬粡鍐欐弧浜嗭紝瀛樻斁浜嗗鍥炬墍绀虹殑鏁版嵁 鈶℃鏃跺啀鎻掑叆id涓?0鐨勮褰曪紝鎴戜滑鏉ョ湅鐪嬩細鍙戠敓浠€涔堢幇璞?浼氬啀娆″紑鍚竴涓〉锛屽啓鍏ユ柊鐨勯〉涓悧锛? 涓嶄細銆傚洜涓猴紝绱㈠紩缁撴瀯鐨勫彾瀛愯妭鐐规槸鏈夐『搴忕殑銆傛寜鐓ч『搴忥紝搴旇瀛樺偍鍦?7涔嬪悗 鈶絾鏄?7鎵€鍦ㄧ殑1#椤碉紝宸茬粡鍐欐弧浜嗭紝瀛樺偍涓嶄簡50瀵瑰簲鐨勬暟鎹簡銆?閭d箞姝ゆ椂浼氬紑杈熶竴涓柊鐨勯〉 3# 鈶d絾鏄苟涓嶄細鐩存帴灏?0瀛樺叆3#椤碉紝鑰屾槸浼氬皢1#椤靛悗涓€鍗婄殑鏁版嵁锛岀щ鍔ㄥ埌3#椤碉紝鐒跺悗鍦?#椤碉紝鎻掑叆50 鈶ょщ鍔ㄦ暟鎹紝骞舵彃鍏d涓?0鐨勬暟鎹箣鍚庯紝閭d箞姝ゆ椂锛岃繖涓変釜椤典箣闂寸殑鏁版嵁椤哄簭鏄湁闂鐨勩€?1#鐨勪笅涓€涓〉锛屽簲璇ユ槸3#锛?3#鐨勪笅涓€涓〉鏄?#銆?鎵€浠ワ紝姝ゆ椂锛岄渶瑕侀噸鏂拌缃摼琛ㄦ寚閽? 涓婅堪鐨勮繖绉嶇幇璞★紝绉颁箣涓?鈥滈〉鍒嗚鈥濓紝鏄瘮杈冭€楄垂鎬ц兘鐨勬搷浣?

3.3.3 椤靛悎骞?/p>

椤靛悎骞讹細鍒犻櫎鏁版嵁鏃讹紝鍒犻櫎鍒颁竴瀹氱▼搴︼紝mysql灏变細鏌ヨ璇ラ〉宸﹀彸鐨勯〉鏄惁鍏锋湁鍚堝苟鍒板彲鑳?鐩墠琛ㄤ腑宸叉湁鏁版嵁鐨勭储寮曠粨鏋?鍙跺瓙鑺傜偣)濡備笅锛? 褰撴垜浠宸叉湁鏁版嵁杩涜鍒犻櫎鏃讹紝鍏蜂綋鐨勬晥鏋滃涓嬶細 鈶犲綋鍒犻櫎涓€琛岃褰曟椂锛屽疄闄呬笂璁板綍骞舵病鏈夎鐗╃悊鍒犻櫎锛屽彧鏄褰曡鏍囪锛坒laged锛変负鍒犻櫎骞朵笖瀹冪殑绌洪棿鍙樺緱鍏佽琚叾浠栬褰曞0鏄庝娇鐢? 鈶″綋鎴戜滑缁х画鍒犻櫎2#鐨勬暟鎹褰? 鈶㈠綋椤典腑鍒犻櫎鐨勮褰曡揪鍒?merge_threshold1锛堥粯璁や负椤电殑50%锛夛紝innodb浼氬紑濮嬪鎵炬渶闈犺繎鐨勯〉锛堝墠鎴栧悗锛夌湅鐪嬫槸鍚﹀彲浠ュ皢涓や釜椤靛悎骞朵互浼樺寲绌洪棿浣跨敤 鈶e垹闄ゆ暟鎹紝骞跺皢椤靛悎骞朵箣鍚庯紝鍐嶆鎻掑叆鏂扮殑鏁版嵁21锛屽垯鐩存帴鎻掑叆3#椤? 杩欎釜閲岄潰鎵€鍙戠敓鐨勫悎骞堕〉鐨勮繖涓幇璞★紝灏辩о涔嬩负 鈥滈〉鍚堝苟鈥?

3.3.4 涓婚敭鐨勮璁″師鍒?/p>

涓婚敭鐨勮璁″師鍒?/p>

婊¤冻涓氬姟闇€姹傜殑鎯呭喌涓嬶紝灏介噺闄嶄綆涓婚敭鐨勯暱搴︺€傚洜涓轰簩绾х储寮曞瓨鍌ㄧ殑涓婚敭绱㈠紩锛屼富閿储寮曡繃甯革紝浼氭氮璐瑰ぇ閲忕殑鍐呭瓨锛屽悓鏃跺澶х鐩樼殑io锛屼粠鑰岄檷浣庢煡璇㈢殑鎬ц兘鎻掑叆鏁版嵁鏃讹紝灏介噺閫夋嫨椤哄簭鎻掑叆锛岄€夋嫨浣跨敤auto_increment鑷涓婚敭銆備娇鐢ㄤ富閿嚜澧炶兘澶熼伩鍏嶉〉鍒嗚鐜拌薄锛岄〉鍒嗚浼氭秷鑰楀ぇ閲忕殑鏃堕棿灏介噺涓嶈浣跨敤uuid鍋氫富閿垨鑰呮槸鍏朵粬鑷劧涓婚敭锛屽韬唤璇佸彿銆傚洜涓篣uid鎴栬€呮槸韬唤璇佷細姣旇緝闀匡紝骞朵笖鏄棤鏃犲簭鐨勶紝瀹规槗鍙戠敓椤靛垎瑁備笟鍔℃搷浣滄椂锛岄伩鍏嶅涓婚敭鐨勪慨鏀广€備慨鏀逛富閿細鏀瑰姩琛ㄧ殑绱㈠紩缁撴瀯锛屾墍浠ヤ富閿竴鑸笉瑕佸姩锛堜富閿竴鑸槸閫夋嫨娌℃湁浠讳綍閫昏緫鍚箟鐨勬暟鎹級

3.4 order by浼樺寲

mysql鐨勬帓搴忥紝鏈変袱绉嶆柟寮忥細

using filesort锛氶€氳繃琛ㄧ殑绱㈠紩鎴栧叏琛ㄦ壂鎻忥紝璇诲彇婊¤冻鏉′欢鐨勬暟鎹锛岀劧鍚庡湪鎺掑簭缂撳啿鍖簊ort buffer涓畬鎴愭帓搴忔搷浣滐紝鎵€鏈変笉鏄€氳繃绱㈠紩鐩存帴杩斿洖鎺掑簭缁撴灉鐨勬帓搴忛兘鍙?filesort 鎺掑簭using index锛氶€氳繃鏈夊簭绱㈠紩椤哄簭鎵弿鐩存帴杩斿洖鏈夊簭鏁版嵁锛岃繖绉嶆儏鍐靛嵆涓?using index锛屼笉闇€瑕侀澶栨帓搴忥紝鎿嶄綔鏁堢巼楂?瀵逛簬浠ヤ笂鐨勪袱绉嶆帓搴忔柟寮忥紝using index鐨勬€ц兘楂橈紝鑰孶sing filesort鐨勬€ц兘浣庯紝鎴戜滑鍦ㄤ紭鍖栨帓搴忔搷浣滄椂锛屽敖閲忚浼樺寲涓?using index

绀轰緥锛?/p>

鈶犳暟鎹噯澶囷細

鈶$洿鎺ヤ娇鐢╫rder by鏍规嵁骞撮緞杩涜鎺掑簭锛圡ysql涓紝order by榛樿鏄噰鐢ㄥ崌搴忔帓搴廇sc锛?/p>

鍙互鐪嬪埌姝ゆ椂娌℃湁浣跨敤绱㈠紩锛岃€屾槸閲囩敤鍏ㄨ〃鎵弿鐨勬柟寮忓緱鍒扮粨鏋滐紝鎺掑簭鏂瑰紡涓?using filesort

澶囨敞锛氬嵆浣挎鏃禷ge鍜宲hone鏈夊悇鑷殑鍗曞垪绱㈠紩锛屾鏃舵煡璇粛鐒舵槸鍏ㄨ〃鎵弿锛屼絾寮哄埗浣跨敤鍗曞垪绱㈠紩鏄垚鍔熺殑why?

鈶负age鍜宲hone鍒涘缓涓€涓仈鍚堢储寮曪紝鐒跺悗鍐嶈繘琛屾煡璇紝涔嬪悗杩涜鍗囧簭鎺掑簭锛圡ysql榛樿鏄崌搴忕殑锛屾墍浠sc鍙互鐪佺暐锛?/p>

鍙互鐪嬪埌杩欐鎺掑簭浣跨敤鍒颁簡绱㈠紩锛屽苟涓旀帓搴忔柟寮忓彉鎴愪簡 using index

鈶ゆ寜鐓ge杩涜闄嶅簭鎺掑簭

涔熷嚭鐜?using index锛?浣嗘槸姝ゆ椂extra涓嚭鐜颁簡 backward index scan锛岃繖涓唬琛ㄥ弽鍚戞壂鎻忕储寮曪紝鍥犱负鍦∕ysql涓垜浠垱寤虹殑绱㈠紩锛岄粯璁ょ储寮曠殑鍙跺瓙鑺傜偣鏄粠灏忓埌澶ф帓搴忕殑锛岃€屾鏃舵垜浠煡璇㈡帓搴忔椂锛屾槸浠庡ぇ鍒板皬锛屾墍浠ワ紝鍦ㄦ壂鎻忔椂锛屽氨鏄弽鍚戞壂鎻忥紝灏变細鍑虹幇 backward index scan锛堝畠鐨勬晥鐜囧拰using index鐩稿樊鏃犲嚑锛夈€?鍦?mysql8 鐗堟湰涓紝鏀寔闄嶅簭绱㈠紩锛屾垜浠篃鍙互鍒涘缓闄嶅簭绱㈠紩

鈶rder by鎺掑簭涓嶆寜鐓ф渶宸﹀墠缂€娉曞垯

鍙互鐪嬪埌鍑虹幇浜?using filesort锛屾樉鐒舵晥鐜囪姣擴sing index瑕佷綆锛岃繖鏄洜涓簅rder by涔熼渶瑕侀伒寰渶宸﹀墠缂€娉曞垯锛屽惁鍒欏氨鏄噰鐢║sing filesort鎺掑簭

鈶︽牴鎹產ge, phone杩涜闄嶅簭涓€涓崌搴忥紝涓€涓檷搴?/p>

鍥犱负鍒涘缓绱㈠紩鏃讹紝濡傛灉鏈寚瀹氶『搴忥紝榛樿閮芥槸鎸夌収鍗囧簭鎺掑簭鐨勶紝鑰屾煡璇㈡椂锛屼竴涓崌搴忥紝涓€涓檷搴忥紝姝ゆ椂灏变細鍑虹幇using filesort

鈶т负浜嗚в鍐宠繖涓棶棰橈紝鎴戜滑闇€瑕佸啀鍒涘缓绱㈠紩鏃舵寚瀹氶『搴?/p>

鐜板湪鎴戜滑鍐嶆潵鏌ョ湅绱㈠紩鐨刢ollection瀛楁

鎬荤粨锛?/p>

mysql涓璒der by 榛樿閲囩敤鍗囧簭鎺掑簭asc锛屽鏋滄煡璇㈢殑鏄涓瓧娈碉紝闇€瑕佷娇鐢ㄨ仈鍚堢储寮曟晥鐜囧姣旓細using index>backward index scan>using filesort锛堝ぇ鑷达紝using index鍜孊ackward index scan鏁堢巼宸笉澶氾級order by涔熸槸闇€瑕侀伒寰渶宸﹀墠缂€娉曞垯浣跨敤鑱斿悎绱㈠紩鏃讹紝oder by鎸変竴涓瓧娈垫帓搴忥紝鍗囧簭銆侀檷搴忔棤鎵€璋擄紱濡傛灉鏄寜澶氫釜瀛楁杩涜鎺掑簭锛屽崌搴忔帓搴忔棤鎵€璋擄紝鍥犱负mysql榛樿鏄噰鐢ㄥ崌搴忔帓搴忥紝浣嗘槸濡傛灉鏄檷搴忥紝鍒欓渶瑕佸垱寤洪檷搴忕储寮曪紝鍚﹀垯鏄疷sing filesor锛屾晥鐜囧緢浣?/p>

鍗囧簭/闄嶅簭鑱斿悎绱㈠紩缁撴瀯锛? oder by浼樺寲鍘熷垯锛?/p>

鏍规嵁鎺掑簭瀛楁寤虹珛鍚堥€傜殑绱㈠紩锛屽瀛楁鎺掑簭鏃讹紝涔熼伒寰渶宸﹀墠缂€娉曞垯灏介噺浣跨敤瑕嗙洊绱㈠紩澶氬瓧娈垫帓搴? 涓€涓崌搴忎竴涓檷搴忥紝姝ゆ椂闇€瑕佹敞鎰忚仈鍚堢储寮曞湪鍒涘缓鏃剁殑瑙勫垯锛圓sc/desc锛夊鏋滀笉鍙伩鍏嶇殑鍑虹幇filesort锛屽ぇ鏁版嵁閲忔帓搴忔椂锛屽彲浠ラ€傚綋澧炲ぇ鎺掑簭缂撳啿鍖哄ぇ灏弒ort_buffer_size(榛樿256k)銆?

3.5 group by浼樺寲

鍒嗙粍鎿嶄綔锛屾垜浠富瑕佹潵鐪嬬湅绱㈠紩瀵逛簬鍒嗙粍鎿嶄綔鐨?/p>

绀轰緥锛?/p>

鈶犳暟鎹噯澶囷細tb_user琛ㄥ彧鏈変富閿储寮?/p>

鈶$洿鎺ユ煡璇㈡病鏈夌储寮曠殑瀛楁

鈶㈠垱寤鸿仈鍚堢储寮曪紝鐒跺悗杩涜鏌ヨ锛堟弧瓒虫渶宸﹀墠缂€娉曞垯鐨勬儏鍐典笅锛?/p>

鈶f煡璇紝浣嗕笉婊¤冻鏈€宸﹀墠缂€娉曞垯锛屾鏃朵細浣跨敤鍒颁复鏃惰〃锛屾晥鐜囪緝浣?/p>

group by浼樺寲鍘熷垯锛?

鍦ㄥ垎缁勬搷浣滄椂锛屽彲浠ラ€氳繃绱㈠紩鏉ユ彁楂樻晥鐜囧垎缁勬搷浣滄椂锛岀储寮曠殑浣跨敤涔熸槸婊¤冻鏈€宸﹀墠缂€娉曞垯鐨?

3.6 limit浼樺寲

鍦ㄦ暟鎹噺姣旇緝澶ф椂锛屽鏋滆繘琛宭imit鍒嗛〉鏌ヨ锛屽湪鏌ヨ鏃讹紝瓒婂線鍚庯紝鍒嗛〉鏌ヨ鏁堢巼瓒婁綆

绀轰緥

鈶犳暟鎹噯澶囷細鍑嗗涓€寮犺繕鏈?000w鏉¤褰曠殑tb_sku琛?/p>

鈶″垎椤垫煡璇㈠墠10鏉℃暟鎹?select * from tb_sku limit 0,10; 鑰楁椂0.0sec

鈶㈠垎椤垫煡璇㈢100w鏉¤褰曡捣濮嬬殑10鏉¤褰?select * from tb_sku limit 1000000,10; 鑰楁椂3.09sec

鈶e垎椤垫煡璇?00鏉¤褰曡捣濮嬬殑10鏉¤褰?select * from tb_sku limit 900000,10; 鑰楁椂 14.11 sec

閫氳繃娴嬭瘯鎴戜滑浼氱湅鍒帮紝limit鏌ヨ瓒婂線鍚庯紝鍒嗛〉鏌ヨ鏁堢巼瓒婁綆锛岃繖灏辨槸鍒嗛〉鏌ヨ鐨勯棶棰樻墍鍦ㄣ€傚洜涓猴紝褰撳湪杩涜鍒嗛〉鏌ヨ鏃讹紝濡傛灉鎵ц limit 2000000,10 锛屾鏃堕渶瑕丮ysql鎺掑簭鍓?000010 璁板綍锛屼粎浠呰繑鍥?2000000 - 2000010 鐨勮褰曪紝鍏朵粬璁板綍涓㈠純锛屾煡璇㈡帓搴忕殑浠d环闈炲父澶?/p>

浼樺寲鎬濊矾锛氫竴鑸垎椤垫煡璇㈡椂锛岄€氳繃鍒涘缓 瑕嗙洊绱㈠紩 鑳藉姣旇緝濂藉湴鎻愰珮鎬ц兘锛屽彲浠ラ€氳繃瑕嗙洊绱㈠紩鍔犲瓙鏌ヨ褰㈠紡杩涜浼樺寲

鍘熷sql锛氾紙鑰楁椂14.11 sec锛?/p>

select * from tb_sku limit 900000,10;

瑕嗙洊绱㈠紩 瀛愭煡璇紭鍖栵細锛堣€楁椂8.12 sec锛?/p>

select * from tb_sku t , (select id from tb_sku order by id limit 2000000,10) a where t.id = a.id;

澶囨敞锛氬皢select id from tb_sku order by id limit 2000000,10;鐨勬煡璇㈢粨鏋滅湅浣滀竴寮犺〃锛岃繘琛屽琛ㄨ仈鍚堟煡璇?/p>

3.7 count浼樺寲

myisam 寮曟搸鎶婁竴涓〃鐨勬€昏鏁板瓨鍦ㄤ簡纾佺洏涓婏紝鍥犳鎵ц count(*) 鐨勬椂鍊欎細鐩存帴杩斿洖杩欎釜鏁帮紝鏁堢巼寰堥珮锛?浣嗘槸濡傛灉鏄甫鏉′欢鐨刢ount锛孧yisam涔熸參銆侷nnodb 寮曟搸灏遍夯鐑︿簡锛屽畠鎵ц count(*) 鐨勬椂鍊欙紝闇€瑕佹妸鏁版嵁涓€琛屼竴琛屽湴浠庡紩鎿庨噷闈㈣鍑烘潵锛岀劧鍚庣疮绉鏁般€傚鏋滆瑕佸ぇ骞呭害鎻愬崌innodb琛ㄧ殑count鏁堢巼锛屼富瑕佺殑浼樺寲鎬濊矾锛氳嚜宸辫鏁?鍙互鍊熷姪浜巖edis杩欐牱鐨勬暟鎹簱杩涜,浣嗘槸濡傛灉鏄甫鏉′欢鐨刢ount鍙堟瘮杈冮夯鐑︿簡)

count鐨勭敤娉曪細count() 鏄竴涓仛鍚堝嚱鏁帮紝瀵逛簬杩斿洖鐨勭粨鏋滈泦锛屼竴琛岃鍦板垽鏂紝濡傛灉 count 鍑芥暟鐨勫弬鏁颁笉鏄疦ull锛岀疮璁″€煎氨鍔?1锛屽惁鍒欎笉鍔狅紝鏈€鍚庤繑鍥炵疮璁″€笺€傜敤娉曪細count锛?锛夈€乧ount锛堜富閿級銆乧ount锛堝瓧娈碉級銆乧ount锛堟暟瀛楋級 鏁堢巼瀵规瘮锛?

c

o

u

n

t

(

瀛楁

)

<

c

o

u

n

t

(

涓婚敭

i

d

)

<

c

o

u

n

t

(

1

)

鈮?

c

o

u

n

t

(

鈭?

)

锛屾墍浠ュ敖閲忎娇鐢?

c

o

u

n

t

(

鈭?

)

count(瀛楁) < count(涓婚敭 id) < count(1) 鈮?count(*)锛屾墍浠ュ敖 閲忎娇鐢?count(*)

count(瀛楁)

3.8 update浼樺寲

update娉ㄦ剰浜嬮」锛氳繘琛屾潯浠舵洿鏂版椂锛屽鏋滄潯浠跺瓧娈垫湁绱㈠紩锛屽垯mysql鏄娇鐢ㄨ閿侊紱濡傛灉鏉′欢瀛楁娌℃湁绱㈠紩锛屽垯mysql鏄娇鐢ㄨ〃閿併€傛墍浠ュ湪鎵ц鏇存柊鎿嶄綔鏃讹紝鏈€濂芥牴鎹储寮曞瓧娈佃繘琛屾洿鏂拌閿侊細灏嗚〃涓竴琛屾暟鎹攣浣忥紝鍙湁绛変簨鍔℃彁浜ゅ悗鎵嶈兘琚闂〃閿侊細灏嗕竴鏁村紶琛ㄩ攣浣忥紝鍙湁绛変簨鍔℃彁浜ゅ悗鎵嶈兘琚闂?/p>

娉ㄦ剰锛欼nnodb鐨勮閿佹槸閽堝绱㈠紩鍔犵殑閿侊紝涓嶆槸閽堝璁板綍鍔犵殑閿?,骞朵笖璇ョ储寮曚笉鑳藉け鏁堬紝鍚﹀垯浼氫粠琛岄攣鍗囩骇涓鸿〃閿?/p>

4銆佽鍥?瀛樺偍杩囩▼/瑙﹀彂鍣?/p>

4.1 瑙嗗浘

4.1.1 瑙嗗浘姒傝堪

浠€涔堟槸瑙嗗浘锛?/p>

瑙嗗浘锛圴iew锛夋槸涓€绉嶈櫄鎷熷瓨鍦ㄧ殑琛ㄣ€傝鍥句腑鐨勬暟鎹苟涓嶅湪鏁版嵁搴撲腑瀹為檯瀛樺湪锛岃鍜屽垪鏁版嵁鏉ヨ嚜瀹氫箟瑙嗗浘鐨勬煡璇腑浣跨敤鐨勮〃锛屽苟涓旀槸鍦ㄤ娇鐢ㄨ鍥炬椂鍔ㄦ€佺敓鎴愮殑銆傞€氫織鐨勮锛岃鍥惧彧淇濆瓨浜嗘煡璇㈢殑sql閫昏緫锛屼笉淇濆瓨鏌ヨ缁撴灉銆傛墍浠ユ垜浠湪鍒涘缓瑙嗗浘鐨勬椂鍊欙紝涓昏鐨勫伐浣滃氨钀藉湪鍒涘缓杩欐潯sql鏌ヨ璇彞涓娿€?/p>

瑙嗗浘鐨勪綔鐢紵

绠€鍖栨煡璇㈡搷浣滐細瑙嗗浘鍙互灏嗗鏉傜殑鏌ヨ鎿嶄綔灏佽璧锋潵锛屽舰鎴愪竴涓畝鍗曟槗鐢ㄧ殑鏌ヨ鎺ュ彛锛岄伩鍏嶄簡閲嶅缂栧啓澶嶆潅鏌ヨ璇彞鐨勯夯鐑︺€傛彁楂樻煡璇㈡晥鐜囷細瑙嗗浘鍙互棰勫厛璁$畻鏌ヨ缁撴灉锛屽苟灏嗙粨鏋滅紦瀛樿捣鏉ワ紝澶氭鏌ヨ鏃跺彲浠ョ洿鎺ヤ娇鐢ㄧ紦瀛樼粨鏋滐紝鎻愰珮鏌ヨ鏁堢巼銆傚疄鐜版暟鎹畨鍏ㄦ€э細閫氳繃瑙嗗浘锛屽彲浠ラ檺鍒剁敤鎴峰鏌愪簺鏁忔劅淇℃伅鐨勮闂紝鍙厑璁哥敤鎴锋煡鐪嬫湁闄愮殑淇℃伅锛屼繚鎶ゆ暟鎹殑瀹夊叏鎬с€傜鐞嗘暟鎹闂潈闄愶細瑙嗗浘鍙互瀵逛笉鍚岀敤鎴峰垎閰嶄笉鍚岀殑璁块棶鏉冮檺锛岄檺鍒剁敤鎴风殑鏁版嵁璁块棶鑼冨洿锛岀淮鎶ゆ暟鎹殑瀹夊叏鎬у拰淇濆瘑鎬с€傛暟鎹嫭绔嬫€э細瑙嗗浘鍙府鍔╃敤鎴峰睆钄界湡瀹炶〃缁撴瀯鍙樺寲甯︽潵鐨勫奖鍝?/p>

浠€涔堟槸鍩鸿〃锛?/p>

鍦ㄦ暟鎹簱涓紝鍩鸿〃锛圔ase table锛夋槸鎸囧疄闄呭瓨鍌ㄦ暟鎹殑琛紝涔熺о涓虹墿鐞嗚〃銆傚熀琛ㄥ寘鍚簡鏁版嵁琛ㄤ腑鐨勬墍鏈夎褰曞拰瀛楁锛屽畠鏄暟鎹簱涓渶鍩烘湰鐨勬暟鎹瓨鍌ㄥ崟鍏冦€?/p>

娉ㄦ剰锛氳鍥炬湰韬笉瀛樺偍鏁版嵁锛屾暟鎹兘鏄瓨鍌ㄥ湪鍩鸿〃涓紝瑙嗗浘鐨勫垱寤恒€佹煡璇篃渚濊禆浜庡熀琛紝濡傛灉鍩鸿〃琚垹闄や簡锛屽熀琛ㄥ搴旂殑瑙嗗浘涔熶細璺熺潃娑堝け

4.1.2 瑙嗗浘鐨勫熀鏈娇鐢?/p>

鍒涘缓瑙嗗浘 create [or replace] view [algorithm={undefined|merge|temptable}] 瑙嗗浘鍚嶇о[(鍒楀悕鍒楄〃)] as select璇彞 [ with [ cascaded | local ] check option ]

鍙傛暟璇存槑锛?/p>

or replace锛氬鏋滆鍥惧凡瀛樺湪锛屽垯鍒犻櫎宸插瓨鍦ㄧ殑鐒跺悗鍒涘缓涓€涓柊鐨勶紙濡傛灉瀛樺湪涓嶅姞杩欎釜鍙傛暟鐩存帴鍒涘缓锛屼細鎶ラ敊鈥濆璞″凡瀛樺湪鈥滐級algorithm锛氬彲閫夈€傝〃绀鸿鍥鹃€夋嫨鐨勭畻娉曘€傦紙涓€鑸儏鍐电洿鎺ヤ娇鐢∕ysql榛樿鐨勫氨濂戒簡锛?

undefined锛氳〃绀篗ysql灏嗚嚜鍔ㄩ€夋嫨鎵€瑕佷娇鐢ㄧ殑绠楁硶銆侻erge锛氳〃绀哄皢浣跨敤瑙嗗浘鐨勮鍙ヤ笌瑙嗗浘瀹氫箟鍚堝苟璧锋潵锛屼娇寰楄鍥惧畾涔夌殑鏌愪竴閮ㄥ垎鍙栦唬璇彞鐨勫搴旈儴鍒嗐€俆emptable锛氳〃绀哄皢瑙嗗浘鐨勭粨鏋滃瓨鍏ヤ复鏃惰〃锛岀劧鍚庝娇鐢ㄤ复鏃惰〃鎵ц璇彞銆?with check option锛氬彲閫夈€傝〃绀轰慨鏀硅鍥炬椂瑕佷繚璇佸湪璇ヨ鍥剧殑鏉冮檺鑼冨洿涔嬪唴锛屾暟鎹笉绗﹀悎鏉′欢鐩存帴鎶ラ敊cascaded锛氬彲閫夈€傝〃绀轰慨鏀硅鍥炬椂锛岄渶瑕佹弧瓒宠窡璇ヨ鍥炬湁鍏崇殑鎵€鏈夌浉鍏宠鍥惧拰琛ㄧ殑鏉′欢锛岃鍙傛暟涓洪粯璁ゅ€笺€侺ocal锛氳〃绀轰慨鏀硅鍥炬椂锛屽彧瑕佹弧瓒宠瑙嗗浘鏈韩瀹氫箟鐨勬潯浠跺嵆鍙? 鏌ヨ瑙嗗浘 # 鏌ョ湅鍒涘缓瑙嗗浘璇彞锛?/p>

show create view 瑙嗗浘鍚嶇о;

# 鏌ョ湅瑙嗗浘鏁版嵁锛?/p>

select * from 瑙嗗浘鍚嶇о ...... ;

淇敼瑙嗗浘 # 鏂瑰紡涓€锛?/p>

create [or replace] view 瑙嗗浘鍚嶇о[(鍒楀悕鍒楄〃)] as select璇彞 [ with [ cascaded | local ] check option ]

# 鏂瑰紡浜岋細

alter view 瑙嗗浘鍚嶇о[(鍒楀悕鍒楄〃)] as select璇彞 [ with [ cascaded | local ] check option ]

鍒犻櫎瑙嗗浘 drop view [if exists] 瑙嗗浘鍚嶇о [,瑙嗗浘鍚嶇о] ...

鏇存柊瑙嗗浘 瑕佷娇瑙嗗浘鍙洿鏂帮紝瑙嗗浘涓殑琛屼笌鍩虹琛ㄤ腑鐨勮涔嬮棿蹇呴』瀛樺湪涓€瀵逛竴鐨勫叧绯汇€傚鏋滆鍥惧寘鍚互涓嬩换浣曚竴椤癸紝鍒欒瑙嗗浘涓嶅彲鏇存柊锛?/p>

鑱氬悎鍑芥暟鎴栫獥鍙e嚱鏁帮紙sum()銆?min()銆?max()銆?count()绛夛級distinctgroup byhavingunion 鎴栬€?union all 鈥︹€?# 鍒涘缓涓€涓鍥?/p>

create view stu_v_count as select count(*) from student;

# 寰€瑙嗗浘涓彃鍏ヤ竴鏉¤褰?/p>

insert into stu_v_count values(10);

鎻掑叆鎿嶄綔鐩存帴鐖嗙矖the target table stu_v_count of the insert is not insertable-into锛岃繖鏄洜涓哄悜瑙嗗浘鎻掑叆鏁版嵁鏃讹紝鎻掑叆鐨勬暟鎹繀椤讳笌鍩鸿〃瀛樺湪涓€瀵逛竴鐨勫叧绯伙紝鎴戜滑杩欏紶瑙嗗浘瀹冪殑鍩鸿〃鏄痵tudent锛屼絾鏄鍥句腑鏄暟鎹苟涓嶆槸涓€瀵逛竴鐨勫叧绯伙紝瀹冩槸count(*)锛屾樉鐒舵槸涓€瀵瑰鐨勫叧绯伙紝鎵€浠ユ鏃舵垜浠彃鍏ュ氨浼氱洿鎺ユ姤閿?

绀轰緥锛?/p>

# 鍒涘缓瑙嗗浘

create or replace view stu_v_1 as select id,name from student where id <= 10;

# 鏌ヨ瑙嗗浘

-- 鏌ヨ瑙嗗浘鐨勫垱寤鸿鍙?/p>

show create view stu_v_1;

-- 鏌ヨ瑙嗗浘涓殑鏁版嵁

select * from stu_v_1;

select * from stu_v_1 where id = 1;

# 淇敼瑙嗗浘

-- 鏂瑰紡涓€锛氫娇鐢╟reate璇彞

create or replace view stu_v_1 as select id,name,no from student where id <= 10;

-- 鏂瑰紡浜岋細浣跨敤alter璇彞

alter view stu_v_1 as select id,name from student where id <= 10;

# 鍒犻櫎瑙嗗浘

drop view if exists stu_v_1;

# 鍒╃敤瑙嗗浘灞忚斀鏁忔劅瀛楁

-- 寮€鍙戜汉鍛樺彧鑳界湅鍒扮敤鎴风殑鍩烘湰瀛楁锛屽睆钄芥墜鏈哄彿鍜岄偖绠变袱涓瓧娈?/p>

create or replace view view_tb_user as select id,name,profession,age,gender,status,createtime from tb_user;

select * from view_tb_user;

# 鍒╃敤瑙嗗浘绠€鍖栧琛ㄨ仈鍚堟煡璇?/p>

-- 2.鏌ヨ姣忎釜瀛︾敓鎵€閫変慨鐨勮绋嬶紙涓夎〃鑱旀煡锛岃繖涓姛鑳藉湪寰堝涓氬姟涓兘瑕佺敤鍒帮紝鎵€浠ュ彲浠ョ洿鎺ュ畾涔変竴涓鍥撅紝鎻愰珮鏌ヨ鏁堢巼锛?/p>

select s.name student_name, s.no student_no, c.name course_name from student s, student_course sc, course c where s.id = sc.studentid and c.id = sc.courseid;

create or replace view view_student_course as select s.name student_name, s.no student_no, c.name course_name from student s, student_course sc, course c where s.id = sc.studentid and c.id = sc.courseid;

select * from view_student_course;

4.1.3 閫夐」閰嶇疆璇﹁в

妫€鏌ラ€夐」锛歐ith check option 鍓嶉潰鎴戜滑璇磋繃锛岃鍥炬槸涓€寮犺櫄鎷熻〃锛屽湪鍚戣鍥句腑鎻掑叆鏁版嵁鏃讹紝鎴戜滑鍙兘浼氶亣鍒帮紝寰€瑙嗗浘涓彃鍏ユ暟鎹垚鍔熶簡锛屼絾鏄鍥惧嵈鏌ヤ笉鍒帮紒 杩欐槸浠€涔堝師鍥犲憿锛熻繖鏄洜涓烘垜浠湪鍒涘缓瑙嗗浘鐨勬椂鍊欙紝鎸囧畾鐨勬潯浠朵负 id<=10, id涓?0鐨勬暟鎹紝鏄笉绗﹀悎鏉′欢鐨勶紝鎵€浠ユ病鏈夋煡 璇㈠嚭鏉ワ紝浣嗘槸杩欐潯鏁版嵁纭疄鏄凡缁忔垚鍔熺殑鎻掑叆鍒颁簡鍩鸿〃涓€傞偅濡備綍閬垮厤杩欑鎯呭喌鍛紵杩欏氨闇€瑕佺敤鍒拌鍥剧殑妫€鏌ラ€夐」浜? 鍙互鐪嬪埌娣诲姞浜嗘鏌ラ€夐」鍚庯紝鎴戜滑鍐嶆彃鍏ヤ笉绗﹀悎鏉′欢鐨勬暟鎹椂锛孧ysql浼氱洿鎺ユ姤閿欙細 妫€鏌ヨ寖鍥?/p>

褰撲娇鐢╓ith check option瀛愬彞鍒涘缓瑙嗗浘鏃讹紝mysql浼氶€氳繃瑙嗗浘妫€鏌ユ鍦ㄦ洿鏀圭殑姣忎釜琛岋紝渚嬪锛氭彃鍏ワ紝鏇存柊锛屽垹闄わ紝浠ヤ娇鍏剁鍚堣鍥剧殑瀹氫箟銆?mysql鍏佽鍩轰簬鍙︿竴涓鍥惧垱寤鸿鍥撅紝瀹冭繕浼氭鏌ヤ緷璧栬鍥句腑鐨勮鍒欎互淇濇寔涓€鑷存€с€備负浜嗙‘瀹氭鏌ョ殑鑼冨洿锛宮ysql鎻愪緵浜嗕袱涓€夐」锛?cascaded 鍜?local锛岄粯璁ゅ€间负 cascaded 銆?/p>

cascaded锛氬己鍒剁骇鑱旓紝浼氬己鍒舵鏌ョ埗绾ц鍥剧殑绱㈠紩銆傚湪杩涜瑙嗗浘鎻掑叆銆佹洿鏂般€佸垹闄ゆ搷浣滄椂锛屼笉浠呮鏌ユ槸鍚︽弧瓒冲綋鍓嶈鍥剧殑绾︽潫锛岃繕瑕佹鏌ユ槸鍚︽弧瓒虫墍鏈夌殑鐖剁骇瑙嗗浘鐨勭害鏉熴€傛瘮濡傦紝v2瑙嗗浘鏄熀浜巚1瑙嗗浘鐨勶紝濡傛灉鍦╲2瑙嗗浘鍒涘缓鐨勬椂鍊欐寚瀹氫簡妫€鏌ラ€夐」涓?cascaded锛屼絾鏄痸1瑙嗗浘鍒涘缓鏃舵湭鎸囧畾妫€鏌ラ€夐」銆?鍒欏湪鎵ц妫€鏌ユ椂锛屼笉浠呬細妫€鏌2锛岃繕浼氱骇鑱旀鏌2鐨勫叧鑱旇鍥緑1銆? # 瑙嗗浘1 鎻掑叆鏁版嵁鐨刬d娌℃湁闄愬埗

create or replace view stu_v_1 as select id,name from student where id <= 20;

insert into stu_v_3 values(9,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_3 values(16,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_2 values(30,'tom'); -- 鎻掑叆鎴愬姛

# 瑙嗗浘2 鎻掑叆鏁版嵁鐨刬d蹇呴』 >=10 && <=20锛屽惁鍒欑洿鎺ユ姤閿?/p>

create or replace view stu_v_2 as select id,name from stu_v_1 where id >= 10 with cascaded check option;

insert into stu_v_3 values(9,'tom'); -- 鎻掑叆澶辫触

insert into stu_v_3 values(16,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_2 values(30,'tom'); -- 鎻掑叆澶辫触锛堝己鍒舵鏌ョ埗绾ц鍥剧殑绾︽潫锛?/p>

# 瑙嗗浘3 鎻掑叆鏁版嵁鐨刬d蹇呴』 >=10 && <=20锛屽惁鍒欑洿鎺ユ姤閿?/p>

create or replace view stu_v_3 as select id,name from stu_v_2 where id <=30 with cascaded check option;

insert into stu_v_3 values(9,'tom'); -- 鎻掑叆澶辫触

insert into stu_v_3 values(16,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_2 values(30,'tom'); -- 鎻掑叆澶辫触锛堝己鍒舵鏌ョ埗绾ц鍥剧殑绾︽潫锛?/p>

local锛氭湰鍦扮骇鑱旓紝涓嶄細寮哄埗妫€鏌ョ埗绾ц鍥俱€傛瘮濡傦紝v2瑙嗗浘鏄熀浜巚1瑙嗗浘鐨勶紝濡傛灉鍦╲2瑙嗗浘鍒涘缓鐨勬椂鍊欐寚瀹氫簡妫€鏌ラ€夐」涓?local 锛屼絾鏄痸1瑙嗗浘鍒涘缓鏃舵湭鎸囧畾妫€鏌ラ€夐」銆?鍒欏湪鎵ц妫€鏌ユ椂锛屽彧浼氭鏌2锛屼笉浼氭鏌2鐨勫叧鑱旇鍥緑1 # 瑙嗗浘1 鎻掑叆鏁版嵁鐨刬d娌℃湁闄愬埗

create or replace view stu_v_1 as select id,name from student where id <= 20;

insert into stu_v_1 values(9,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_1 values(16,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_1 values(30,'tom'); -- 鎻掑叆鎴愬姛

# 瑙嗗浘2 鎻掑叆鏁版嵁鐨刬d蹇呴』 >=10锛屽惁鍒欑洿鎺ユ姤閿?/p>

create or replace view stu_v_2 as select id,name from stu_v_1 where id >= 10 with local check option;

insert into stu_v_2 values(9,'tom'); -- 鎻掑叆澶辫触

insert into stu_v_2 values(16,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_2 values(30,'tom'); -- 鎻掑叆鎴愬姛锛堜笉浼氬己鍒舵鏌ョ埗绾ц鍥剧殑绾︽潫锛?/p>

# 瑙嗗浘3 鎻掑叆鏁版嵁鐨刬d蹇呴』 >=10 && <=30锛屽惁鍒欑洿鎺ユ姤閿檒ocal

create or replace view stu_v_3 as select id,name from stu_v_2 where id <=30 with local check option;

insert into stu_v_3 values(9,'tom'); -- 鎻掑叆澶辫触

insert into stu_v_3 values(16,'tom'); -- 鎻掑叆鎴愬姛

insert into stu_v_3 values(30,'tom'); -- 鎻掑叆鎴愬姛锛堜笉浼氬己鍒舵鏌ョ埗绾ц鍥剧殑绾︽潫锛?/p>

4.2 瀛樺偍杩囩▼

4.2.1 瀛樺偍杩囩▼姒傝堪

浠€涔堟槸瀛樺偍杩囩▼锛?/p>

瀛樺偍杩囩▼锛圫tored procedure锛夋槸浜嬪厛缁忚繃缂栬瘧骞跺瓨鍌ㄥ湪鏁版嵁搴撲腑鐨勪竴娈?sql 璇彞鐨勯泦鍚堬紙鍏跺疄璇寸櫧浜嗗氨鏄竴涓嚱鏁帮級锛岃皟鐢ㄥ瓨鍌ㄨ繃绋嬪彲浠ョ畝鍖栧簲鐢ㄥ紑鍙戜汉鍛樼殑寰堝宸ヤ綔锛屽噺灏戞暟鎹湪鏁版嵁搴撳拰搴旂敤鏈嶅姟鍣ㄤ箣闂寸殑浼犺緭锛屽浜庢彁楂樻暟鎹鐞嗙殑鏁堢巼鏄湁濂藉鐨勩€傚瓨鍌ㄨ繃绋嬫€濇兂涓婂緢绠€鍗曪紝灏辨槸鏁版嵁搴?sql 璇█灞傞潰鐨勪唬鐮佸皝瑁呬笌閲嶇敤銆?/p>

瀛樺偍杩囩▼鐨勪綔鐢ㄦ槸浠€涔堬紵

鎻愰珮鎬ц兘锛氬瓨鍌ㄨ繃绋嬪彲浠ュ湪鏁版嵁搴撴湇鍔″櫒涓婃墽琛岋紝鍑忓皯浜嗗鎴风涓庢湇鍔″櫒涔嬮棿鐨勭綉缁滈€氫俊锛屼粠鑰屾彁楂樹簡鎬ц兘銆傞噸鐢ㄤ唬鐮侊細瀛樺偍杩囩▼鍙互鍦ㄥ涓簲鐢ㄧ▼搴忎腑閲嶅浣跨敤鐩稿悓鐨勪唬鐮侀€昏緫锛屼粠鑰屾彁楂樹簡寮€鍙戞晥鐜囥€傜畝鍖栧鏉傛搷浣滐細瀛樺偍杩囩▼鍙互瀹炵幇澶嶆潅鐨勬搷浣滃拰閫昏緫锛屾瘮濡傛潯浠跺垽鏂€佸惊鐜€佸紓甯稿鐞嗙瓑锛屼粠鑰岀畝鍖栦簡寮€鍙戜汉鍛樼殑宸ヤ綔銆傛敼鍠勬暟鎹畨鍏細瀛樺偍杩囩▼鍙互闄愬埗鐢ㄦ埛瀵规暟鎹簱鐨勮闂紝浠庤€屾彁楂樹簡鏁版嵁鐨勫畨鍏ㄦ€с€傛澶栵紝瀛樺偍杩囩▼杩樺彲浠ラ槻姝ql娉ㄥ叆鏀诲嚮銆傜淮鎶ゆ柟渚匡細瀛樺偍杩囩▼鍙互鍦ㄦ暟鎹簱涓繘琛屽瓨鍌ㄣ€佷慨鏀瑰拰璋冪敤锛屼粠鑰屾柟渚夸簡鏁版嵁搴撶殑缁存姢鍜岀鐞嗐€?/p>

瀛樺偍杩囩▼鐨勭壒鐐规湁鍝簺锛?/p>

棰勭紪璇戯細瀛樺偍杩囩▼鍦ㄥ垱寤烘椂琚紪璇戯紝鎵€浠ュ畠鐨勬墽琛岄€熷害姣斿姩鎬丼ql璇彞瑕佸揩銆傛墽琛屾晥鐜囬珮锛氬瓨鍌ㄨ繃绋嬪湪鏁版嵁搴撴湇鍔″櫒涓婃墽琛岋紝鍑忓皯浜嗗鎴风涓庢湇鍔″櫒涔嬮棿鐨勭綉缁滈€氫俊锛屼粠鑰屾彁楂樹簡鎵ц鏁堢巼銆傚鏋滄秹鍙婂澶氭潯sql锛屾病鎵ц涓€娆¢兘鏄竴娆$綉缁滀紶杈擄紝浣嗘槸浣跨敤瀛樺偍杩囩▼鍚庯紝鍙渶瑕佽繘琛屼竴娆$綉缁滀氦浜掑嵆鍙彲閲嶇敤锛氬瓨鍌ㄨ繃绋嬪彲浠ュ湪澶氫釜搴旂敤绋嬪簭涓噸澶嶄娇鐢ㄧ浉鍚岀殑浠g爜閫昏緫锛屼粠鑰屾彁楂樹簡寮€鍙戞晥鐜囥€傚彲鎵╁睍鎬у己锛氬瓨鍌ㄨ繃绋嬪彲浠ュ疄鐜板鏉傜殑鎿嶄綔鍜岄€昏緫锛屾瘮濡傛潯浠跺垽鏂€佸惊鐜€佸紓甯稿鐞嗙瓑锛屼粠鑰屾彁楂樹簡鍙墿灞曟€с€傛暟鎹畨鍏ㄦ€ч珮锛氬瓨鍌ㄨ繃绋嬪彲浠ラ檺鍒剁敤鎴峰鏁版嵁搴撶殑璁块棶锛屼粠鑰屾彁楂樹簡鏁版嵁鐨勫畨鍏ㄦ€с€傛澶栵紝瀛樺偍杩囩▼杩樺彲浠ラ槻姝ql娉ㄥ叆鏀诲嚮缁存姢鏂逛究锛氬瓨鍌ㄨ繃绋嬪彲浠ュ湪鏁版嵁搴撲腑杩涜瀛樺偍銆佷慨鏀瑰拰璋冪敤锛屼粠鑰屾柟渚夸簡鏁版嵁搴撶殑缁存姢鍜岀鐞嗗彲鎺ュ彈鍙傛暟锛屼篃鍙互杩斿洖鏁版嵁锛氬湪瀛樺偍杩囩▼涓紝鏃㈠彲浼犻€掑弬鏁帮紝鍙堝彲浠ユ帴鏀惰繑鍥炲€?/p>

4.2.2 瀛樺偍杩囩▼鐨勫熀鏈娇鐢?/p>

鍒涘缓 create procedure 瀛樺偍杩囩▼鍚嶇о ([ 鍙傛暟鍒楄〃 ])

begin

-- sql璇彞

end;

ps锛氬弬鏁板垪琛ㄨ鍙傝€?.2.3 璋冪敤 call 鍚嶇о ([ 鍙傛暟 ]);

鏌ョ湅 -- 鏌ヨ鎸?/p>

瀹氭暟鎹簱鐨勫瓨鍌ㄨ繃绋嬪強鐘舵€佷俊鎭?/p>

select * from information_schema.routines where routine_schema = 'xxx';

-- 鏌ヨ鏌愪釜瀛樺偍杩囩▼鐨勫畾涔?/p>

show create procedure 瀛樺偍杩囩▼鍚嶇о;

鍒犻櫎 drop procedure [ if exists ] 瀛樺偍杩囩▼鍚嶇о;

绀轰緥

# 鍒涘缓瀛樺偍杩囩▼

create procedure p1()

begin

select count(*) from student;

end;

# 璋冪敤瀛樺偍杩囩▼

call p1();

# 鏌ョ湅瀛樺偍杩囩▼

-- 鏌ョ湅鎸囧畾鏁版嵁搴撶殑瀛樺偍杩囩▼鍙婄姸鎬佷俊鎭?/p>

select * from information_schema.routines where routine_schema = 'mysql_study';

-- 鏌ヨ瀛樺偍杩囩▼鐨勫垱寤鸿鍙?/p>

show create procedure p1;

# 鍒犻櫎瀛樺偍杩囩▼

drop procedure if exists p1;

娉ㄦ剰锛氬湪鍛戒护琛屼腑锛屾墽琛屽垱寤哄瓨鍌ㄨ繃绋嬬殑sql鏃讹紝闇€瑕侀€氳繃鍏抽敭瀛?delimiter 鎸囧畾sql璇彞鐨勭粨鏉熺锛屽洜涓哄懡浠よ涓粯璁ゆ槸浠?涓虹粨鏉熺鐨?/p>

delimiter $$鎸囧畾$$涓虹粨鏉熺

4.2.3 鍙橀噺

鍦∕ysql涓彉閲忓垎涓轰笁绉嶇被鍨? 绯荤粺鍙橀噺銆佺敤鎴峰畾涔夊彉閲忋€佸眬閮ㄥ彉閲?/p>

绯荤粺鍙橀噺锛氱郴缁熷彉閲?鏄疢ysql鏈嶅姟鍣ㄦ彁渚涳紝涓嶆槸鐢ㄦ埛瀹氫箟鐨勶紝灞炰簬鏈嶅姟鍣ㄥ眰闈€傚垎涓哄叏灞€鍙橀噺锛圙lobal锛夈€佷細璇濆彉閲忥紙session锛?/p>

鏌ョ湅绯荤粺鍙橀噺 -- 鏌ョ湅鎵€鏈夌郴缁熷彉閲?/p>

show [ session | global ] variables ;

-- 鍙互閫氳繃like妯$硦鍖归厤鏂瑰紡鏌ユ壘鍙橀噺

show [ session | global ] variables like '......';

-- 鏌ョ湅鎸囧畾鍙橀噺鐨勫€?/p>

select @@[session | global] 绯荤粺鍙橀噺鍚?

澶囨敞锛?/p>

鍏ㄥ眬鍙橀噺(global)锛氬叏灞€鍙橀噺閽堝浜庢墍鏈夌殑浼氳瘽銆備細璇濆彉閲?session)锛氫細璇濆彉閲忛拡瀵逛簬鍗曚釜浼氳瘽锛屽彧鍦ㄦ湰浼氳瘽绐楀彛鏈夋晥锛屽湪鍙﹀涓€涓細璇濈獥鍙e氨涓嶇敓鏁堜簡 璁剧疆绯荤粺鍙橀噺 set [ session | global ] 绯荤粺鍙橀噺鍚?= 鍊?;

set @@[session | global] 绯荤粺鍙橀噺鍚?= 鍊?;

娉ㄦ剰锛?/p>

濡傛灉娌℃湁鎸囧畾session/global锛岄粯璁ゆ槸session锛屼細璇濆彉閲廙ysql 鏈嶅姟閲嶆柊鍚姩涔嬪悗锛屾墍璁剧疆鐨勫叏灞€鍙傛暟浼氬け鏁堬紝瑕佹兂涓嶅け鏁堬紝鍙互鍦?/etc/my.cnf 涓厤缃?绀轰緥 # 鏌ョ湅绯荤粺鍙橀噺

-- 鏌ョ湅鎵€鏈夌殑绯荤粺鍙橀噺锛圡ysql榛樿鐨勬槸session锛?/p>

show variables;

-- 鏌ョ湅鎵€鏈夌殑浼氳瘽绯荤粺鍙橀噺

show session variables;

-- 鏌ョ湅鎵€鏈夌殑鍏ㄥ眬绯荤粺鍙橀噺

show global variables;

-- 鏌ョ湅鎸囧畾鐨勭郴缁熷彉閲忥紙鏌ョ湅浜嬪姟寮€鍚嚜鍔ㄦ彁浜わ紝榛樿鏌ョ湅鐨勬槸session绾у埆鐨勶級

select @@autocommit;

-- 鏌ョ湅鎸囧畾鐨凷ession绾у埆鐨勭郴缁熷彉閲?/p>

select @@session.autocommit;

# 璁剧疆绯荤粺鍙橀噺

set global autocommit = 1;

鐢ㄦ埛瀹氫箟鍙橀噺锛氱敤鎴峰畾涔夊彉閲?鏄敤鎴锋牴鎹渶瑕佽嚜宸卞畾涔夌殑鍙橀噺锛岀敤鎴峰彉閲忎笉鐢ㄦ彁鍓嶅0鏄庯紝鍦ㄧ敤鐨勬椂鍊欑洿鎺ョ敤 @鍙橀噺鍚?浣跨敤灏卞彲浠ャ€傚叾浣滅敤鍩熶负褰撳墠杩炴帴

璧嬪€?set @var_name = expr [, @var_name = expr] ... ;

set @var_name := expr [, @var_name := expr] ... ;

select @var_name := expr [, @var_name := expr] ... ;

select 瀛楁鍚?into @var_name from 琛ㄥ悕;

浣跨敤 select @varname;

娉ㄦ剰锛氱敤鎴峰畾涔夌殑鍙橀噺鏃犻渶瀵瑰叾杩涜澹版槑鎴栧垵濮嬪寲锛屽彧涓嶈繃鑾峰彇鍒扮殑鍊间负null 绀轰緥 # 涓虹敤鎴峰畾涔夌殑鍙橀噺杩涜璧嬪€?/p>

-- 鏂瑰紡涓€

set @my_name = 'ghp1';

-- 鏂瑰紡浜岋紙鎺ㄨ崘浣跨敤锛屽洜涓?鍦∕ysql涓湁璧嬪€肩殑鎰忔€濓紝浠ョず鍖哄垎锛?/p>

set @my_name := 'ghp2';

-- 鏂瑰紡涓?/p>

select count(*) into @my_count from tb_user;

-- 鎵归噺璧嬪€?/p>

set @my_gender = '鐢?, @my_age = 18;

# 鏌ョ湅鐢ㄦ埛鑷畾涔夌殑鍙橀噺

select @my_name, @my_gender, @my_age, @my_count;

澶囨敞锛氬亣濡傛垜浠煡鐪嬫垜浠病鏈夊畾涔夌殑鍙橀噺锛孧ysql鏄笉浼氭姤閿欑殑锛岃€屾槸鏄剧ず涓簄ull 灞€閮ㄥ彉閲忥細灞€閮ㄥ彉閲?鏄牴鎹渶瑕佸畾涔夌殑鍦ㄥ眬閮ㄧ敓鏁堢殑鍙橀噺锛岃闂箣鍓嶏紝闇€瑕丏eclare澹版槑銆傚彲鐢ㄤ綔瀛樺偍杩囩▼鍐呯殑灞€閮ㄥ彉閲忓拰杈撳叆鍙傛暟锛屽眬閮ㄥ彉閲忕殑鑼冨洿鏄湪鍏跺唴澹版槑鐨凚egin ... end鍧?/p>

澹版槑 declare 鍙橀噺鍚?鍙橀噺绫诲瀷 [default ... ] ;

澶囨敞锛氬彉閲忕被鍨嬪氨鏄暟鎹簱瀛楁绫诲瀷锛欼nt銆丅igint銆丆har銆乂archar銆丏ate銆乀ime绛? 璧嬪€?set 鍙橀噺鍚?= 鍊?;

set 鍙橀噺鍚?:= 鍊?;

select 瀛楁鍚?into 鍙橀噺鍚?from 琛ㄥ悕 ... ;

绀轰緥 # 瀹氫箟涓€涓瓨鍌ㄨ繃绋嬶紝鍦╞egin end鍧椾腑瀹氫箟涓€涓悕涓簊tu_count鐨勫眬閮ㄥ彉閲?/p>

create procedure p1()

begin

-- 澹版槑涓€涓眬閮ㄥ彉閲?/p>

declare stu_count int default 0;

-- set stu_count := 1; -- 鏂瑰紡涓€锛岀洿鎺ヨ祴鍊?/p>

select count(*) into stu_count from student; -- 鏂瑰紡浜岋紝into璧嬪€?/p>

select stu_count;

end;

# 璋冪敤瀛樺偍杩囩▼

call p1();

4.2.4 if

if锛?鐢ㄤ簬鍋氭潯浠跺垽鏂?if 鏉′欢1 then

.....

elseif 鏉′欢2 then -- 鍙€?/p>

.....

else -- 鍙€?/p>

.....

end if;

绀轰緥锛?/p>

鏍规嵁瀹氫箟鐨勫垎鏁皊core鍙橀噺锛屽垽瀹氬綋鍓嶅垎鏁板搴旂殑鍒嗘暟绛夌骇锛?/p>

score<0 鎴栬€?score>100锛屽垎鏁伴潪娉? score >= 85鍒嗭紝绛夌骇涓轰紭绉€ score >= 60鍒?涓?score < 85鍒嗭紝绛夌骇涓哄強鏍? score < 60鍒嗭紝绛夌骇涓轰笉鍙婃牸

drop procedure if exists p1;

create procedure p1()

begin

declare score int default 99;

declare result varchar(10);

if score < 0 || score > 100 then

set result := '鍒嗘暟闈炴硶锛?;

elseif score < 60 then

set result := '鍒嗘暟涓嶅悎鏍硷紒';

elseif score >= 60 && score < 85 then

set result := '鍒嗘暟鍚堟牸';

else

set result := '浼樼';

end if;

select score, result;

end;

call p1();

鍙傛暟

涓婅堪鐨勯渶姹傛垜浠櫧鐒跺凡缁忓疄鐜颁簡锛屼絾鏄篃瀛樺湪涓€浜涢棶棰橈紝姣斿锛歴core 鍒嗘暟鎴戜滑鏄湪瀛樺偍杩囩▼涓畾涔夋鐨勶紝鑰屼笖鏈€缁堣绠楀嚭鏉ョ殑鍒嗘暟绛夌骇锛屾垜浠篃浠呬粎鏄渶缁堟煡璇㈠睍绀哄嚭鏉ヨ€屽凡銆傞偅涔堟垜浠兘涓嶈兘锛屾妸score鍒嗘暟鍔ㄦ€佺殑浼犻€掕繘鏉ワ紝璁$畻鍑烘潵鐨勫垎鏁扮瓑绾ф槸鍚﹀彲浠ヤ綔涓鸿繑鍥炲€艰繑鍥炲憿锛熺瓟妗堟槸鑲畾鐨勶紝鎴戜滑鍙互閫氳繃鎺ヤ笅鏉ユ墍璁茶в鐨?鍙傛暟 鏉ヨв鍐充笂杩扮殑闂銆?/p>

鍙傛暟鐨勭被鍨? 鐢ㄦ硶 create procedure 瀛樺偍杩囩▼鍚嶇о ([ in/out/inout 鍙傛暟鍚?鍙傛暟绫诲瀷 ])

begin

-- sql璇彞

end ;

绀轰緥 绀轰緥涓€锛歩n銆乷ut鍙傛暟鐨勬紨绀?/p>

鍔ㄦ€佷紶鍏ュ垎鏁帮紝鐒跺悗鐩存帴灏嗙粨鏋滆繑鍥?/p>

drop procedure if exists p1;

create procedure p1(in score int, out result varchar(10))

begin

if score < 0 || score > 100 then

set result := '鍒嗘暟闈炴硶锛?;

elseif score < 60 then

set result := '鍒嗘暟涓嶅悎鏍硷紒';

elseif score >= 60 && score < 85 then

set result := '鍒嗘暟鍚堟牸';

else

set result := '浼樼';

end if;

-- 鏌ョ湅浼犲叆鐨勫垎鏁?/p>

select score;

end;

call p1(60, @result);

-- 鏌ョ湅杈撳嚭鐨勭粨鏋?/p>

select @result;

绀轰緥浜岋細inout鍙傛暟鐨勬紨绀?/p>

杈撳叆涓€涓垎鏁帮紝鐒跺悗璁$畻鍙婃牸绾挎槸澶氬皯

drop procedure if exists p1;

create procedure p1(inout score double)

begin

set score := score * 0.6;

end;

set @score := 710;

call p1(@score);

-- 鏌ョ湅杈撳嚭鐨勭粨鏋?/p>

select @score;

4.2.5 case

case锛氫綔鐢ㄧ被浼间簬java涓殑switch鈥?case鈥?default鈥︼紝鐢ㄤ簬娴佺▼鎺у埗锛宑ase鏈変袱绉嶈娉?璇硶涓€锛?-- 鍚箟锛?褰揷ase_value鐨勫€间负 when_value1鏃讹紝鎵цstatement_list1锛?/p>

-- 褰撳€间负 when_value2鏃讹紝鎵цstatement_list2锛?鍚﹀垯灏辨墽琛?statement_list

case case_value

when when_value1 then statement_list1

[ when when_value2 then statement_list2] ...

[ else statement_list ]

end case;

璇硶浜岋細 -- 鍚箟锛?褰撴潯浠秙earch_condition1鎴愮珛鏃讹紝鎵цstatement_list1锛?/p>

-- 褰撴潯浠秙earch_condition2鎴愮珛鏃讹紝鎵цstatement_list2锛?鍚﹀垯灏辨墽琛?statement_list

case

when search_condition1 then statement_list1

[when search_condition2 then statement_list2] ...

[else statement_list]

end case;

绀轰緥

杩欓噷杩樻槸浣跨敤鍓嶉潰if鐨勫摢涓ず渚嬶紝灏唅f else 鎹㈡垚 case

drop procedure if exists p1;

create procedure p1(in score int)

begin

declare result varchar(10);

case score

when score >= 0 and score < 60 then set result := '涓嶅悎鏍?';

when score >= 60 and score < 85 then set result := '鍚堟牸';

when score >= 85 and score <=100 then set result := '浼樼';

else set result := '闈炴硶!';

end case;

select concat('鎮ㄧ殑鍒嗘暟', score, result);

end;

call p1(2);

why?

4.2.6 while

while锛氱被浼间簬java涓殑while寰幆 -- 鍏堝垽瀹氭潯浠讹紝濡傛灉鏉′欢涓簍rue锛屽垯鎵ц閫昏緫锛屽惁鍒欙紝涓嶆墽琛岄€昏緫

while 鏉′欢 do

sql閫昏緫...

end while;

绀轰緥

璁$畻1~100鐨勫拰

drop procedure if exists p1;

create procedure p1(in n int)

begin

declare sum int default 0;

declare i int default 1;

while i <= n do -- 鏉′欢鎴愮珛灏辨墽琛屽惊鐜?/p>

set sum = sum i;

set i = i 1;

end while;

select concat('1鍒?, n, '鐨勫拰涓?, sum);

end;

call p1(100);

4.2.7 repeat

repeat锛氱被浼间簬java涓殑do while寰幆 銆傚叿浣撹娉曚负锛?-- 鍏堟墽琛屼竴娆¢€昏緫锛岀劧鍚庡垽瀹歎ntil鏉′欢鏄惁婊¤冻锛屽鏋滄弧瓒筹紝鍒欓€€鍑恒€傚鏋滀笉婊¤冻锛屽垯缁х画涓嬩竴娆″惊鐜?/p>

repeat

sql閫昏緫...

until 鏉′欢

end repeat;

绀轰緥

璁$畻1~100鐨勫拰

drop procedure if exists p1;

create procedure p1(in n int)

begin

declare sum int default 0;

declare i int default 1;

repeat

set sum := sum i;

set i := i 1;

until i > n -- 鏉′欢婊¤冻灏遍€€鍑哄惊鐜?/p>

end repeat;

select concat('1鍒?, n, '鐨勫拰涓?, sum);

end;

call p1(100);

4.2.8 loop

loop锛歀oop 瀹炵幇绠€鍗曠殑寰幆锛屽鏋滀笉鍦⊿ql閫昏緫涓鍔犻€€鍑哄惊鐜殑鏉′欢锛屽彲浠ョ敤鍏舵潵瀹炵幇绠€鍗曠殑姝诲惊鐜€?loop鍙互閰嶅悎涓€涓嬩袱涓鍙ヤ娇鐢細

leave 锛氶厤鍚堝惊鐜娇鐢紝閫€鍑哄惊鐜€侷terate锛氬繀椤荤敤鍦ㄥ惊鐜腑锛屼綔鐢ㄦ槸璺宠繃褰撳墠寰幆鍓╀笅鐨勮鍙ワ紝鐩存帴杩涘叆涓嬩竴娆″惊鐜?[begin_label:] loop

sql閫昏緫...

end loop [end_label];

# begin_label锛宔nd_label锛宭abel 鎸囩殑閮芥槸鎴戜滑鎵€鑷畾涔夌殑鏍囪

leave label; -- 閫€鍑烘寚瀹氭爣璁扮殑寰幆浣?/p>

iterate label; -- 鐩存帴杩涘叆涓嬩竴娆″惊鐜?/p>

绀轰緥 绀轰緥涓€锛?/p>

璁$畻浠?绱姞鍒皀鐨勫€硷紝n涓轰紶鍏ョ殑鍙傛暟鍊?/p>

drop procedure if exists p1;

create procedure p1(in n int)

begin

declare sum int default 0;

declare i int default 1;

s:loop -- s绫讳技浜嶫ava涓殑out:

if i > n then -- 鏉′欢鎴愮珛灏辩粨鏉熷惊鐜紝绫讳技浜嶫ava涓殑break out;

leave s;

end if;

set sum := sum i;

set i := i 1;

end loop s;

select concat('1鍒?, n, '鐨勫拰涓?, sum);

end;

call p1(100);

绀轰緥浜岋細

璁$畻浠?鍒皀涔嬮棿鐨勫伓鏁扮疮鍔犵殑鍊硷紝n涓轰紶鍏ョ殑鍙傛暟鍊?/p>

drop procedure if exists p1;

create procedure p1(in n int)

begin

declare sum int default 0;

declare i int default 1;

s:loop

if i > n then

leave s;

end if;

-- ---- 杩欐浠g爜鎶ラ敊 why?

if i%2 = 1 then

set i := i 1;

iterate s; -- 鐩稿綋浜嶫ava涓殑continue锛岃烦杩囨湰娆″惊鐜洿鎺ヨ繘琛屼笅娆″惊鐜?/p>

end if;

-- ----

set sum := sum i;

set i := i 1;

end loop s;

select concat('1鍒?, n, '鐨勫拰涓?, sum);

end;

call p1(100);

4.2.9 娓告爣

娓告爣锛圕ursor锛夋槸鐢ㄦ潵瀛樺偍鏌ヨ缁撴灉闆嗙殑鏁版嵁绫诲瀷 , 鍦ㄥ瓨鍌ㄨ繃绋嬪拰鍑芥暟涓彲浠ヤ娇鐢ㄦ父鏍囧缁撴灉闆嗚繘琛屽惊鐜殑澶勭悊銆傛父鏍囩殑浣跨敤鍖呮嫭娓告爣鐨勫0鏄庛€丱pen銆丗etch 鍜?close

浠€涔堟槸娓告爣锛?/p>

娓告爣锛圕ursor锛夋槸涓€绉嶇敤浜庝粠缁撴灉闆嗕腑閫愯妫€绱㈡暟鎹殑鏁版嵁搴撳璞°€傛父鏍囧彲浠ヨ璁や负鏄竴涓寚鍚戠粨鏋滈泦鐨勬寚閽堬紝鍙互鍦ㄧ粨鏋滈泦涓щ鍔紝浠庤€岄€夊彇涓嶅悓鐨勮

娓告爣鑳藉共鍢涳紵

娓告爣鍦ㄦ暟鎹簱涓殑浣滅敤鏄鏌ヨ缁撴灉闆嗚繘琛岄€愯澶勭悊鍜屾搷浣溿€備娇鐢ㄦ父鏍囧彲浠ヤ娇鏌ヨ缁撴灉闆嗘洿鐏垫椿鍜岀簿纭湴鎺у埗锛屽苟閫愯澶勭悊缁撴灉闆嗚€屼笉蹇呭皢鏁翠釜缁撴灉闆嗗姞杞藉埌鍐呭瓨涓€?/p>

娓告爣鐨勪娇鐢ㄥ満鏅湁鍝簺锛?/p>

閫愯澶勭悊缁撴灉闆嗭細浣跨敤娓告爣鍙互瀵圭粨鏋滈泦閫愯杩涜澶勭悊锛岃繖鍦ㄩ渶瑕佹寜鐓ф煇绉嶇壒瀹氶『搴忔垨闇€瑕佸姣忎竴琛岃繘琛岀壒瀹氭搷浣滄椂闈炲父鏈夌敤銆傛墽琛屽鏉傜殑鏁版嵁澶勭悊浠诲姟锛氭父鏍囧彲浠ュ府鍔╁鐞嗕竴浜涘鏉傜殑鏁版嵁澶勭悊浠诲姟锛屼緥濡傝绠楀钩鍧囧€笺€佹€诲拰绛夈€傞€氳繃娓告爣锛屽彲浠ラ€愯鍒嗘瀽鏁版嵁骞惰繘琛屼竴浜涚壒瀹氱殑璁$畻銆傚鐞嗗ぇ閲忔暟鎹細褰撻渶瑕佸鐞嗗ぇ閲忔暟鎹椂锛屼娇鐢ㄦ父鏍囧彲浠ヤ娇澶勭悊鏇村姞楂樻晥銆傜浉姣斾簬涓€娆℃€у皢鏁翠釜缁撴灉闆嗗姞杞藉埌鍐呭瓨涓鐞嗭紝娓告爣鍙互閫愯澶勭悊缁撴灉闆嗭紝鍑忓皯鍐呭瓨鐨勪娇鐢? 澹版槑娓告爣 declare 娓告爣鍚嶇о cursor for 鏌ヨ璇彞

鎵撳紑娓告爣 open 娓告爣鍚嶇о

鑾峰彇娓告爣璁板綍 fetch 娓告爣鍚嶇о into 鍙橀噺 [ 鍙橀噺 ]

鍏抽棴娓告爣 close 娓告爣鍚嶇о

绀轰緥

绀轰緥涓€锛氫娇鐢ㄨ嚜瀹氫箟鐨勫眬閮ㄥ彉閲忔帴鏀跺琛屽鍒楁暟鎹?/p>

鍦ㄤ箣鍓嶆垜浠湪瀛樺偍杩囩▼涓紝瀹氫箟鐨勫眬閮ㄥ彉閲忓彧鑳芥帴鏀跺崟琛屽崟鍒楁暟鎹紝瀵逛簬澶氳澶氬垪鏁版嵁浣跨敤鑷畾涔夌殑灞€閮ㄥ彉閲忓幓鎺ユ敹浼氱洿鎺ユ姤閿欙紒

drop procedure if exists p1;

create procedure p1()

begin

declare stu_count int default 0;

select * into stu_count from student;

select stu_count;

end;

call p1();

绀轰緥浜岋細

鏍规嵁浼犲叆鐨勫弬鏁皍age锛屾潵鏌ヨ鐢ㄦ埛琛╰b_user涓紝鎵€鏈夌殑鐢ㄦ埛骞撮緞灏忎簬绛変簬uage鐨勭敤鎴峰鍚嶏紙name锛夊拰涓撲笟锛坧rofession锛夛紝骞跺皢鐢ㄦ埛鐨勫鍚嶅拰涓撲笟鎻掑叆鍒版墍鍒涘缓鐨勪竴寮犳柊琛?id,name,profession)涓?/p>

drop procedure if exists p1;

create procedure p1(in u_age int)

begin

declare u_name varchar(100);

declare u_pro varchar(100); -- 杩欎袱涓眬閮ㄥ彉閲忕殑澹版槑涓€瀹氳鍦ㄦ父鏍囧0鏄庝箣鍓?/p>

-- 鐢虫槑娓告爣锛屾煡璇㈢粨鏋滈泦锛岀劧鍚庡瓨鍌ㄥ埌娓告爣涓?/p>

declare u_cursor cursor for select name,profession from tb_user where age <= u_age;

-- 鍒涘缓琛ㄧ粨鏋?/p>

drop table if exists tb_user_pro; -- 濡傛灉璇ヨ〃宸茬粡瀛樺湪灏辩洿鎺ュ垹闄?/p>

create table if not exists tb_user_pro(

id int primary key auto_increment,

name varchar(100),

profession varchar(100)

);

-- 寮€鍚父鏍?/p>

open u_cursor;

-- 灏嗘父鏍囦腑鐨勬暟鎹彃鍏b_user_pro琛ㄤ腑

while true do

fetch u_cursor into u_name, u_pro;

insert into tb_user_pro values(null, u_name, u_pro);

end while;

-- 鍏抽棴娓告爣

close u_cursor;

end;

call p1(30);

娉ㄦ剰锛氭父鏍囩殑澹版槑涓€瀹氳鍦ㄥ眬閮ㄥ彉閲忎箣鍚庯紝鍚﹀垯鎶ラ敊variable or condition declaration after cursor or handler declaration

鍙互鐪嬪埌涓婇潰鐨凷ql鎵ц鎴愬姛浜嗭紝浣嗘槸鐢变簬while鏄寰幆锛屾墍浠ュ綋鎵€鏈夊ぇ瀹惰褰曡閬嶅巻瀹屽悗锛屼細鐩存帴鎶ラ敊no data - zero rows fetched, selected, or processed銆傝в鍐虫柟娉曪紝闇€瑕佷娇鐢ㄦ潯浠跺鐞嗙▼搴?/p>

鏉′欢澶勭悊绋嬪簭锛氭潯浠跺鐞嗙▼搴忥紙handler锛夊彲浠ョ敤鏉ュ畾涔夊湪娴佺▼鎺у埗缁撴瀯鎵ц杩囩▼涓亣鍒伴棶棰樻椂鐩稿簲鐨勫鐞嗘楠ゃ€?鍏蜂綋璇硶涓猴細

璇︽儏璇峰弬鑰僊ysql瀹樻柟鏂囨。

declare handler_action handler for condition_value [, condition_value]

... statement ;

handler_action 鐨勫彇鍊硷細

continue: 缁х画鎵ц褰撳墠绋嬪簭

exit: 缁堟鎵ц褰撳墠绋嬪簭

condition_value 鐨勫彇鍊硷細

sqlstate sqlstate_value: 鐘舵€佺爜锛屽 02000

sqlwarning: 鎵€鏈変互01寮€澶寸殑sqlstate浠g爜鐨勭畝鍐?/p>

not found: 鎵€鏈変互02寮€澶寸殑sqlstate浠g爜鐨勭畝鍐?/p>

sqlexception: 鎵€鏈夋病鏈夎sqlwarning 鎴?not found鎹曡幏鐨凷qlstate浠g爜鐨勭畝鍐?/p>

鐜板湪浣跨敤鏉′欢澶勭悊绋嬪簭瑙e喅涓婇潰 while 姝诲惊鐜殑bug锛?drop procedure if exists p1;

create procedure p1(in u_age int)

begin

declare u_name varchar(100);

declare u_pro varchar(100);

-- 鐢虫槑娓告爣锛屾煡璇㈢粨鏋滈泦锛岀劧鍚庡瓨鍌ㄥ埌娓告爣涓?/p>

declare u_cursor cursor for select name,profession from tb_user where age <= u_age;

-- 澹版槑鏉′欢澶勭悊绋嬪簭锛堝叧闂父鏍囷紝鐒跺悗閫€鍑猴紝02000琛ㄧずsql鐘舵€佺爜锛屾剰鎬濇槸閫€鍑猴級

declare exit handler for sqlstate '02000' close u_cursor;

-- declare exit handler for sqlstate not fount close u_cursor; -- 鍐欐硶浜?/p>

-- 鍒涘缓琛ㄧ粨鏋?/p>

drop table if exists tb_user_pro; -- 濡傛灉璇ヨ〃宸茬粡瀛樺湪灏辩洿鎺ュ垹闄?/p>

create table if not exists tb_user_pro(

id int primary key auto_increment,

name varchar(100),

profession varchar(100)

);

-- 寮€鍚父鏍?/p>

open u_cursor;

-- 灏嗘父鏍囦腑鐨勬暟鎹彃鍏b_user_pro琛ㄤ腑

while true do

fetch u_cursor into u_name, u_pro;

insert into tb_user_pro values(null, u_name, u_pro);

end while;

-- 鍏抽棴娓告爣

close u_cursor;

end;

call p1(30);

澶囨敞锛氶€氳繃sqlstate鎸囧畾鍏蜂綋鐨勭姸鎬佺爜锛岄€氳繃sqlstate鐨勪唬鐮佺畝鍐欐柟寮?not found锛?2 寮€澶寸殑鐘舵€佺爜锛屼唬鐮佺畝鍐欎负 not found

4.3 瀛樺偍鍑芥暟

瀛樺偍鍑芥暟鏄粈涔堬紵

鍦∕ysql涓紝瀛樺偍鍑芥暟锛團unction锛夋槸涓€娈佃灏佽鍦ㄥ嚱鏁颁綋鍐呯殑sql璇彞锛屽彲浠ュ湪闇€瑕佹椂琚皟鐢ㄣ€傚瓨鍌ㄥ嚱鏁板彲浠ユ帴鍙楄緭鍏ュ弬鏁帮紝骞朵笖鍙互杩斿洖涓€涓€兼垨涓€涓粨鏋滈泦

瀛樺偍鍑芥暟鐨勪綔鐢ㄦ槸浠€涔堬紵

閲嶇敤sql浠g爜锛氬瓨鍌ㄥ嚱鏁板彲浠ュ皝瑁呬竴娈靛父鐢ㄧ殑sql浠g爜锛屼娇鍏跺彲浠ュ湪闇€瑕佹椂琚弽澶嶈皟鐢紝浠庤€岄伩鍏嶄簡閲嶅缂栧啓鐩稿悓鐨勪唬鐮佹彁楂樻€ц兘锛氬瓨鍌ㄥ嚱鏁板彲浠ラ€氳繃鍑忓皯鏁版嵁浼犺緭鍜屽噺灏戠綉缁滃欢杩熺瓑鏂瑰紡锛屾彁楂楽ql璇彞鐨勬墽琛屾晥鐜囷紝浠庤€屾彁楂樻暟鎹簱鐨勬€ц兘鎻愰珮瀹夊叏鎬э細瀛樺偍鍑芥暟鍙互浣跨敤瀛樺偍杩囩▼璇█鎻愪緵鐨勫畨鍏ㄦ満鍒讹紝渚嬪鍙橀噺澹版槑銆佹潯浠惰鍙ャ€佸惊鐜鍙ョ瓑锛屼粠鑰屾彁楂樻暟鎹簱鐨勫畨鍏ㄦ€х畝鍖栧鏉傜殑鏁版嵁澶勭悊锛氬瓨鍌ㄥ嚱鏁板彲浠ュ府鍔╁鐞嗕竴浜涘鏉傜殑鏁版嵁澶勭悊浠诲姟锛屼緥濡傝绠楀钩鍧囧€笺€佹€诲拰绛夈€傞€氳繃瀛樺偍鍑芥暟锛屽彲浠ュ皢澶嶆潅鐨勫鐞嗚繃绋嬪皝瑁呭湪涓€涓嚱鏁颁腑锛屼粠鑰岀畝鍖朣ql浠g爜鍜屾暟鎹鐞嗚繃绋?/p>

瀛樺偍鍑芥暟鐨勫熀鏈娉曪細

create function 瀛樺偍鍑芥暟鍚嶇о ([ 鍙傛暟鍒楄〃 ])

returns type [characteristic ...]

begin

-- sql璇彞

return ...;

end ;

characteristic璇存槑锛?

deterministic锛氱浉鍚岀殑杈撳叆鍙傛暟鎬绘槸浜х敓鐩稿悓鐨勭粨鏋淣0sql锛氫笉鍖呭惈sql璇彞reads sol data锛氬寘鍚鍙栨暟鎹殑璇彞锛屼絾涓嶅寘鍚啓鍏ユ暟鎹殑璇彞

绀轰緥

璁$畻浠?绱姞鍒皀鐨勫€硷紝n涓轰紶鍏ョ殑鍙傛暟鍊?/p>

-- 鍒涘缓瀛樺偍鍑芥暟

create function fun1(n int)

-- 澹版槑瀛樺偍鍑芥暟鐨勮繑鍥炵被鍨?/p>

returns int deterministic

begin

declare total int default 0;

-- 璁$畻1~n鐨勫拰

while n > 0 do

set total := total n;

set n := n - 1;

end while;

return total;

end;

select fun1(100);

娉ㄦ剰锛歁ysql8.x浜岃繘鍒舵棩蹇楅粯璁ゆ槸寮€鍚殑锛孊inlog浜岃繘鍒舵棩蹇楀紑鍚垯蹇呴』娣诲姞charracteristic鍙傛暟

4.4 瑙﹀彂鍣?/p>

4.4.1 瑙﹀彂鍣ㄦ杩?/p>

浠€涔堟槸瑙﹀彂鍣紵

瑙﹀彂鍣紙trigger锛夋槸涓€绉嶇敤浜庡湪鐗瑰畾琛ㄤ笂鎵ц鐨勭壒娈婄被鍨嬬殑瀛樺偍杩囩▼銆傚畠鏄敱鏁版嵁搴撳畾涔夊苟鑷姩鎵ц鐨勶紝涓嶉渶瑕佹墜鍔ㄨ皟鐢ㄣ€傝е鍙戝櫒閫氬父鐢ㄤ簬鍦ㄦ暟鎹簱涓畾涔変竴浜涜嚜鍔ㄥ寲鎿嶄綔锛屼緥濡傚湪鐗瑰畾琛ㄤ笂鎻掑叆銆佹洿鏂版垨鍒犻櫎鏁版嵁鏃惰е鍙戞煇浜涙搷浣溿€傚綋婊¤冻鐗瑰畾鐨勬潯浠舵椂锛岃е鍙戝櫒浼氳嚜鍔ㄦ墽琛屼竴浜汼ql璇彞銆?/p>

瑙﹀彂鍣ㄧ殑缁勬垚

浜嬩欢绫诲瀷锛氳е鍙戝櫒鍙湪鐗瑰畾琛ㄤ笂鐨処nsert銆乁pdate鎴朌elete鎿嶄綔涓е鍙戞墽琛屻€傝е鍙戞椂鏈猴細瑙﹀彂鍣ㄥ彲鍦ㄤ簨浠跺彂鐢熶箣鍓嶏紙before锛夋垨涔嬪悗锛圓fter锛夎е鍙戞墽琛屻€傝е鍙戝櫒涓讳綋锛氳е鍙戝櫒鐨勪富浣撳寘鍚墽琛岀殑sql璇彞锛屽彲浠ユ槸涓€鏉℃垨澶氭潯sql璇彞銆? 瑙﹀彂鍣ㄧ殑浣滅敤锛?/p>

鏁版嵁绾︽潫锛氳е鍙戝櫒鍙互鐢ㄤ簬瀹炵幇鏁版嵁绾︽潫锛屼緥濡傚湪鎻掑叆銆佹洿鏂版垨鍒犻櫎鏁版嵁鏃跺鏁版嵁杩涜楠岃瘉锛屼互纭繚鏁版嵁鐨勫畬鏁存€у拰姝g‘鎬с€傛暟鎹璁★細瑙﹀彂鍣ㄥ彲浠ョ敤浜庤褰曟暟鎹簱鎿嶄綔鐨勫璁′俊鎭紝渚嬪璁板綍鐢ㄦ埛鍦ㄤ綍鏃跺鏁版嵁搴撴墽琛屼簡鍝簺鎿嶄綔銆傛暟鎹慨鏀癸細瑙﹀彂鍣ㄥ彲浠ョ敤浜庡湪鏁版嵁搴撲腑鑷姩淇敼鏁版嵁锛屼緥濡傚湪鎻掑叆鎴栨洿鏂版暟鎹椂锛岃嚜鍔ㄦ洿鏂扮浉鍏崇殑璁$畻缁撴灉銆傛暟鎹悓姝ワ細瑙﹀彂鍣ㄥ彲浠ョ敤浜庡湪鏁版嵁搴撲腑瀹炵幇鏁版嵁鍚屾锛屼緥濡傚湪涓€涓暟鎹簱涓殑琛ㄦ洿鏂版椂锛岃嚜鍔ㄥ皢鏇存柊鍚屾鍒板彟涓€涓暟鎹簱鐨勭浉搴旇〃涓€傛暟鎹浠斤細瑙﹀彂鍣ㄥ彲浠ョ敤浜庡湪鏁版嵁搴撲腑瀹炵幇鏁版嵁澶囦唤锛屼緥濡傚湪瀵硅〃杩涜淇敼鏃讹紝鑷姩灏嗕慨鏀规搷浣滆褰曞埌澶囦唤琛ㄤ腑锛屼互渚夸簬鏁版嵁鎭㈠銆? 瑙﹀彂鍣ㄧ殑鍒嗙被锛?/p>

鏍规嵁浜嬩欢绫诲瀷杩涜鍒嗙被锛氭牴鎹е鍙戝櫒鎵ц鐨勪簨浠剁被鍨嬶紝瑙﹀彂鍣ㄥ彲浠ュ垎涓篒nsert銆乁pdate鍜孌elete瑙﹀彂鍣ㄣ€傛牴鎹е鍙戞椂鏈哄垎绫伙細鏍规嵁瑙﹀彂鍣ㄦ墽琛岀殑鏃舵満锛岃е鍙戝櫒鍙互鍒嗕负before鍜孉fter瑙﹀彂鍣ㄣ€侭efore瑙﹀彂鍣ㄥ湪浜嬩欢鍙戠敓涔嬪墠瑙﹀彂鎵ц锛岃€孉fter瑙﹀彂鍣ㄥ湪浜嬩欢鍙戠敓涔嬪悗瑙﹀彂鎵ц銆傛牴鎹е鍙戝璞″垎绫伙細鏍规嵁瑙﹀彂鍣ㄧ殑瀵硅薄锛岃е鍙戝櫒鍙互鍒嗕负琛ㄧ骇瑙﹀彂鍣ㄥ拰琛岀骇瑙﹀彂鍣ㄣ€傝〃绾це鍙戝櫒鍦ㄦ暣涓〃涓婅е鍙戞墽琛岋紝鑰岃绾це鍙戝櫒鍦ㄨ〃鐨勬瘡涓€琛屼笂瑙﹀彂鎵ц銆傛牴鎹е鍙戞鏁板垎绫伙細鏍规嵁瑙﹀彂鍣ㄦ墽琛岀殑娆℃暟锛岃е鍙戝櫒鍙互鍒嗕负for each row鍜孎or each statement瑙﹀彂鍣ㄣ€侳or each row瑙﹀彂鍣ㄥ湪姣忎竴琛屾暟鎹笂瑙﹀彂鎵ц锛岃€孎or each statement瑙﹀彂鍣ㄥ湪姣忎竴鏉ql璇彞鎵ц涓€娆°€?瑙﹀彂鍣ㄦ槸涓庤〃鏈夊叧鐨勬暟鎹簱瀵硅薄锛屾寚鍦╥nsert/update/delete涔嬪墠(before)鎴栦箣鍚?after)锛岃е鍙戝苟鎵ц瑙﹀彂鍣ㄤ腑瀹氫箟鐨凷ql璇彞闆嗗悎銆傝е鍙戝櫒鐨勮繖绉嶇壒鎬у彲浠ュ崗鍔╁簲鐢ㄥ湪鏁版嵁搴撶纭繚鏁版嵁鐨勫畬鏁存€? 鏃ュ織璁板綍 , 鏁版嵁鏍¢獙绛夋搷浣?銆備娇鐢ㄥ埆鍚峅ld鍜孨ew鏉ュ紩鐢ㄨе鍙戝櫒涓彂鐢熷彉鍖栫殑璁板綍鍐呭锛岃繖涓庡叾浠栫殑鏁版嵁搴撴槸鐩镐技鐨勩€傜幇鍦ㄨе鍙戝櫒杩樺彧鏀寔琛岀骇瑙﹀彂锛屼笉鏀寔璇彞绾це

4.4.2 瑙﹀彂鍣ㄧ殑鍩烘湰浣跨敤

鍒涘缓 create trigger trigger_name

before/after insert/update/delete

on tbl_name for each row -- 琛岀骇瑙﹀彂鍣?/p>

begin

trigger_stmt ; -- sql璇彞

end;

鏌ョ湅 show triggers;

鍒犻櫎 drop trigger [schema_name]trigger_name; -- 濡傛灉娌℃湁鎸囧畾schema_name锛岄粯璁や负褰撳墠鏁版嵁搴?/p>

绀轰緥

绀轰緥涓€锛氭彃鍏ユ暟鎹殑瑙﹀彂鍣?/p>

閫氳繃瑙﹀彂鍣ㄨ褰?tb_user 琛ㄧ殑鏁版嵁鍙樻洿鏃ュ織锛屽皢鍙樻洿鏃ュ織鎻掑叆鍒版棩蹇楄〃user_logs涓? 鍖呭惈澧炲姞,淇敼 , 鍒犻櫎 ;

鈶犲噯澶囧伐浣滐細鍑嗗涓€寮?users_logs琛?/p>

-- 鍑嗗宸ヤ綔 : 鏃ュ織琛?user_logs

drop table if exists user_logs;

create table user_logs(

id int(11) not null auto_increment,

operation varchar(20) not null comment '鎿嶄綔绫诲瀷, insert/update/delete',

operate_time datetime not null comment '鎿嶄綔鏃堕棿',

operate_id int(11) not null comment '鎿嶄綔鐨処d',

operate_params varchar(500) comment '鎿嶄綔鍙傛暟',

primary key(`id`)

)engine=innodb default charset=utf8;

鈶$紪鍐欎竴涓е鍙戝櫒

-- 鍒犻櫎宸插瓨鍦ㄧ殑鍚屽悕瑙﹀彂鍣?/p>

drop trigger if exists tb_user_insert_trigger;

-- 鍒涘缓鎻掑叆瑙﹀彂鍣?/p>

create trigger tb_user_insert_trigger

after insert on tb_user for each row -- 鎻掑叆鎿嶄綔涔嬪悗瑙﹀彂鍣ㄦ墽琛?/p>

begin

insert into user_logs(id, operation, operate_time, operate_id, operate_params) values

(null, 'insert', now(), new.id, concat('鎻掑叆鐨勬暟鎹唴瀹逛负: id=', new.id, ', name=', new.name,

', phone=', new.phone, ', email=', new.email, ', profession=', new.profession));

end;

-- 鏌ョ湅褰撳墠鏁版嵁搴撲腑鎵€鏈夌殑瑙﹀彂鍣?/p>

show triggers;

-- 寰€tb_user琛ㄤ腑鎻掑叆涓€鏉℃暟鎹?/p>

insert into tb_user(id, name, phone, email, profession, age, gender, status,createtime)

values (26,'涓夌殗瀛?,'18809091212','erhuangzi@163.com','杞欢宸ョ▼',23,'1','1',now());

鍚慚ysql涓彃鍏ヤ簡涓€鏉℃暟鎹悗锛屾槸鑳藉鍦?users_logs 涓煡鐪嬪埌鐩稿叧鎿嶄綔鐨?/p>

绀轰緥浜岋細淇敼鏁版嵁鐨勮е鍙戝櫒

-- 鍒犻櫎宸插瓨鍦ㄧ殑鍚屽悕瑙﹀彂鍣?/p>

drop trigger if exists tb_user_update_trigger;

-- 鍒涘缓淇敼瑙﹀彂鍣?/p>

create trigger tb_user_update_trigger

after update on tb_user for each row -- 鏇存柊鎿嶄綔涔嬪悗瑙﹀彂鍣ㄦ墽琛?/p>

begin

insert into user_logs(id, operation, operate_time, operate_id, operate_params) values

(null, 'update', now(), old.id, concat('鏇存柊涔嬪墠鐨勬暟鎹唴瀹逛负: id=', old.id, ', name=', old.name,

', phone=', old.phone, ', email=', old.email, ', profession=', old.profession,

' | 鏇存柊涔嬪悗鐨勬暟鎹唴瀹逛负: id=', new.id, ', name=', new.name,

', phone=', new.phone, ', email=', new.email, ', profession=', new.profession));

end;

-- 鏌ョ湅褰撳墠鏁版嵁搴撲腑鎵€鏈夌殑瑙﹀彂鍣?/p>

show triggers;

-- 淇敼tb_user琛ㄤ腑鐨勪竴鏉℃暟鎹?/p>

update tb_user set profession = '浼氳' where id = 2;

update tb_user set profession = '浼氳' where id < 5; -- 姝ql璇彞锛屼細瀵艰嚧瑙﹀彂鍣ㄨ瑙﹀彂5娆★紝鍥犱负褰撳墠瑙﹀彂鍣ㄦ槸琛岀骇瑙﹀彂鍣?/p>

绀轰緥涓夛細鍒犻櫎鏁版嵁鐨勮е鍙戝櫒

-- 鍒犻櫎宸插瓨鍦ㄧ殑鍚屽悕瑙﹀彂鍣?/p>

drop trigger if exists tb_user_delete_trigger;

-- 鍒涘缓淇敼瑙﹀彂鍣?/p>

create trigger tb_user_delete_trigger

after delete on tb_user for each row

begin

insert into user_logs(id, operation, operate_time, operate_id, operate_params) values

(null, 'delete', now(), old.id, concat('鍒犻櫎鐨勬暟鎹唴瀹逛负: id=', old.id, ', name=', old.name,

', phone=', old.phone, ', email=', old.email, ', profession=', old.profession));

end;

-- 鏌ョ湅褰撳墠鏁版嵁搴撲腑鎵€鏈夌殑瑙﹀彂鍣?/p>

show triggers;

-- 淇敼tb_user琛ㄤ腑鐨勪竴鏉℃暟鎹?/p>

delete from tb_user where id = 26;

5銆侀攣

5.1 閿佹杩?/p>

浠€涔堟槸閿侊紵

閿佹槸璁$畻鏈哄崗璋冨涓繘绋嬫垨绾跨▼骞跺彂璁块棶鏌愪竴璧勬簮鐨勬満鍒躲€傚湪鏁版嵁搴撲腑锛岄櫎浼犵粺鐨勮绠楄祫婧愶紙cpu銆丷am銆両/o锛夌殑浜夌敤浠ュ锛屾暟鎹篃鏄竴绉嶄緵璁稿鐢ㄦ埛鍏变韩鐨勮祫婧愩€傚浣曚繚璇佹暟鎹苟鍙戣闂殑涓€鑷存€с€佹湁鏁堟€ф槸鎵€鏈夋暟鎹簱蹇呴』瑙e喅鐨勪竴涓棶棰橈紝閿佸啿绐佷篃鏄奖鍝嶆暟鎹簱骞跺彂璁块棶鎬ц兘鐨勪竴涓噸瑕佸洜绱犮€備粠杩欎釜瑙掑害鏉ヨ锛岄攣瀵规暟鎹簱鑰岃█鏄惧緱灏ゅ叾閲嶈锛屼篃鏇村姞澶嶆潅銆?/p>

绠€鑰岃█涔嬶細閿侊紙lock锛夋槸涓€绉嶇敤浜庢帶鍒跺苟鍙戣闂殑鏈哄埗銆傚綋澶氫釜鐢ㄦ埛鍚屾椂璁块棶鍚屼竴鏁版嵁鏃讹紝閿佸彲浠ヤ繚璇佹暟鎹殑涓€鑷存€у拰瀹屾暣鎬с€?/p>

閿佺殑浣滅敤锛?/p>

骞跺彂鎺у埗锛氶攣鍙互鐢ㄤ簬鎺у埗骞跺彂璁块棶锛岄伩鍏嶆暟鎹啿绐併€傚綋澶氫釜鐢ㄦ埛鍚屾椂璁块棶鍚屼竴鏁版嵁鏃讹紝閿佸彲浠ヤ繚璇佹暟鎹殑涓€鑷存€у拰瀹屾暣鎬с€傛暟鎹竴鑷存€э細閿佸彲浠ョ敤浜庝繚璇佹暟鎹殑涓€鑷存€с€傚綋涓€涓敤鎴峰鏁版嵁杩涜淇敼鏃讹紝鍏朵粬鐢ㄦ埛鏃犳硶瀵规暟鎹繘琛岃鍙栨垨淇敼鎿嶄綔锛岀洿鍒板綋鍓嶇敤鎴峰畬鎴愭搷浣滃苟閲婃斁閿佷负姝€傛暟鎹畨鍏ㄦ€э細閿佸彲浠ョ敤浜庝繚璇佹暟鎹殑瀹夊叏鎬с€傚綋涓€涓敤鎴峰鏁版嵁杩涜淇敼鏃讹紝鍏朵粬鐢ㄦ埛鏃犳硶瀵规暟鎹繘琛岃鍙栨垨淇敼鎿嶄綔锛屼互淇濊瘉鏁版嵁涓嶈鎭舵剰淇敼鎴栧垹闄ゃ€傛暟鎹彲闈犳€э細閿佸彲浠ョ敤浜庝繚璇佹暟鎹殑鍙潬鎬с€傚綋涓€涓敤鎴峰鏁版嵁杩涜淇敼鏃讹紝鍏朵粬鐢ㄦ埛鏃犳硶瀵规暟鎹繘琛岃鍙栨垨淇敼鎿嶄綔锛屼互閬垮厤鏁版嵁琚悓鏃朵慨鏀瑰鑷存暟鎹崯鍧忔垨涓㈠け銆? 閿佺殑鍒嗙被锛?/p>

鎸夌収閿佺殑绮掑害锛氶攣鍙互鍒嗕负琛ㄧ骇閿佸拰琛岀骇閿併€傝〃绾ч攣鍙互閿佸畾鏁翠釜琛紝浠ユ帶鍒跺琛ㄧ殑骞跺彂璁块棶锛涜绾ч攣鍙互閿佸畾琛ㄤ腑鐨勫崟涓锛屼互鎺у埗瀵硅鐨勫苟鍙戣闂€傛寜鐓ч攣鐨勭被鍨嬶細閿佸彲浠ュ垎涓哄叡浜攣鍜屾帓浠栭攣銆傚叡浜攣鍏佽澶氫釜鐢ㄦ埛鍚屾椂璇诲彇鍚屼竴鏁版嵁锛屼絾涓嶅厑璁镐换浣曠敤鎴峰鏁版嵁杩涜淇敼鎿嶄綔锛涙帓浠栭攣鍙厑璁镐竴涓敤鎴峰鏁版嵁杩涜淇敼鎿嶄綔锛屽叾浠栫敤鎴锋棤娉曞鏁版嵁杩涜璇诲彇鎴栦慨鏀规搷浣溿€傛寜鐓ч攣鐨勭矑搴﹀拰绫诲瀷鐨勭粍鍚堬細閿佸彲浠ユ牴鎹矑搴﹀拰绫诲瀷鐨勭粍鍚堣繘琛屽垎绫伙紝鍙互鍒嗕负鍏ㄥ眬閿併€佽〃绾ч攣鍜岃绾ч攣銆傚叏灞€閿侀攣瀹氭暟鎹簱涓殑鎵€鏈夎〃锛岃〃绾ч攣姣忔鎿嶄綔閿佷綇鏁村紶琛紝琛岀骇閿佹瘡娆℃搷浣滈攣浣忓搴旂殑琛屾暟鎹€? 閿佺殑浣跨敤鍦烘櫙锛?/p>

琛ㄧ骇鍏变韩閿侊細閫傜敤浜庡涓敤鎴峰悓鏃惰鍙栧悓涓€鏁版嵁鐨勫満鏅紝淇濊瘉鏁版嵁鐨勪竴鑷存€у拰瀹屾暣鎬с€備緥濡傦紝鍦ㄥ浠芥垨澶嶅埗鏁版嵁搴撴椂锛岄渶瑕佸鏁翠釜琛ㄨ繘琛屽叡浜攣瀹氥€傝〃绾ф帓浠栭攣锛氶€傜敤浜庨渶瑕佸鏁翠釜琛ㄨ繘琛屼慨鏀规搷浣滅殑鍦烘櫙锛屼繚璇佹暟鎹殑瀹夊叏鎬у拰瀹屾暣鎬с€備緥濡傦紝鍦ㄥ鏁翠釜琛ㄨ繘琛岀粨鏋勫彉鏇存垨鏁版嵁娓呯悊鏃讹紝闇€瑕佸琛ㄨ繘琛屾帓浠栭攣瀹氥€傝绾у叡浜攣锛氶€傜敤浜庡涓敤鎴峰悓鏃惰鍙栧悓涓€鏁版嵁鐨勫満鏅紝浣嗕笉鍏佽浠讳綍鐢ㄦ埛瀵规暟鎹繘琛屼慨鏀规搷浣溿€備緥濡傦紝鍦ㄦ煡璇㈡搷浣滀腑锛岄渶瑕佸琛岃繘琛屽叡浜攣瀹氥€傝绾ф帓浠栭攣锛氶€傜敤浜庨渶瑕佸鍗曚釜琛岃繘琛屼慨鏀规搷浣滅殑鍦烘櫙锛屼繚璇佹暟鎹殑瀹夊叏鎬у拰瀹屾暣鎬с€備緥濡傦紝鍦ㄥ鍗曚釜琛岃繘琛屾洿鏂般€佸垹闄ゆ垨鎻掑叆鎿嶄綔鏃讹紝闇€瑕佸琛岃繘琛屾帓浠栭攣瀹氥€?

5.2 鍏ㄥ眬閿?/p>

鍏ㄥ眬閿佸氨鏄鏁翠釜鏁版嵁搴撳疄渚嬪姞閿侊紝鍔犻攣鍚庢暣涓疄渚嬪氨澶勪簬鍙鐘舵€侊紝鍚庣画鐨凞ml鐨勫啓璇彞锛孌dl璇彞锛屽凡缁忔洿鏂版搷浣滅殑浜嬪姟鎻愪氦璇彞閮藉皢琚樆濉炪€傚叾鍏稿瀷鐨勪娇鐢ㄥ満鏅槸鍋氬叏搴撶殑閫昏緫澶囦唤锛屽鎵€鏈夌殑琛ㄨ繘琛岄攣瀹氾紝浠庤€岃幏鍙栦竴鑷存€ц鍥撅紝淇濊瘉鏁版嵁鐨勫畬鏁存€?/p>

浠€涔堟槸閫昏緫澶囦唤锛?/p>

閫昏緫澶囦唤鏄寚灏嗘暟鎹簱涓殑鏁版嵁閫氳繃sql璇彞绛夐€昏緫鏂瑰紡杩涜澶囦唤鐨勬搷浣溿€傞€昏緫澶囦唤閫氬父鐢熸垚涓€涓寘鍚暟鎹簱涓墍鏈夋暟鎹殑鑴氭湰鏂囦欢锛岃鏂囦欢鍖呭惈涓€绯诲垪sql璇彞锛屽彲浠ョ敤浜庢仮澶嶆暟鎹簱涓殑鏁版嵁銆?/p>

閫昏緫澶囦唤鐨勪紭缂虹偣

浼樼偣锛氱伒娲绘€у拰鍙鎬ц緝寮猴紝澶囦唤鏂囦欢鍙互浜哄伐鏌ョ湅鍜岀紪杈戙€傚畠閫傜敤浜庡皬鍨嬫暟鎹簱鎴栭渶瑕佸鏁版嵁搴撹繘琛岄儴鍒嗗浠界殑鎯呭喌缂虹偣锛氬浠藉拰杩樺師鏃堕棿杈冮暱锛屽洜涓洪渶瑕佹墽琛屽ぇ閲忕殑sql璇彞銆傚悓鏃讹紝澶囦唤鏂囦欢鐨勫ぇ灏忎篃姣旂墿鐞嗗浠芥枃浠惰澶э紝鍥犱负澶囦唤鏂囦欢涓寘鍚簡sql璇彞绛夊厓鏁版嵁淇℃伅 涓轰粈涔堝叏搴撻€昏緫澶囦唤闇€瑕佸姞鍏ㄥ眬閿佸憿锛?/p>

鍏ㄥ簱閫昏緫澶囦唤闇€瑕佸姞鍏ㄥ眬閿佹槸鍥犱负鍦ㄥ浠借繃绋嬩腑锛屽浠藉伐鍏烽渶瑕佽鍙栨暣涓暟鎹簱鐨勬暟鎹苟鐢熸垚澶囦唤鏂囦欢锛岃€屽湪姝ゆ湡闂村鏋滄湁鍏朵粬鐢ㄦ埛瀵规暟鎹簱杩涜淇敼鎿嶄綔锛屽彲鑳戒細瀵艰嚧澶囦唤鏁版嵁鐨勪笉涓€鑷存€э紝浠庤€屽奖鍝嶅浠界殑鍙潬鎬у拰瀹屾暣鎬с€傚洜姝わ紝涓轰簡淇濊瘉澶囦唤鏁版嵁鐨勪竴鑷存€э紝闇€瑕佸湪澶囦唤杩囩▼涓鏁翠釜鏁版嵁搴撳姞鍏ㄥ眬閿侊紝浠ラ槻姝㈠叾浠栫敤鎴峰鏁版嵁搴撹繘琛屼慨鏀规搷浣溿€?/p>

鍔犲叏灞€閿?flush tables with read lock ;

鏁版嵁澶囦唤 mysqldump -uroot -p32345678 mysql_study > mysql_study.sql

閲婃斁閿?unlock tables;

绀轰緥

鈶犲湪绐楀彛涓€绉嶏紝缁檓ysql_study鍔犱笂鍏ㄥ眬閿侊細

鈶$幇鍦ㄧ獥鍙d簩锛屽彧鑳借繘琛岃鎿嶄綔锛屼笉鑳借繘琛屽啓鎿嶄綔

鈶㈠湪绐楀彛浜岀閲婃斁鍏ㄥ眬閿侊紝鐒跺悗杩涜鍐欐搷浣?/p>

鈶e湪绐楀彛涓€绉嶉噴鏀惧叏灞€閿侊紝杩欐牱灏辫兘鍦ㄧ獥鍙d簩涓兘杩涜鍐欐搷浣滀簡

鈶ら€昏緫澶囦唤锛屾敞鎰忛€昏緫澶囦唤鐨勮鍙ヤ笉绠桽ql璇彞锛屾墍浠ラ渶瑕佺洿鎺ュ湪鍛戒护琛岃繍琛?/p>

mysqldump -h浣犵殑涓绘満ip -u浣犵殑mysql鐢ㄦ埛鍚?-p浣犵殑鏁版嵁搴撳瘑鐮?鏁版嵁搴撳悕 > 瀵煎嚭鐨勬枃浠?/p>

# 姣斿锛歮ysqldump -h192.168.88.141 -uroot -p32345678 mysql_study > mysql_study.sql

瀛樺湪鐨勯棶棰橈細

濡傛灉鍦ㄤ富搴撲笂澶囦唤锛岄偅涔堝湪澶囦唤鏈熼棿涓嶈兘鎵ц鏇存柊鎿嶄綔锛屼笟鍔″熀鏈緱鍋滄憜濡傛灉鍦ㄤ粠搴撲笂澶囦唤锛岄偅涔堝浠芥湡闂翠粠搴撲笉鑳芥墽琛屼富搴撳悓姝ヨ繃鏉ョ殑浜岃繘鍒舵棩蹇楋紝浼氬鑷翠富浠庡欢杩燂紝浠庤€屽鑷翠富浠庢暟鎹笉涓€鑷?/p>

澶囨敞锛氬湪innodb寮曟搸涓紝鎴戜滑鍙互鍦ㄥ浠芥椂娣诲姞涓€涓弬鏁?-single-transaction鍙傛暟鏉ュ畬鎴愪笉鍔犻攣鐨勪竴鑷存€ф暟鎹簱澶囦唤锛圡ysql搴曞眰鏄€氳繃蹇収鏈嶅姟瀹炵幇鐨勶級

mysqldump --single-transaction -h192.168.88.141 -uroot -p32345678 mysql_study > mysql_study.sql

5.3 琛ㄧ骇閿?/p>

琛ㄧ骇閿侊紝姣忔鎿嶄綔閿佷綇鏁村紶琛ㄣ€傞攣瀹氱矑搴﹀ぇ锛屽彂鐢熼攣鍐茬獊鐨勬鐜囨渶楂橈紝骞跺彂搴︽渶浣庛€?/p>

琛ㄧ骇閿佺殑鍒嗙被锛?/p>

琛ㄩ攣锛氳〃閿佹槸mysql涓渶绠€鍗曠殑涓€绉嶉攣锛屽畠鍙互瀵规暣涓〃杩涜鍔犻攣銆傚綋涓€涓敤鎴峰琛ㄨ繘琛屼慨鏀规椂锛屽彲浠ヤ娇鐢ㄨ〃閿佸皢鏁翠釜琛ㄩ攣瀹氾紝浠ラ槻姝㈠叾浠栫敤鎴峰琛ㄨ繘琛岃鍐欐搷浣滐紝淇濊瘉鏁版嵁鐨勪竴鑷存€у拰瀹屾暣鎬с€傝〃閿佸彲浠ュ垎涓哄叡浜攣鍜屾帓浠栭攣涓ょ绫诲瀷銆? 鍏冩暟鎹攣锛坢eta data lock锛孧dl锛夛細鍏冩暟鎹攣鏄竴绉嶇壒娈婄殑閿侊紝瀹冨彲浠ュ鏁版嵁搴撲腑鐨勫厓鏁版嵁2杩涜鍔犻攣锛屼緥濡傝〃缁撴瀯銆佺储寮曠瓑銆傚綋涓€涓敤鎴峰琛ㄧ粨鏋勮繘琛屼慨鏀规椂锛屽彲浠ヤ娇鐢ㄥ厓鏁版嵁閿佸皢琛ㄧ殑鍏冩暟鎹攣瀹氾紝浠ラ槻姝㈠叾浠栫敤鎴峰琛ㄨ繘琛岃鍐欐搷浣滐紝淇濊瘉鏁版嵁鐨勪竴鑷存€у拰瀹屾暣鎬с€備富瑕佹槸涓轰簡閬垮厤dml涓嶥dl涓婚敭鐨勫啿绐侊紝淇濊瘉璇诲啓鐨勬纭€? 鎰忓悜閿侊細鎰忓悜閿佹槸涓€绉嶇壒娈婄殑閿侊紝瀹冧笉閿佸畾浠讳綍鏁版嵁锛屽彧鐢ㄤ簬鎸囩ず浜嬪姟鎴栬鍙ヨ閿佸畾浠€涔堢被鍨嬬殑閿併€傚綋涓€涓簨鍔℃垨璇彞瑕侀攣瀹氭煇涓〃鎴栬鏃讹紝浼氬厛灏濊瘯鑾峰彇鐩稿簲鐨勬剰鍚戦攣锛屼互鍛婅瘔鍏朵粬浜嬪姟鎴栬鍙ヨ繖涓〃鎴栬宸茬粡琚攣瀹氫簡锛岄伩鍏嶅叾浠栦簨鍔℃垨璇彞閲嶅鍔犻攣鎴栬繘琛屾棤鏁堢殑绛夊緟銆傛剰鍚戦攣鍒嗕负鎰忓悜鍏变韩閿佸拰鎰忓悜鎺掍粬閿佷袱绉嶇被鍨?娉ㄦ剰锛氭剰鍚戦攣涓嶄細瀵硅〃鎴栬杩涜瀹為檯鐨勫姞閿佹搷浣滐紝鍙槸鐢ㄤ簬鎸囩ず浜嬪姟鎴栬鍙ヨ閿佸畾浠€涔堢被鍨嬬殑閿?

5.3.1 琛ㄩ攣

琛ㄩ攣鐨勫垎绫?/p>

琛ㄥ叡浜閿侊紙read lock锛夛細琛ㄥ叡浜攣锛圱able shared lock锛夋槸涓€绉嶉攣瀹氭満鍒讹紝鐢ㄤ簬鎺у埗骞跺彂璁块棶鏁版嵁搴撲腑鐨勮〃銆傚綋涓€涓簨鍔¢渶瑕佽鍙栦竴寮犺〃涓殑鏁版嵁鏃讹紝瀹冧細璇锋眰璇ヨ〃鐨勫叡浜攣锛屼互纭繚璇诲彇鎿嶄綔涓嶄細涓庡叾浠栦簨鍔$殑鏇存柊鎿嶄綔鍙戠敓鍐茬獊琛ㄧ嫭鍗犲啓閿侊紙write lock锛夛細琛ㄧ嫭鍗犻攣锛圱able exclusive lock锛夋槸涓€绉嶉攣瀹氭満鍒讹紝鐢ㄤ簬鎺у埗骞跺彂璁块棶鏁版嵁搴撲腑鐨勮〃銆傚綋涓€涓簨鍔¢渶瑕佸涓€寮犺〃杩涜鏇存柊鎿嶄綔鏃讹紝瀹冧細璇锋眰璇ヨ〃鐨勭嫭鍗犻攣锛屼互纭繚鏇存柊鎿嶄綔涓嶄細涓庡叾浠栦簨鍔$殑璇诲彇鎴栨洿鏂版搷浣滃彂鐢熷啿绐? 琛ㄥ叡浜攣鍜岃〃鐙崰閿佺殑搴旂敤鍦烘櫙锛?/p>

琛ㄧ嫭鍗犻攣鐨勫簲鐢ㄥ満鏅細

褰撲竴涓簨鍔¢渶瑕佹洿鏂颁竴寮犺〃涓殑鏁版嵁鏃讹紝瀹冧細璇锋眰璇ヨ〃鐨勭嫭鍗犻攣锛屼互闃叉鍏朵粬浜嬪姟鍚屾椂瀵硅琛ㄨ繘琛屾洿鏂版搷浣滆€屽鑷存暟鎹笉涓€鑷淬€傚綋涓€涓簨鍔¢渶瑕佸涓€寮犺〃杩涜缁撴瀯鍙樻洿锛堝娣诲姞鎴栧垹闄ゅ垪锛夋椂锛屽畠蹇呴』鑾峰彇璇ヨ〃鐨勭嫭鍗犻攣锛屼互闃叉鍏朵粬浜嬪姟鍚屾椂瀵硅琛ㄨ繘琛岃鍙栨垨鏇存柊鎿嶄綔鑰屽鑷寸粨鏋勪笉涓€鑷淬€? 琛ㄥ叡浜攣鐨勫簲鐢ㄥ満鏅細

褰撲竴涓簨鍔¢渶瑕佽鍙栦竴寮犺〃涓殑鏁版嵁鏃讹紝瀹冧細璇锋眰璇ヨ〃鐨勫叡浜攣锛屼互纭繚璇诲彇鎿嶄綔涓嶄細涓庡叾浠栦簨鍔$殑鏇存柊鎿嶄綔鍙戠敓鍐茬獊銆傚綋涓€涓簨鍔¢渶瑕佸涓€寮犺〃杩涜璇诲彇鎿嶄綔锛屽苟涓斾笉闇€瑕佸鍏惰繘琛屾洿鏂版搷浣滄椂锛屽畠鍙互璇锋眰璇ヨ〃鐨勫叡浜攣锛屼互鍏佽鍏朵粬浜嬪姟鍚屾椂璇诲彇璇ヨ〃銆? 澶囨敞锛氳〃鐙崰閿佸拰琛ㄥ叡浜攣閮介渶瑕佽皑鎱庝娇鐢紝浠ラ伩鍏嶉攣绔炰簤鍜屾閿佺瓑闂 鍔犻攣\閲婃斁閿佺殑鎿嶄綔锛?# 鍔犻攣

-- 鍔犺〃鍏变韩璇婚攣

lock tables 琛ㄥ悕 read;

-- 鍔犺〃鐙崰鍐欓攣

lock tables 琛ㄥ悕 write;

# 閲婃斁閿?/p>

unlock tables;

绀轰緥

绀轰緥涓€锛?/p>

瀹㈡埛绔竴缁檚tudent琛ㄥ姞琛ㄥ叡浜閿侊紝鐒跺悗瀹㈡埛绔竴鍜屽鎴风浜屽垎鍒皾璇曡繘琛?璇绘搷浣滃拰鍐欐搷浣溿€?/p>

棰勬湡鏁堟灉锛氬鎴风涓€鍜屽鎴风浜岀殑璇绘搷浣滈兘鎴愬姛锛屽鎴风涓€鐨勫啓鎿嶄綔鐩存帴鎶ラ敊锛屽鎴风浜岀殑鍐欐搷浣滃浜庨樆濉炵姸鎬侊紝褰撳鎴风涓€閲婃斁浜嗚閿佹椂锛屽鎴风浜岀殑鍐欐搷浣滄墽琛屾垚鍔燂紙鎴愬姛楠岃瘉锛?/p>

绀轰緥浜岋細

瀹㈡埛绔竴缁檚tudent娣诲姞琛ㄧ嫭鍗犲啓閿侊紝鐒跺悗瀹㈡埛绔竴鍜屽鎴风浜屽垎鍒繘琛岃鍜屽啓鎿嶄綔銆?/p>

棰勬湡鏁堟灉锛氬鎴风涓€鐨勮鍜屽啓鎿嶄綔閮芥垚鍔熶簡锛屽鎴风浜岀殑璇诲拰鍐欐搷浣滈兘澶勪簬闃诲鐘舵€侊紙鎴愬姛楠岃瘉锛?/p>

5.3.2 鍏冩暟鎹攣

鈥冣€僊dl鍔犻攣杩囩▼鏄郴缁熻嚜鍔ㄦ帶鍒讹紝鏃犻渶鏄惧紡浣跨敤锛屽湪璁块棶涓€寮犺〃鐨勬椂鍊欎細鑷姩鍔犱笂銆侻dl閿佷富瑕佷綔鐢ㄦ槸缁存姢琛ㄥ厓鏁版嵁鐨勬暟鎹竴鑷存€э紝鍦ㄨ〃涓婃湁娲诲姩浜嬪姟鐨勬椂鍊欙紝涓嶅彲浠ュ鍏冩暟鎹繘琛屽啓鍏ユ搷浣溿€備富瑕佹槸涓轰簡閬垮厤dml涓嶥dl鍐茬獊锛屼繚璇佽鍐欑殑姝g‘鎬с€?/p>

鈥冣€冭繖閲岀殑鍏冩暟鎹紝澶у鍙互绠€鍗曠悊瑙d负灏辨槸涓€寮犺〃鐨勮〃缁撴瀯銆?涔熷氨鏄锛屾煇涓€寮犺〃娑夊強鍒版湭鎻愪氦鐨勪簨鍔℃椂锛屾槸涓嶈兘澶熶慨鏀硅繖寮犺〃鐨勮〃缁撴瀯鐨勩€傚湪mysql5.5涓紩鍏ヤ簡mdl锛屽綋瀵逛竴寮犺〃杩涜澧炲垹鏀规煡鐨勬椂鍊欙紝鍔燤dl璇婚攣(鍏变韩)锛涘綋瀵硅〃缁撴瀯杩涜鍙?鏇存搷浣滅殑鏃跺€欙紝鍔燤dl鍐欓攣(鎺掍粬閿侊級銆?/p>

甯歌鐨凷ql鎿嶄綔鏃讹紝鎵€娣诲姞鐨勫厓鏁版嵁閿侊細

绀轰緥

绀轰緥涓€锛?/p>

褰撴墽琛孲elect銆両nsert銆乁pdate銆丏elete绛夎鍙ユ椂锛屾坊鍔犵殑鏄厓鏁版嵁鍏变韩閿侊紙shared_read / shared_write锛夛紝涔嬮棿鏄吋瀹圭殑

鍙互鐪嬪埌褰撴垜浠湪绐楀彛涓€杩涜鏌ヨ鎿嶄綔鐨勫悓鏃讹紝鍦ㄧ獥鍙d簩杩涜鏌ヨ鎿嶄綔鍜屾洿鏂版搷浣滈兘鏄垚鍔熶簡锛屽苟娌℃湁鎶ラ敊锛岃繖鏄洜涓烘煡璇㈠拰鏇存柊鎿嶄綔閮芥槸鍏变韩閿侊紙鏌ヨ鎿嶄綔鏄叡浜閿侊紝鏇存柊鎿嶄綔鏄叡浜啓閿侊級锛屽叡浜攣涔嬮棿鏄吋瀹圭殑

绀轰緥浜岋細

褰撴墽琛孲elect璇彞鏃讹紝娣诲姞鐨勬槸鍏冩暟鎹叡浜攣锛圫hared_read锛夛紝浼氶樆濉炲厓鏁版嵁鎺掍粬閿侊紙exclusive锛夛紝涔嬮棿鏄簰鏂ョ殑

鈶犵獥鍙d竴寮€鍚簨鍔★紝鎵цdql璇彞锛堟煡璇tudent琛ㄧ殑鎵€鏈夋暟鎹級锛屼絾鏄鏃舵彁浜や簨鍔★紙涓€鏃︿簨鍔℃彁浜ゅ厓鏁版嵁閿佸氨琚噴鏀句簡锛?/p>

澶囨敞锛歁ysql榛樿鏄嚜鍔ㄦ彁浜や簨鍔$殑锛屾墍浠ヤ笉鏄剧ず寮€鍚簨鍔★紝dql璇彞涓€鎵ц鍏冩暟鎹攣灏辫閲婃斁浜嗭紝鎵€浠ヤ负浜嗘洿鍔犳槑鏄惧湴楠岃瘉鍏变韩閿佷笌鎺掍粬閿佹槸鍚﹀彂鐢熷啿绐侊紝杩欓闇€瑕佹樉绀哄湴寮€鍚簨鍔★紝褰撶劧锛屼綘涔熷彲浠ュ皢mysql浜嬪姟鐨勬彁浜ゆ柟寮忕敱鑷姩鏀逛负鎵嬪姩鎻愪氦

鈶$獥鍙d簩鎵цddl鎿嶄綔锛堜负student琛ㄦ柊澧炰竴涓瓧娈碉級锛屽彲浠ュ彂鐜版搷浣滄墽琛屽悗锛岃繘鍏ヤ簡闃诲鐘舵€侊紝杩欐槸鐢变簬ddl鎿嶄綔瑕佺粰琛ㄦ坊鍔犳帓浠栭攣锛屾鏃惰〃宸茬粡鐢变簡鍏变韩閿侊紝涓ょ閿佷簰鏂ワ紝鎵€浠dl鎿嶄綔闇€瑕佺瓑寰呭叡浜閲婃斁鍚庢墠鑳芥坊鍔犳帓浠栭攣

鈶㈢獥鍙d竴鎻愪氦浜嬪姟锛屾鏃剁獥鍙d竴缁檚tudent琛ㄦ坊鍔犵殑鍏变韩閿佽閲婃斁浜嗭紝鎵€浠ユ鏃剁獥鍙d簩澶勪簬闃诲鐘舵€佺殑ddl鐩存帴鍙互缁欒〃娣诲姞鎺掍粬閿侊紝鐒跺悗ddl灏辫兘澶熸墽琛屾垚鍔熶簡

5.3.3 鎰忓悜閿?/p>

涓轰簡閬垮厤dml鍦ㄦ墽琛屾椂锛屽姞鐨勮閿佷笌琛ㄩ攣鐨勫啿绐侊紝鍦↖nnodb涓紩鍏ヤ簡鎰忓悜閿侊紝浣垮緱琛ㄩ攣涓嶇敤妫€鏌ユ瘡琛屾暟鎹槸鍚﹀姞閿侊紝浣跨敤鎰忓悜閿佹潵鍑忓皯琛ㄩ攣鐨勬鏌ワ紙鎰忓悜閿佹槸涓€绉嶄笉涓庤绾ч攣鍐茬獊琛ㄧ骇閿侊級銆?/p>

澶囨敞锛氭剰鍚戦攣锛圛ntention lock锛夋槸鏈夋暟鎹紩鎿庤嚜宸辩淮鎶ょ殑锛岀敤鎴锋棤娉曟墜鍔ㄦ搷浣滄剰鍚戦攣锛屽湪涓烘暟鎹鍔犲叡浜?/ 鎺掍粬閿佷箣鍓嶏紝inoodb 浼氬厛鑾峰彇璇ユ暟鎹鎵€鍦ㄥ湪鏁版嵁琛ㄧ殑瀵瑰簲鎰忓悜閿侊紝涓€鏃︿簨鍔℃彁浜や簡锛屾剰鍚戝叡浜攣銆佹剰鍚戞帓浠栭攣锛岄兘浼氳嚜鍔ㄩ噴鏀俱€?/p>

鎰忓悜閿佺殑鍒嗙被

鎰忓悜鍏变韩閿侊紙intent share lock锛孖s锛夛細 鐢辫鍙elect ... lock in share mode娣诲姞 銆?涓庤〃閿佸叡浜攣(read)鍏煎锛屼笌琛ㄩ攣鎺掍粬閿?write)浜掓枼鎰忓悜鎺掍粬閿侊紙intent exclude lock锛孖x锛夛細 鐢眎nsert銆乽pdate銆乨elete銆乻elect...for update娣诲姞 銆備笌琛ㄩ攣鍏变韩閿?read)鍙婃帓浠栭攣(write)閮戒簰鏂ワ紝鎰忓悜閿佷箣闂翠笉浼氫簰鏂ワ紝涔熷氨鏄鎰忓悜鎺掍粬閿佸彧涓庡埆瀹朵汉锛堟櫘閫氱殑鎺掍粬 / 鍏变韩閿侊級浜掓枼锛屼笉涓庤嚜瀹朵汉锛堟剰鍚戦攣锛変簰鏂? 鍋囧娌℃湁鎰忓悜閿侊紝瀹㈡埛绔竴瀵硅〃鍔犱簡琛岄攣鍚庯紝瀹㈡埛绔簩濡備綍缁欒〃鍔犺〃閿佸憿锛屾潵閫氳繃绀烘剰鍥剧畝鍗曞垎鏋愪竴涓嬶細 鈶犻鍏堝鎴风涓€锛屽紑鍚竴涓簨鍔★紝鐒跺悗鎵цdml鎿嶄綔锛屽湪鎵цdml璇彞鏃讹紝浼氬娑夊強鍒扮殑琛屽姞琛岄攣 鈶″綋瀹㈡埛绔簩锛屾兂瀵硅繖寮犺〃鍔犺〃閿佹椂锛屼細妫€鏌ュ綋鍓嶈〃鏄惁鏈夊搴旂殑琛岄攣锛屽鏋滄病鏈夛紝鍒欐坊鍔犺〃閿侊紝姝ゆ椂灏变細浠庣涓€琛屾暟鎹紝妫€鏌ュ埌鏈€鍚庝竴琛屾暟鎹紝鏁堢巼杈? 鏈変簡鎰忓悜閿佷箣鍚?锛?鈶犲鎴风涓€锛屽湪鎵цdml鎿嶄綔鏃讹紝浼氬娑夊強鐨勮鍔犺閿侊紝鍚屾椂涔熶細瀵硅琛ㄥ姞涓婃剰鍚?鈶¤€屽叾浠栧鎴风锛屽湪瀵硅繖寮犺〃鍔犺〃閿佺殑鏃跺€欙紝浼氭牴鎹琛ㄤ笂鎵€鍔犵殑鎰忓悜閿佹潵鍒ゅ畾鏄惁鍙互鎴愬姛鍔犺〃閿侊紝鑰屼笉鐢ㄩ€愯鍒ゆ柇琛岄攣鎯呭喌

绀轰緥

鍙互閫氳繃浠ヤ笅sql锛屾煡鐪嬫剰鍚戦攣鍙婅閿佺殑鍔犻攣

select object_schema,object_name,index_name,lock_type,lock_mode,lock_data from

performance_schema.data_locks;

绀轰緥涓€锛?/p>

妫€楠?鎰忓悜鍏变韩閿佷笌琛ㄨ閿佹槸鍏煎鐨?/p>

鈶犲鎴风涓€娣诲姞 鎰忓悜鍏变韩閿?/p>

鈶″鎴风浜屾煡鐪嬫剰鍚戦攣鍜岃閿佺殑鎯呭喌锛岀劧鍚庝负琛ㄦ坊鍔犱竴涓?琛ㄧ骇鍏变韩閿?/p>

鍙互鐪嬪埌琛ㄧ骇鍏变韩閿佹坊鍔犳垚鍔熶簡锛岃繖璇存槑 鎰忓悜鍏变韩閿?涓庤〃绾у叡浜攣 涓嶅啿绐?/p>

鈶絾鏄鎴风浜屾坊鍔犺〃绾х嫭鍗犻攣锛屽彂鐢熶簡闃诲锛岃繖璇存槑 鎰忓悜鍏变韩閿?涓?琛ㄧ骇鍏变韩閿佸彂鐢熶簡鍐茬獊

绀轰緥浜岋細

妫€楠?鎰忓悜鎺掍粬閿佷笌琛ㄨ閿併€佸啓閿侀兘鏄簰鏂ョ殑

鐣モ€︹€?/p>

5.4 琛岀骇閿?/p>

琛岀骇閿侊紝姣忔鎿嶄綔閿佷綇瀵瑰簲鐨勮鏁版嵁銆傞攣瀹氱矑搴︽渶灏忥紝鍙戠敓閿佸啿绐佺殑姒傜巼鏈€浣庯紝骞跺彂搴︽渶楂樸€傚簲鐢ㄥ湪innodb瀛樺偍寮曟搸涓?/p>

innodb鐨勬暟鎹槸鍩轰簬绱㈠紩缁勭粐鐨勶紝琛岄攣鏄€氳繃瀵圭储寮曚笂鐨勭储寮曢」鍔犻攣鏉ュ疄鐜扮殑锛岃€屼笉鏄璁板綍鍔犵殑閿併€傚浜庤绾ч攣锛屼富瑕佸垎涓轰互涓嬩笁绫伙細

琛岄攣锛圧ecord lock锛夛細閿佸畾鍗曚釜琛岃褰曠殑閿侊紝闃叉鍏朵粬浜嬪姟瀵规琛岃繘琛寀pdate鍜宒elete銆傚湪rc銆丷r闅旂绾у埆涓嬮兘鏀寔 闂撮殭閿侊紙gap lock锛夛細閿佸畾绱㈠紩璁板綍闂撮殭锛堜笉鍚璁板綍锛夛紝纭繚绱㈠紩璁板綍闂撮殭涓嶅彉锛岄槻姝㈠叾浠栦簨鍔″湪杩欎釜闂撮殭杩涜insert锛屼骇鐢熷够璇汇€傚湪rr闅旂绾у埆涓嬮兘鏀寔 涓撮敭閿侊紙next-key lock锛夛細琛岄攣鍜岄棿闅欓攣缁勫悎锛屽悓鏃堕攣浣忔暟鎹紝骞堕攣浣忔暟鎹墠闈㈢殑闂撮殭gap銆傚湪rr闅旂绾у埆涓嬫敮鎸?

5.4.1 琛岄攣

琛岄攣鐨勫垎绫?/p>

鍏变韩閿侊紙s锛夛細鍏佽涓€涓簨鍔″幓璇讳竴琛岋紝闃绘鍏朵粬浜嬪姟鑾峰緱鐩稿悓鏁版嵁闆嗙殑鎺掑畠閿侊紙涓庡叡浜攣鍏煎锛屼笌鎺掍粬閿佷簰鏂ワ級鎺掍粬閿侊紙x锛夛細鍏佽鑾峰彇鎺掍粬閿佺殑浜嬪姟鏇存柊鏁版嵁锛岄樆姝㈠叾浠栦簨鍔¤幏寰楃浉鍚屾暟鎹泦鐨勫叡浜攣鍜屾帓浠栭攣锛堜笌鍏变韩閿佸拰涓庢帓浠栭攣浜掓枼锛?

澶囨敞锛氳閿侊紙intention lock锛夋槸鏈夋暟鎹紩鎿庤嚜宸辩淮鎶ょ殑锛屽湪涓烘暟鎹鍔犲叡浜?/ 鎺掍粬閿佷箣鍓嶏紝inoodb 浼氬厛鑾峰彇璇ユ暟鎹鎵€鍦ㄥ湪鏁版嵁琛ㄧ殑瀵瑰簲琛岄攣锛屼竴鏃︿簨鍔℃彁浜や簡锛屽叡浜攣銆佹帓浠栭攣锛岄兘浼氳嚜鍔ㄩ噴鏀?/p>

甯歌鐨凷ql璇彞锛屽湪鎵ц鏃讹紝鎵€鍔犵殑琛岄攣濡備笅锛?/p>

绀轰緥

榛樿鎯呭喌涓嬶紝innodb鍦?repeatable read浜嬪姟闅旂绾у埆杩愯锛孖nnodb浣跨敤 next-key 閿佽繘琛屾悳绱㈠拰绱㈠紩鎵弿锛屼互闃叉骞昏閽堝鍞竴绱㈠紩杩涜妫€绱㈡椂锛屽宸插瓨鍦ㄧ殑璁板綍杩涜绛夊€煎尮閰嶆椂锛屽皢浼氳嚜鍔ㄤ紭鍖栦负琛岄攣innodb鐨勮閿佹槸閽堝浜庣储寮曞姞鐨勯攣锛屼笉閫氳繃绱㈠紩鏉′欢妫€绱㈡暟鎹紝閭d箞innodb灏嗗琛ㄤ腑鐨勬墍鏈夎褰曞姞閿侊紝姝ゆ椂灏变細鍗囩骇涓鸿〃閿?/p>

鍙互閫氳繃浠ヤ笅sql锛屾煡鐪嬫剰鍚戦攣鍙婅閿佺殑鍔犻攣鎯呭喌锛?/p>

select object_schema,object_name,index_name,lock_type,lock_mode,lock_data from

performance_schema.data_locks;

绀轰緥涓€锛?/p>

鏅€氱殑select璇彞锛屾墽琛屾椂锛屼笉浼氬姞閿?/p>

绀轰緥浜岋細

select鈥ock in share mode锛屽姞鍏变韩閿侊紝鍏变韩閿佷笌鍏变韩閿佷箣闂村吋瀹癸紝鍏变韩閿佷笌鎺掍粬閿佷箣闂翠簰鏂ワ紝 鎺掑畠閿佷笌鎺掍粬閿佷箣闂翠簰鏂?/p>

鈶犲叡浜攣鍜屽叡浜攣鏄吋瀹圭殑

鈶″叡浜攣涓庢帓浠栭攣鏄簰鏂ョ殑

鐣モ€︹€?/p>

鈶㈡帓浠栭攣涓庢帓浠栭攣鏄簰鏂ョ殑

鐣モ€︹€?/p>

绀轰緥涓夛細

鏃犵储寮曡閿佸崌绾т负琛ㄩ攣锛堣繖涓湪鍓嶉潰 銆?.8 update浼樺寲銆?宸茬粡婕旂ず杩囦簡锛?/p>

鈶犱笉涓簄ame寤虹珛绱㈠紩锛岀劧鍚庝緷鎹畁ame鏉ヨ繘琛屾暟鎹洿鏂帮紝姝ゆ椂琛岄攣鍗囩骇涓鸿〃閿?/p>

鈶′负name寤虹珛绱㈠紩

5.4.2 闂撮殭閿?涓撮敭閿?/p>

榛樿鎯呭喌涓嬶紝innodb鍦?repeatable read浜嬪姟闅旂绾у埆杩愯锛孖nnodb浣跨敤 next-key锛堜复閿攣锛?閿佽繘琛屾悳绱㈠拰绱㈠紩鎵弿锛屼互闃叉骞昏绱㈠紩涓婄殑绛夊€兼煡璇?鍞竴绱㈠紩)锛岀粰涓嶅瓨鍦ㄧ殑璁板綍鍔犻攣鏃? 浼樺寲涓洪棿闅欓攣锛坓ap lock锛夌储寮曚笂鐨勭瓑鍊兼煡璇?闈炲敮涓€鏅€氱储寮?锛屽悜鍙抽亶鍘嗘椂鏈€鍚庝竴涓€间笉婊¤冻鏌ヨ闇€姹傛椂锛宯ext-key lock (涓村缓閿?閫€鍖栦负闂撮殭閿侊紙gap lock锛夌储寮曚笂鐨勮寖鍥存煡璇?鍞竴绱㈠紩)鈥撲細璁块棶鍒颁笉婊¤冻鏉′欢鐨勭涓€涓€间负姝?/p>

娉ㄦ剰锛氶棿闅欓攣鍞竴鐩殑鏄槻姝㈠叾浠栦簨鍔℃彃鍏ラ棿闅欍€傞棿闅欓攣鍙互鍏卞瓨锛屼竴涓簨鍔¢噰鐢ㄧ殑闂撮殭閿佷笉浼氶樆姝㈠彟涓€涓簨鍔″湪鍚屼竴闂撮殭涓婇噰鐢ㄩ棿闅欓攣

绀轰緥

绀轰緥涓€锛?/p>

绱㈠紩涓婄殑绛夊€兼煡璇?鍞竴绱㈠紩)锛岀粰涓嶅瓨鍦ㄧ殑璁板綍鍔犻攣鏃? 涓撮敭閿侀€€鍖栦负闂撮殭閿併€?/p>

鍘熷洜锛氬綋鎴戜滑缁欎竴涓笉瀛樺湪鐨勫€艰繘琛屾洿鏂版搷浣滄椂锛岀敱浜庢暟鎹笉瀛樺湪浼氱洿鎺ユ洿鏂板け璐ワ紝濡傛灉涓嶅姞闂撮殭閿侊紝姝ゆ椂鍏跺畠鐢ㄦ埛鎻掑叆杩欐潯鏁版嵁锛岃繖灏卞鑷村嚭鐜颁簡骞昏锛堜箣鍓嶆洿鏂版病鏈夎繖鏉¤褰曪紝鐜板湪鏌ヨ鍙堟湁杩欐潯璁板綍浜嗭級锛屾墍浠ュ共鑴嗙洿鎺ュ皢杩欎釜闂撮殭缁欓攣浣忥紝闃叉鍏跺畠鐢ㄦ埛杩涜鎻掑叆鎿嶄綔鍑虹幇骞昏鐜拌薄锛屼笉浣跨敤涓撮敭閿佹槸鍥犱负涓撮敭閿佷細灏嗗乏鍙充袱杈圭殑閭f潯璁板綍涔熼攣涓伙紝閿佺殑鑼冨洿澶ぇ浜嗭紝娌″繀瑕?/p>

姝ゆ椂濡傛灉鎴戜滑鎻掑叆璁板綍 id涓?銆?銆?杩欎笁鏉¤褰曟椂锛屼細杩涘叆闃诲鐘舵€?/p>

绀轰緥浜岋細

绱㈠紩涓婄殑绛夊€兼煡璇?闈炲敮涓€鏅€氱储寮?锛屽悜鍙抽亶鍘嗘椂鏈€鍚庝竴涓€间笉婊¤冻鏌ヨ闇€姹傛椂锛宯ext-key lock锛堜复閿攣锛?閫€鍖栦负闂撮殭閿併€?/p>

鍘熷洜锛氬洜涓烘槸闈炲敮涓€绱㈠紩锛岃繖涓粨鏋勪腑鍙兘鏈夊涓?鐨勫瓨鍦紝鎵€浠ワ紝鍦ㄥ姞閿佹椂浼氱户缁線鍚庢壘锛屾壘鍒颁竴涓笉婊¤冻鏉′欢鐨勫€硷紙褰撳墠妗堜緥涓篃灏辨槸8锛夈€傛鏃朵細瀵?鍔犱复閿攣锛屽苟瀵?涔嬪墠鐨勯棿闅欏姞閿?/p>

鈶犳牴鎹潪鍞竴绱㈠紩杩涜绛夊€兼煡璇?锛坣ame瀛楁鏈夐潪鍞竴绱㈠紩锛?/p>

鈶℃鏃讹紝璧靛叚杩欐潯璁板綍浼氭坊鍔犱复閿攣锛岀劧鍚?~8涔嬮棿浼氭坊鍔犻棿闅欓攣

绀轰緥涓夛細

绱㈠紩涓婄殑鑼冨洿鏌ヨ(鍞竴绱㈠紩)鈥撲細璁块棶鍒颁笉婊¤冻鏉′欢鐨勭涓€涓€间负姝€?/p>

鏌ヨ鐨勬潯浠朵负id>=6锛屾鏃舵垜浠彲浠ユ牴鎹暟鎹簱琛ㄤ腑鐜版湁鐨勬暟鎹紝灏嗘暟鎹垎涓轰笁涓儴鍒嗭細[6]銆?6,8]銆?8, 鈭瀅

鎵€浠ユ暟鎹簱鏁版嵁鍦ㄥ姞閿佹槸锛屽氨鏄皢6鍔犱簡琛岄攣锛?鐨勪复閿攣锛堜复寤洪攣鍖呭惈8鍙?涔嬪墠鐨勯棿闅欙級锛屾鏃犵┓鐨勪复閿攣(姝f棤绌峰強涔嬪墠鐨勯棿闅?

6銆両nnodb寮曟搸

6.1 innodb姒傝堪

innodb鐨勯€昏緫瀛樺偍缁撴瀯濡備笅鍥炬墍绀猴細

琛ㄧ┖闂达細琛ㄧ┖闂存槸innodb瀛樺偍寮曟搸閫昏緫缁撴瀯鐨勬渶楂樺眰锛?濡傛灉鐢ㄦ埛鍚敤浜嗗弬鏁?innodb_file_per_table(鍦?.0鐗堟湰涓粯璁ゅ紑鍚? 锛屽垯姣忓紶琛ㄩ兘浼氭湁涓€涓〃绌洪棿锛坸xx.ibd锛夛紝涓€涓猰ysql瀹炰緥鍙互瀵瑰簲澶氫釜琛ㄧ┖闂达紝鐢ㄤ簬瀛樺偍璁板綍銆佺储寮曠瓑娈碉細娈碉紝鍒嗕负鏁版嵁娈碉紙leaf node segment锛夈€佺储寮曟锛圢on-leaf node segment锛夈€佸洖婊氭锛圧ollback segment锛夛紝innodb鏄储寮曠粍缁囪〃锛屾暟鎹灏辨槸b 鏍戠殑鍙跺瓙鑺傜偣锛?绱㈠紩娈靛嵆涓築 鏍戠殑闈炲彾瀛愯妭鐐广€傛鐢ㄦ潵绠$悊澶氫釜extent锛堝尯锛氬尯锛岃〃绌洪棿鐨勫崟鍏冪粨鏋勶紝姣忎釜鍖虹殑澶у皬涓?m銆?榛樿鎯呭喌涓嬶紝 innodb瀛樺偍寮曟搸椤靛ぇ灏忎负16k锛?鍗充竴涓尯涓竴鍏辨湁64涓繛缁殑椤碉細椤碉紝鏄疘nnodb 瀛樺偍寮曟搸纾佺洏绠$悊鐨勬渶灏忓崟鍏冿紝姣忎釜椤电殑澶у皬榛樿涓?16kb銆備负浜嗕繚璇侀〉鐨勮繛缁€э紝innodb 瀛樺偍寮曟搸姣忔浠庣鐩樼敵璇?4-5 涓锛氳锛孖nnodb 瀛樺偍寮曟搸鏁版嵁鏄寜琛岃繘琛屽瓨鏀剧殑銆傚湪琛屼腑锛岄粯璁ゆ湁涓や釜闅愯棌瀛楁锛歍rx_id锛氭瘡娆″鏌愭潯璁板綍杩涜鏀瑰姩鏃讹紝閮戒細鎶婂搴旂殑浜嬪姟id璧嬪€肩粰trx_id闅愯棌鍒椼€俁oll_pointer锛氭瘡娆″鏌愭潯寮曡褰曡繘琛屾敼鍔ㄦ椂锛岄兘浼氭妸鏃х殑鐗堟湰鍐欏叆鍒皍ndo鏃ュ織涓紝鐒跺悗杩欎釜闅愯棌鍒楀氨鐩稿綋浜庝竴涓寚閽堬紝鍙互閫氳繃瀹冩潵鎵惧埌璇ヨ褰曚慨鏀瑰墠鐨勪俊

鍏充簬 innodb鐨勭壒鐐?浠ュ強 innodb涓庡叾瀹冨瓨鍌ㄥ紩鎿庣殑鍖哄埆 鍙互鐪嬪墠闈?/p>

澶囨敞锛歁ysql鐨勬暟鎹瓨鍌ㄧ洰褰昪d /var/llib/mysql

6.2 innodb鐨勬灦鏋?/p>

mysql5.5 鐗堟湰寮€濮嬶紝榛樿浣跨敤innodb瀛樺偍寮曟搸锛屽畠鎿呴暱浜嬪姟澶勭悊锛屽叿鏈夊穿婧冩仮澶嶇壒鎬э紝鍦ㄦ棩甯稿紑鍙戜腑浣跨敤闈炲父骞挎硾銆?/p>

璇︽儏璇峰弬鑰冨畼缃戯細mysql8.0瀹樻柟鎵嬪唽

涓嬮潰鏄疘nnodb鏋舵瀯鍥撅紝宸︿晶涓哄唴瀛樼粨鏋勶紝鍙充晶涓虹鐩樼粨鏋?/p>

6.2.1 鍐呭瓨缁撴瀯

鍐呭瓨缁撴瀯锛? 鍦ㄥ乏渚х殑鍐呭瓨缁撴瀯涓紝涓昏鍒嗕负杩欎箞鍥涘ぇ鍧楀効锛?buffer pool銆丆hange buffer銆丄daptive hash index銆丩og buffer銆?鎺ヤ笅鏉ヤ粙缁嶄竴涓嬭繖鍥涗釜閮ㄥ垎

buffer pool锛氱紦瀛樻睜锛孖nnodb瀛樺偍寮曟搸鍩轰簬纾佺洏鏂囦欢瀛樺偍锛岃闂墿鐞嗙‖鐩樺拰鍦ㄥ唴瀛樹腑杩涜璁块棶锛岄€熷害鐩稿樊寰堝ぇ锛屼负浜嗗敖鍙兘寮ヨˉ杩欎袱鑰呬箣闂寸殑i/o鏁堢巼鐨勫樊鍊硷紝灏遍渶瑕佹妸缁忓父浣跨敤鐨勬暟鎹姞杞藉埌缂撳啿姹犱腑锛岄伩鍏嶆瘡娆¤闂兘杩涜纾佺洏i/o銆傚湪innodb鐨勭紦鍐叉睜涓笉浠呯紦瀛樹簡绱㈠紩椤靛拰鏁版嵁椤碉紝杩樺寘鍚簡undo椤点€佹彃鍏ョ紦瀛樸€佽嚜閫傚簲鍝堝笇绱㈠紩浠ュ強innodb鐨勯攣淇℃伅绛夌瓑銆?缂撳啿姹?buffer pool锛屾槸涓诲唴瀛樹腑鐨勪竴涓尯鍩燂紝閲岄潰鍙互缂撳瓨纾佺洏涓婄粡甯告搷浣滅殑鐪熷疄鏁版嵁锛屽湪鎵ц澧炲垹鏀规煡鎿嶄綔鏃讹紝鍏堟搷浣滅紦鍐叉睜涓殑鏁版嵁锛堣嫢缂撳啿姹犳病鏈夋暟鎹紝鍒欎粠纾佺洏鍔犺浇骞剁紦瀛橈級锛岀劧鍚庡啀浠ヤ竴瀹氶鐜囧埛鏂板埌纾佺洏锛屼粠鑰屽噺灏戠鐩業o锛屽姞蹇鐞嗛€熷害銆傜紦鍐叉睜浠age椤典负鍗曚綅锛屽簳灞傞噰鐢ㄩ摼琛ㄦ暟鎹粨鏋勭鐞哖age銆傛牴鎹姸鎬侊紝灏哖age鍒嗕负涓夌绫诲瀷锛?/p>

绌洪棽椤碉紙free page锛夛細鏈浣跨敤琚娇鐢ㄩ〉锛坈lean page锛夛細鏁版嵁娌℃湁琚慨鏀硅繃銆傝剰椤碉紙dirty page锛夛細鏁版嵁琚慨鏀硅繃锛屼篃涓暟鎹笌纾佺洏鐨勬暟鎹骇鐢熶簡涓嶄竴鑷淬€?鍦ㄤ笓鐢ㄦ湇鍔″櫒涓婏紝閫氬父灏嗗杈?0锛呯殑鐗╃悊鍐呭瓨鍒嗛厤缁欑紦鍐叉睜 銆傚弬鏁拌缃細 # 鏌ョ湅缂撳瓨姹犲ぇ灏忥紙榛樿鍊?34217728锛岀害涓?.3gb锛?/p>

show variables like 'innodb_buffer_pool_size';

change buffer锛氭洿鏀圭紦鍐插尯锛堥拡瀵逛簬闈炲敮涓€浜岀骇绱㈠紩椤碉級锛屽湪鎵цdml璇彞鏃讹紝濡傛灉杩欎簺鏁版嵁page娌℃湁鍦˙uffer pool涓紝涓嶄細鐩存帴鎿嶄綔纾佺洏锛岃€屼細灏嗘暟鎹彉鏇村瓨鍦ㄦ洿鏀圭紦鍐插尯 change buffer涓紝鍦ㄦ湭鏉ユ暟鎹璇诲彇鏃讹紝鍐嶅皢鏁版嵁鍚堝苟鎭㈠鍒癇uffer pool涓紝鍐嶅皢鍚堝苟鍚庣殑鏁版嵁鍒锋柊鍒扮鐩樹腑銆?/p>

change buffer瀛樺湪鐨勬剰涔変綍鍦紵

涓庤仛闆嗙储寮曚笉鍚岋紝浜岀骇绱㈠紩閫氬父鏄潪鍞竴鐨勶紝骞朵笖浠ョ浉瀵归殢鏈虹殑椤哄簭鎻掑叆浜岀骇绱㈠紩銆傚悓鏍凤紝鍒犻櫎鍜屾洿鏂板彲鑳戒細褰卞搷绱㈠紩鏍戜腑涓嶇浉閭荤殑浜岀骇绱㈠紩椤碉紝濡傛灉姣忎竴娆¢兘鎿嶄綔纾佺洏锛屼細閫犳垚澶ч噺鐨勭鐩業o銆傛湁浜咰hangebuffer涔嬪悗锛屾垜浠彲浠ュ湪缂撳啿姹犱腑杩涜鍚堝苟澶勭悊锛屽噺灏戠鐩業o銆?/p>

adaptive hash index锛氳嚜閫傚簲hash绱㈠紩锛岀敤浜庝紭鍖栧buffer pool鏁版嵁鐨勬煡璇€侻ysql鐨刬nnodb寮曟搸涓櫧鐒舵病鏈夌洿鎺ユ敮鎸?hash绱㈠紩锛屼絾鏄粰鎴戜滑鎻愪緵浜嗕竴涓姛鑳藉氨鏄繖涓嚜閫傚簲hash绱㈠紩銆傚洜涓哄墠闈㈡垜浠鍒拌繃锛宧ash绱㈠紩鍦ㄨ繘琛岀瓑鍊煎尮閰嶆椂锛屼竴鑸€ц兘鏄楂樹簬b 鏍戠殑锛屽洜涓篽ash绱㈠紩涓€鑸彧闇€瑕佷竴娆o鍗冲彲锛岃€孊 鏍戯紝鍙兘闇€瑕佸嚑娆″尮閰嶏紝鎵€浠ash绱㈠紩鐨勬晥鐜囪楂橈紝浣嗘槸hash绱㈠紩鍙堜笉閫傚悎鍋氳寖鍥存煡璇€佹ā绯婂尮閰嶇瓑銆侷nnodb瀛樺偍寮曟搸浼氱洃鎺у琛ㄤ笂鍚勭储寮曢〉鐨勬煡璇紝濡傛灉瑙傚療鍒板湪鐗瑰畾鐨勬潯浠朵笅hash绱㈠紩鍙互鎻愬崌閫熷害锛屽垯寤虹珛hash绱㈠紩锛岀о涔嬩负鑷€傚簲hash绱㈠紩銆傦紙鑷€傚簲鍝堝笇绱㈠紩锛屾棤闇€浜哄伐骞查锛屾槸绯荤粺鏍规嵁鎯呭喌鑷姩瀹屾垚锛?# 鏌ョ湅鑷€傚簲hash鏄惁寮€鍚?榛樿鍊糘n锛岃〃绀哄紑鍚?

show variables like 'hash_index';

log buffer锛氭棩蹇楃紦鍐插尯锛岀敤鏉ヤ繚瀛樿鍐欏叆鍒扮鐩樹腑鐨刲og鏃ュ織鏁版嵁锛坮edo log 銆乽ndo log锛夛紝榛樿澶у皬涓?16mb锛屾棩蹇楃紦鍐插尯鐨勬棩蹇椾細瀹氭湡鍒锋柊鍒扮鐩樹腑銆傚鏋滈渶瑕佹洿鏂般€佹彃鍏ユ垨鍒犻櫎璁稿琛岀殑浜嬪姟锛屽鍔犳棩蹇楃紦鍐插尯鐨勫ぇ灏忓彲浠ヨ妭鐪佺鐩?i/o銆?# 鏌ョ湅鏃ュ織缂撳啿鍖虹殑澶у皬锛堥粯璁ゅぇ灏忎负16777216锛岀害涓?6mb锛?/p>

show variables like 'innodb_log_buffer_size';

# 鏌ョ湅鏃ュ織鍒锋柊鍒扮鐩樼殑鏃舵満锛堜笁涓彇鍊硷紝1銆?銆?锛?/p>

show variables like 'innodb_flush_log_at_trx_commit';

-- 1: 鏃ュ織鍦ㄦ瘡娆′簨鍔℃彁浜ゆ椂鍐欏叆骞跺埛鏂板埌纾佺洏锛岄粯璁ゅ€笺€?/p>

-- 0: 姣忕灏嗘棩蹇楀啓鍏ュ苟鍒锋柊鍒扮鐩樹竴娆°€?/p>

-- 2: 鏃ュ織鍦ㄦ瘡娆′簨鍔℃彁浜ゅ悗鍐欏叆锛屽苟姣忕鍒锋柊鍒扮鐩樹竴娆°€?/p>

6.2.2 纾佺洏缁撴瀯

纾佺洏缁撴瀯

system tablespace锛氱郴缁熻〃绌洪棿鏄洿鏀圭紦鍐插尯鐨勫瓨鍌ㄥ尯鍩熴€傚鏋滆〃鏄湪绯荤粺琛ㄧ┖闂磋€屼笉鏄瘡涓〃鏂囦欢鎴栭€氱敤琛ㄧ┖闂翠腑鍒涘缓 鐨勶紝瀹冧篃鍙兘鍖呭惈琛ㄥ拰绱㈠紩鏁版嵁銆?鍦∕ysql5.x鐗堟湰涓繕鍖呭惈innodb鏁版嵁瀛楀吀銆乽ndolog绛? # 鏌ョ湅绯荤粺琛ㄧ┖闂达紙榛樿鍊?ibdata1:12m:autoextend 锛?/p>

show variables like 'innodb_data_file_path';

澶囨敞锛氱郴缁熻〃绌洪棿锛岄粯璁ょ殑鏂囦欢鍚嶅彨 ibdata1锛屽彲浠ラ€氳繃cd /var/lib/mysql锛岃繘鍏ysql鏁版嵁瀛樺偍鐩綍锛屽湪杩欎釜鐩綍鍙互鐪嬪埌 ibadata1 杩欎釜鏂囦欢 file-per-table tablespaces锛氭瘡琛ㄦ枃浠惰〃绌洪棿锛屽鏋滃紑鍚簡innodb_file_per_table寮€鍏?锛屽垯姣忎釜琛ㄧ殑鏂囦欢琛ㄧ┖闂村寘鍚崟涓狪nnodb琛ㄧ殑鏁版嵁鍜岀储寮?锛屽苟瀛樺偍鍦ㄦ枃浠剁郴缁熶笂鐨勫崟涓暟鎹枃浠朵腑 # 鏌ユ瘡琛ㄦ枃浠惰〃绌洪棿锛堥粯璁ゅ€间负on锛岃〃绀哄紑鍚級

show variables like 'innodb_file_per_table';

general tablespaces锛氶€氱敤琛ㄧ┖闂达紝闇€瑕侀€氳繃 create tablespace 璇硶鍒涘缓閫氱敤琛ㄧ┖闂达紝鍦ㄥ垱寤鸿〃鏃讹紝鍙互鎸囧畾璇ヨ〃绌洪棿 # 鍒涘缓琛ㄧ┖闂?/p>

create tablespace ts_name add datafile 'file_name' engine = engine_name;

-- 绀轰緥锛?/p>

create tablespace ts_mysql_study add datafile 'mysql_study.ibd' engine = innodb;

# 鍒涘缓琛ㄦ椂鎸囧畾琛ㄧ┖闂?/p>

create table xxx ... tablespace ts_name;

-- 绀轰緥锛?/p>

create table test(id int primary key auto_increment, name varchar(10)) engine=innodb tablespace ts_mysql_study;

undo tablespaces锛氭挙閿€琛ㄧ┖闂达紝mysql瀹炰緥鍦ㄥ垵濮嬪寲鏃朵細鑷姩鍒涘缓涓や釜榛樿鐨剈ndo琛ㄧ┖闂达紙鍒濆澶у皬16m锛夛紝鐢ㄤ簬瀛樺偍 undo log鏃ュ織锛圲ndo log 琚о涓烘挙閿€鏃ュ織銆佸洖婊氭棩蹇楋紝璁板綍鍥炴粴鎿嶄綔锛? temporary tablespaces锛氫复鏃惰〃绌洪棿銆傚瓨鍌ㄧ敤鎴峰垱寤虹殑涓存椂琛ㄧ瓑鏁版嵁 doublewrite buffer files锛氬弻鍐欑紦鍐插尯锛宨nnodb寮曟搸灏嗘暟鎹〉浠嶣uffer pool鍒锋柊鍒扮鐩樺墠锛屽厛灏嗘暟鎹〉鍐欏叆鍙屽啓缂撳啿鍖烘枃浠朵腑锛屼究浜庣郴缁熷紓甯告椂鎭㈠鏁版嵁 澶囨敞锛氭枃浠跺悕#ib_xxx_xxx.dblwr redo log锛氶噸鍋氭棩蹇楋紝鏄敤鏉ュ疄鐜颁簨鍔$殑鎸佷箙鎬с€傝鏃ュ織鏂囦欢鐢变袱閮ㄥ垎缁勬垚锛氶噸鍋氭棩蹇楃紦鍐诧紙redo logbuffer锛変互鍙婇噸鍋氭棩蹇楁枃浠讹紙redo log锛?鍓嶈€呮槸鍦ㄥ唴瀛樹腑锛屽悗鑰呭湪纾佺洏涓€傚綋浜嬪姟鎻愪氦涔嬪悗浼氭妸鎵€鏈変慨鏀逛俊鎭兘浼氬瓨鍒拌鏃ュ織涓? 鐢ㄤ簬鍦ㄥ埛鏂拌剰椤靛埌纾佺洏鏃?鍙戠敓閿欒鏃? 杩涜鏁版嵁鎭㈠浣跨敤 浠ュ惊鐜柟寮忓啓鍏ラ噸鍋氭棩蹇楁枃浠讹紝娑夊強涓や釜鏂?ib_logfile0 鍜?ib_logfile1

6.2.3 鍚庡彴绾跨▼

鍓嶉潰鎴戜滑浠嬬粛浜咺nnodb鐨勫唴瀛樼粨鏋勶紝浠ュ強纾佺洏缁撴瀯锛岄偅涔堝唴瀛樹腑鎴戜滑鎵€鏇存柊鐨勬暟鎹紝鍙堟槸濡備綍鍒扮鐩樹腑鐨勫憿锛?姝ゆ椂锛屽氨娑夊強鍒颁竴缁勫悗鍙扮嚎绋嬶紝鎺ヤ笅鏉ワ紝灏辨潵浠嬬粛涓€浜汭nnodb涓秹鍙婂埌鐨勫悗鍙扮嚎绋?/p>

鍦↖nnodb鐨勫悗鍙扮嚎绋嬩腑锛屽垎涓?绫伙紝鍒嗗埆鏄細master thread 銆両o thread銆丳urge thread銆丳age cleaner thread

master thread锛氭牳蹇冨悗鍙扮嚎绋嬶紝璐熻矗璋冨害鍏朵粬绾跨▼锛岃繕璐熻矗灏嗙紦鍐叉睜涓殑鏁版嵁寮傛鍒锋柊鍒扮鐩樹腑, 淇濇寔鏁版嵁鐨勪竴鑷存€э紝杩樺寘鎷剰椤电殑鍒锋柊銆佸悎骞舵彃鍏ョ紦瀛樸€乽ndo椤电殑鍥? io thread锛氬湪innodb瀛樺偍寮曟搸涓ぇ閲忎娇鐢ㄤ簡aio鏉ュ鐞咺o璇锋眰, 杩欐牱鍙互鏋佸ぇ鍦版彁楂樻暟鎹簱鐨勬€ц兘锛岃€孖othread涓昏璐熻矗杩欎簺io璇锋眰鐨勫洖 # 鏌ョ湅鏌ョ湅鍒癐nnodb鐨勭姸鎬佷俊鎭紝鍏朵腑灏卞寘鍚獻o thread淇℃伅

show engine innodb status \g;

purge thread锛氫富瑕佺敤浜庡洖鏀朵簨鍔″凡缁忔彁浜や簡鐨剈ndo log锛屽湪浜嬪姟鎻愪氦涔嬪悗锛寀ndo log鍙兘涓嶇敤浜嗭紝灏辩敤瀹冩潵鍥炴敹 page cleaner thread锛氬崗鍔?master thread 鍒锋柊鑴忛〉鍒扮鐩樼殑绾跨▼锛屽畠鍙互鍑忚交 master thread 鐨勫伐浣滃帇鍔涳紝鍑忓皯闃?

6.3 浜嬪姟鍘熺悊

浠€涔堟槸浜嬪姟锛?/p>

浜嬪姟鏄竴缁勬搷浣滅殑闆嗗悎锛屽畠鏄竴涓笉鍙垎鍓茬殑宸ヤ綔鍗曚綅锛屼簨鍔′細鎶婃墍鏈夌殑鎿嶄綔浣滀负涓€涓暣浣撲竴璧峰悜绯荤粺鎻愪氦鎴栨挙閿€鎿嶄綔璇锋眰锛屽嵆杩欎簺鎿嶄綔瑕佷箞鍚屾椂鎴愬姛锛岃涔堝悓鏃跺け璐?/p>

浜嬪姟鐨凙cid鐗规€э細

鍘熷瓙鎬э紙atomicity锛夛細浜嬪姟鏄笉鍙垎鍓茬殑鏈€灏忔搷浣滃崟鍏冿紝瑕佷箞鍏ㄩ儴鎴愬姛锛岃涔堝叏閮ㄥけ璐ャ€備竴鑷存€э紙consistency锛夛細浜嬪姟瀹屾垚鏃讹紝蹇呴』浣挎墍鏈夌殑鏁版嵁閮戒繚鎸佷竴鑷寸姸鎬併€傞殧绂绘€э紙isolation锛夛細鏁版嵁搴撶郴缁熸彁渚涚殑闅旂鏈哄埗锛屼繚璇佷簨鍔″湪涓嶅彈澶栭儴骞跺彂鎿嶄綔褰卞搷鐨勭嫭绔嬬幆澧冧笅杩愯銆傛寔涔呮€э紙durability锛夛細浜嬪姟涓€鏃︽彁浜ゆ垨鍥炴粴锛屽畠瀵规暟鎹簱涓殑鏁版嵁鐨勬敼鍙樺氨鏄案涔?

瀹為檯涓婏紝鎴戜滑鐮旂┒浜嬪姟鐨勫師鐞嗭紝灏辨槸鐮旂┒mysql鐨処nnodb寮曟搸鏄浣曚繚璇佷簨鍔$殑杩欏洓澶х壒鎬х殑銆傝€屽浜庤繖鍥涘ぇ鐗规€э紝瀹為檯涓婂垎涓轰袱涓儴鍒嗐€?鍏朵腑鐨勫師瀛愭€с€佷竴鑷存€с€佹寔涔呭寲锛屽疄闄呬笂鏄敱innodb涓殑涓や唤鏃ュ織鏉ヤ繚璇佺殑锛屼竴浠芥槸redo log鏃ュ織锛屼竴浠芥槸undo log鏃ュ織銆?鑰屾寔涔呮€ф槸閫氳繃鏁版嵁搴撶殑**閿?mvcc**鏉ヤ繚璇佺殑銆傛垜浠湪璁茶в浜嬪姟鍘熺悊鐨勬椂鍊欙紝涓昏灏辨槸鏉ョ爺绌朵竴涓媟edo log锛寀ndo log浠ュ強mvcc

澶囨敞锛?/p>

鍘熷瓙鎬э細undo log鎸佷箙鎬э細redo log涓€鑷存€э細undo log redo log闅旂鎬э細閿? mvcc

6.3.1 redo log

浠€涔堟槸 redo log?

redo log 琚о浣滈噸鍋氭棩蹇楋紝璁板綍鐨勬槸浜嬪姟鎻愪氦鏃舵暟鎹〉鐨勭墿鐞嗕慨鏀癸紝鏄敤鏉ュ疄鐜颁簨鍔$殑鎸佷箙鎬с€?/p>

褰撴暟鎹簱鎵ц鍐欏叆鎿嶄綔鏃讹紝redo log浼氳褰曚笅鎵€鍋氱殑淇敼銆傝繖浜涗慨鏀硅璁板綍涓嬫潵涔嬪悗锛屽嵆浣垮湪鍐欏叆鎿嶄綔鏈鍐欏叆鍒扮鐩樹箣鍓嶏紝鏁版嵁搴撶郴缁熶篃鍙互閫氳繃redo log涓殑淇℃伅閲嶆柊鎵ц杩欎簺淇敼鎿嶄綔銆?/p>

redo log閫氬父鏄竴涓惊鐜殑銆佸浐瀹氬ぇ灏忕殑鏂囦欢锛屽叾涓寘鍚簡鏁版嵁搴撶郴缁熶腑鏈€杩戠殑涓€浜涘啓鍏ユ搷浣滅殑淇℃伅銆傚綋redo log鏂囦欢宸叉弧鏃讹紝鏁版嵁搴撶郴缁熶細灏嗗叾涓殑鍐呭鍐欏叆鍒扮鐩樹笂鐨勬暟鎹枃浠朵腑锛屽苟閲嶆柊寮€濮嬭褰曟柊鐨勫啓鍏ユ搷浣溿€?/p>

redo log 鐨勪綔鐢細

淇濊瘉浜嬪姟鐨勬寔涔呮€э細redo log璁板綍浜嗘墍鏈変慨鏀规暟鎹簱鐨勬搷浣滐紝鍖呮嫭鏁版嵁鐨勬彃鍏ャ€佹洿鏂板拰鍒犻櫎绛夋搷浣溿€傝繖鏍凤紝鍗充娇鍦ㄤ簨鍔℃彁浜や箣鍓嶏紝濡傛灉绯荤粺宕╂簝鎴栨柇鐢碉紝redo log涓殑淇℃伅涔熷彲浠ョ敤鏉ユ仮澶嶆暟鎹紝纭繚浜嬪姟瀵规暟鎹簱鐨勪慨鏀规槸鎸佷箙鐨勩€傛彁楂樼郴缁熺殑鎬ц兘锛氬綋鏁版嵁搴撴墽琛屽啓鍏ユ搷浣滄椂锛宺edo log鍙互灏嗚繖浜涙搷浣滆褰曞埌鍐呭瓨涓紝鑰屼笉蹇呯珛鍗冲啓鍏ョ鐩樸€傝繖鏍峰彲浠ラ伩鍏嶉绻佸湴璁块棶纾佺洏锛屾彁楂樼郴缁熺殑鎬ц兘銆傛彁楂樼郴缁熺殑鍙潬鎬э細褰撶郴缁熷穿婧冩垨鏂數鏃讹紝redo log鍙互鐢ㄦ潵鎭㈠鏁版嵁搴擄紝浣垮叾鎭㈠鍒板穿婧冧箣鍓嶇殑鐘舵€併€傝繖鏍峰彲浠ラ伩鍏嶆暟鎹殑涓㈠け鍜屾崯鍧忥紝鎻愰珮绯荤粺鐨勫彲闈犳€с€傛敮鎸佺儹澶囦唤锛歳edo log鍙互琚敤鏉ユ敮鎸佺儹澶囦唤锛屽嵆鍦ㄨ繍琛屼腑鐨勬暟鎹簱涓婅繘琛屽浠姐€傜儹澶囦唤鍙互閬垮厤瀵规暟鎹簱鐨勫仠鏈烘椂闂达紝瀵逛簬闇€瑕?4灏忔椂涓嶉棿鏂繍琛岀殑绯荤粺闈炲父閲嶈銆? redo log 鐨勭粍鎴愶細

閲嶅仛鏃ュ織缂撳啿锛坮edo log buffer锛夛細鏄唴瀛樹腑鐨勪竴鍧楃紦鍐插尯锛岀敤浜庢殏瀛樺嵆灏嗗啓鍏ラ噸鍋氭棩蹇楁枃浠朵腑鐨勯噸鍋氭棩蹇楄褰曘€傚綋鏁版嵁搴撴墽琛屽啓鍏ユ搷浣滄椂锛屼慨鏀规搷浣滀細琚厛鍐欏叆閲嶅仛鏃ュ織缂撳啿涓紝鑰屼笉鏄洿鎺ュ啓鍏ラ噸鍋氭棩蹇楁枃浠躲€傝繖鏍疯兘澶熷噺灏戠鐩業/o鐨勫紑閿€锛屾彁楂樻暟鎹簱鐨勬€ц兘閲嶅仛鏃ュ織鏂囦欢锛坮edo log file锛夛細鏄竴涓垨澶氫釜鍥哄畾澶у皬鐨勬棩蹇楁枃浠讹紝鐢ㄤ簬鎸佷箙鍖栧瓨鍌ㄥ凡缁忓啓鍏ュ埌閲嶅仛鏃ュ織缂撳啿涓殑閲嶅仛鏃ュ織璁板綍銆傞噸鍋氭棩蹇楁枃浠舵槸寰幆浣跨敤鐨勶紝褰撲竴涓噸鍋氭棩蹇楁枃浠跺啓婊℃椂锛屾暟鎹簱浼氳嚜鍔ㄥ垏鎹㈠埌涓嬩竴涓噸鍋氭棩蹇楁枃浠讹紝浠ョ‘淇濋噸鍋氭棩蹇楃殑杩炵画鎬?

濡傛灉娌℃湁redo log浼氭€庢牱锛?/p>

涓嶅姞redo log鐨勬儏鍐碉細 鎴戜滑鐭ラ亾锛屽湪innodb寮曟搸涓殑鍐呭瓨缁撴瀯涓紝涓昏鐨勫唴瀛樺尯鍩熷氨鏄紦鍐叉睜锛坆uffer pool锛夛紝鍦ㄧ紦鍐叉睜涓紦瀛樹簡寰堝鐨勬暟鎹〉銆?褰撴垜浠湪涓€涓簨鍔′腑锛屾墽琛屽涓鍒犳敼鐨勬搷浣滄椂锛孖nnodb寮曟搸浼氬厛鎿嶄綔缂撳啿姹犱腑鐨勬暟鎹紝濡傛灉缂撳啿鍖烘病鏈夊搴旂殑鏁版嵁锛屼細閫氳繃鍚庡彴绾跨▼灏嗙鐩樹腑鐨勬暟鎹姞杞藉嚭鏉ワ紝瀛樻斁鍦ㄧ紦鍐插尯涓紝鐒跺悗灏嗙紦鍐叉睜涓殑鏁版嵁淇敼锛屼慨鏀瑰悗鐨勬暟鎹〉鎴戜滑绉颁负鑴忛〉銆?鑰岃剰椤靛垯浼氬湪涓€瀹氱殑鏃舵満锛岄€氳繃鍚庡彴绾跨▼鍒锋柊鍒扮鐩樹腑锛屼粠鑰屼繚璇佺紦鍐插尯涓庣鐩樼殑鏁版嵁涓€鑷淬€?鑰岀紦鍐插尯鐨勮剰椤垫暟鎹苟涓嶆槸瀹炴椂鍒锋柊鐨勶紝鑰屾槸涓€娈垫椂闂翠箣鍚庡皢缂撳啿鍖虹殑鏁版嵁鍒锋柊鍒扮鐩樹腑锛屽亣濡傚埛鏂板埌纾佺洏鐨勮繃绋嬪嚭閿欎簡锛岃€屾彁绀虹粰鐢ㄦ埛浜嬪姟鎻愪氦鎴愬姛锛岃€屾暟鎹嵈娌℃湁鎸佷箙鍖栦笅鏉ワ紝杩欏氨鍑虹幇闂浜嗭紝娌℃湁淇濊瘉浜嬪姟鐨勬寔涔呮€с€? 娣诲姞redo log鐨勬儏鍐碉細 鏈変簡redo log涔嬪悗锛屾垜浠湪杩涜dml璇彞锛堝鍒犳敼鎿嶄綔锛夋椂锛屾暟鎹収鏍峰厛鍔犺浇鍒扮紦鍐叉睜涓紝鍚屾椂杩樹細鍔犲叆鍒?redo log buffer 涓€傚湪浜嬪姟鎻愪氦鏃讹紝浼氬皢redo log buffer涓殑鏁版嵁鍒锋柊鍒?redo log纾佺洏鏂囦欢涓€傚鏋滅紦鍐叉睜(buffer pool) 鍦ㄥ埛鏂拌剰鏁版嵁鍒?纾佺洏鏃讹紝鍙戠敓浜嗛敊璇紝杩欐椂灏卞彲浠ュ€熷姪 redo log 杩涜鏁版嵁鎭㈠浜嗭紝浠庤€岄伩鍏嶈剰鏁版嵁鐨勪涪澶憋紝浠庤€屼繚璇佹暟鎹殑鎸佷箙鍖栵紱濡傛灉buffer pool鎴愬姛灏嗘爲鍒锋柊鍒扮鐩樹腑锛屾鏃?redo log灏辨病鏈変綔鐢ㄤ簡锛屽氨鍙互灏嗚繖涓?redo log鏂囦欢鍒犻櫎 閭d负浠€涔堟瘡涓€娆℃彁浜や簨鍔★紝瑕佸埛鏂皉edo log 鍒扮鐩樹腑鍛紝鑰屼笉鏄洿鎺ュ皢buffer pool涓殑鑴忛〉鍒锋柊鍒扮鐩樺憿 ? 鍥犱负鍦ㄤ笟鍔℃搷浣滀腑锛屾垜浠搷浣滄暟鎹竴鑸兘鏄殢鏈鸿鍐欑鐩樼殑锛岃€屼笉鏄『搴忚鍐欑鐩樸€?鑰宺edo log鍦ㄥ線纾佺洏鏂囦欢涓啓鍏ユ暟鎹紝鐢变簬鏄棩蹇楁枃浠讹紝鎵€浠ラ兘鏄『搴忓啓鐨勩€傞『搴忓啓鐨勬晥鐜囷紝瑕佽繙澶т簬闅忔満鍐欍€?杩欑鍏堝啓鏃ュ織鐨勬柟寮忥紝绉颁箣涓?wal锛圵rite-ahead logging锛夈€?

6.3.2 undo log

浠€涔堟槸 undo log锛?/p>

鍥炴粴鏃ュ織锛岀敤浜庤褰曟暟鎹淇敼鍓嶇殑淇℃伅 , 浣滅敤鍖呭惈涓や釜 : 鎻愪緵鍥炴粴(淇濊瘉浜嬪姟鐨勫師瀛愭€? 鍜孧vcc(澶氱増鏈苟鍙戞帶鍒?銆?/p>

undo log鍜宺edo log璁板綍鐗╃悊鏃ュ織涓嶄竴鏍凤紝瀹冩槸閫昏緫鏃ュ織銆傚彲浠ヨ涓哄綋delete涓€鏉¤褰曟椂锛寀ndolog涓細璁板綍涓€鏉″搴旂殑insert璁板綍锛屽弽涔嬩害鐒讹紝褰搖pdate涓€鏉¤褰曟椂锛屽畠璁板綍涓€鏉″搴旂浉鍙嶇殑update璁板綍銆傚綋鎵цrollback鏃讹紝灏卞彲浠ヤ粠undo log涓殑閫昏緫璁板綍璇诲彇鍒扮浉搴旂殑鍐呭骞惰繘琛屽洖

褰撴暟鎹簱鎵ц鍐欏叆鎿嶄綔鏃讹紝undo log浼氳褰曚笅鎵€鍋氱殑淇敼鐨勫弽鍚戞搷浣滐紝涔熷氨鏄挙閿€鎿嶄綔銆傝繖浜涘弽鍚戞搷浣滆璁板綍涓嬫潵涔嬪悗锛屽彲浠ョ敤浜庡洖婊氫簨鍔″拰鏀寔mvcc銆倁ndo log閫氬父鏄竴涓惊鐜殑銆佸浐瀹氬ぇ灏忕殑鏂囦欢锛屽叾涓寘鍚簡鏁版嵁搴撶郴缁熶腑鏈€杩戠殑涓€浜涘啓鍏ユ搷浣滅殑鍙嶅悜鎿嶄綔銆傚綋undo log鏂囦欢宸叉弧鏃讹紝鏁版嵁搴撶郴缁熶細灏嗗叾涓殑鍐呭鍐欏叆鍒扮鐩樹笂鐨勬暟鎹枃浠朵腑锛屽苟閲嶆柊寮€濮嬭褰曟柊鐨勫啓鍏ユ搷浣?/p>

undo log 鐨勪綔鐢細

鏀寔浜嬪姟鐨勫洖婊氾細褰撴暟鎹簱鎵ц浜嬪姟鏃讹紝undo log鍙互璁板綍涓嬫墍鏈変慨鏀规搷浣滅殑鍙嶅悜鎿嶄綔锛屼篃灏辨槸鎾ら攢鎿嶄綔銆傚鏋滀簨鍔℃墽琛屽け璐ユ垨鑰呴渶瑕佸洖婊氭椂锛寀ndo log涓殑淇℃伅鍙互琚敤鏉ユ挙閿€鎵€鏈夌殑淇敼鎿嶄綔锛屼娇寰楁暟鎹簱鍥炴粴鍒颁箣鍓嶇殑鐘舵€併€傝繖鏍峰彲浠ョ‘淇濅簨鍔$殑涓€鑷存€у拰瀹屾暣鎬э紝淇濇姢鏁版嵁鐨勫彲闈犳€с€傛敮鎸丮vcc鏈哄埗锛氬湪mvcc锛堝鐗堟湰骞跺彂鎺у埗锛夋満鍒朵腑锛屾暟鎹簱绯荤粺浼氫负姣忎釜浜嬪姟鍒涘缓涓€涓嫭绔嬬殑瑙嗗浘锛岀敤浜庨殧绂诲苟鍙戜簨鍔′箣闂寸殑璇诲啓鎿嶄綔銆傚湪杩欎釜瑙嗗浘涓紝undo log璁板綍浜嗕簨鍔″紑濮嬩箣鍓嶇殑鎵€鏈変慨鏀规搷浣滐紝杩欐牱鍙互淇濊瘉浜嬪姟鍦ㄨ鍙栨暟鎹椂鑾峰彇鍒扮殑鏄箣鍓嶇殑鐗堟湰锛屼粠鑰岄伩鍏嶄簡鑴忚绛夐棶棰樸€? undo log 鎿嶄綔锛?/p>

undo log 閿€姣侊細undo log鍦ㄤ簨鍔℃墽琛屾椂浜х敓锛屼簨鍔℃彁浜ゆ椂锛屽苟涓嶄細绔嬪嵆鍒犻櫎undo log锛屽洜涓鸿繖浜涙棩蹇楀彲鑳借繕鐢ㄤ簬mvcc銆倁ndo log 瀛樺偍锛歶ndo log閲囩敤娈电殑鏂瑰紡杩涜绠$悊鍜岃褰曪紝瀛樻斁鍦ㄥ墠闈粙缁嶇殑 rollback segment鍥炴粴娈典腑锛屽唴閮ㄥ寘鍚?024涓猽ndo log segmenundo log 鐗堟湰閾撅細姣忓綋涓€涓簨鍔¤鎻愪氦鏃讹紝灏变細鐢熸垚涓€涓搴旂殑undo鏃ュ織璁板綍銆傝繖浜泆ndo鏃ュ織璁板綍浠ラ摼鐨勫舰寮忚繛鎺ュ湪涓€璧凤紝褰㈡垚undo log鐗堟湰閾?

6.3.3 mvcc

鏈皬鑺傛垜浠皢瑕佸涔?/p>

璁よ瘑mvcc鏁版嵁搴撶殑涓夌闅愯棌瀛楁readviewmvcc瀹炵幇鍘熺悊

浠€涔堟槸mvcc锛?/p>

mvcc鏄暟鎹簱绯荤粺涓殑涓€绉嶅苟鍙戞帶鍒舵妧鏈紝鍏ㄧо涓篗ulti-version concurrency control锛屽嵆澶氱増鏈苟鍙戞帶鍒讹紝鎸囩淮鎶や竴涓暟鎹殑澶氫釜鐗堟湰锛?浣垮緱璇诲啓鎿嶄綔娌℃湁鍐茬獊锛屽揩鐓ц涓篗ysql瀹炵幇mvcc鎻愪緵浜嗕竴涓潪闃诲璇诲姛鑳姐€侻vcc鐨勫叿浣撳疄鐜帮紝杩橀渶瑕佷緷璧栦簬鏁版嵁搴撹褰曚腑鐨勪笁涓殣寮忓瓧娈点€乽ndo log鏃ュ織銆乺eadview銆?/p>

鍦∕vcc涓紝姣忎釜浜嬪姟鍦ㄥ紑濮嬫椂浼氬垱寤轰竴涓嫭绔嬬殑浜嬪姟瑙嗗浘锛岃瑙嗗浘鍙互鐪嬩綔鏄竴涓暟鎹簱鐨勫揩鐓?锛屽畠璁板綍浜嗗綋鍓嶄簨鍔″紑濮嬫椂鏁版嵁搴撲腑鐨勬墍鏈夋暟鎹€傚綋浜嬪姟闇€瑕佽鍙栨煇涓暟鎹椂锛屼細浠庤浜嬪姟瑙嗗浘涓幏鍙栨暟鎹殑鐗堟湰锛屽苟鎵ц鐩稿簲鐨勬搷浣溿€?/p>

鍦∕vcc涓紝姣忎釜鏁版嵁閮芥湁澶氫釜鐗堟湰锛屾瘡涓増鏈兘鏈変竴涓椂闂存埑锛岀敤浜庢爣璇嗚鐗堟湰鐨勫垱寤烘椂闂淬€傚綋浜嬪姟鎵ц鍐欐搷浣滄椂锛屼細鍒涘缓涓€涓柊鐨勬暟鎹増鏈紝骞跺皢鏂扮増鏈殑鏃堕棿鎴宠缃负褰撳墠鏃堕棿鎴炽€傚綋浜嬪姟鎵ц璇绘搷浣滄椂锛屼細鏍规嵁褰撳墠浜嬪姟鐨勬椂闂存埑鍜屾暟鎹増鏈殑鏃堕棿鎴虫潵閫夋嫨鍚堥€傜殑鏁版嵁鐗堟湰锛屼粠鑰屼繚璇佷簨鍔¤鍙栧埌鐨勬暟鎹槸绗﹀悎瑕佹眰鐨勩€?/p>

mvcc鏈哄埗鍦ㄨ澶氭祦琛岀殑鏁版嵁搴撶郴缁熶腑寰楀埌骞挎硾搴旂敤锛屽postgresql銆丮ysql銆丱racle绛夈€?/p>

mvcc鐨勪綔鐢紵

鏀寔骞跺彂璇诲彇鎿嶄綔锛歁vcc鍏佽澶氫釜浜嬪姟鍚屾椂璇诲彇鏁版嵁搴撶殑鏁版嵁锛屼笉浼氬洜涓鸿鍙栨搷浣滆€屽鏁版嵁鐨勪竴鑷存€ч€犳垚褰卞搷銆傞伩鍏嶈鍙栬剰鏁版嵁锛氬湪mvcc涓紝姣忎釜浜嬪姟璇诲彇鐨勬槸鏁版嵁搴撲腑鐨勪竴涓揩鐓э紙snapshot锛夛紝鑰屼笉鏄疄鏃舵暟鎹€傝繖鏍峰彲浠ラ伩鍏嶈鍙栧埌鍏朵粬浜嬪姟姝e湪淇敼鐨勬暟鎹紝淇濊瘉璇诲彇鐨勬暟鎹槸涓€鑷寸殑銆傛敮鎸佷簨鍔$殑鍥炴粴鎿嶄綔锛氬湪mvcc涓紝姣忎釜浜嬪姟瀵规暟鎹簱鐨勪慨鏀规搷浣滈兘浼氳璁板綍鍦╱ndo log涓紝杩欐牱鍙互鍦ㄤ簨鍔″洖婊氭椂锛屾牴鎹畊ndo log涓殑淇℃伅灏嗘暟鎹仮澶嶅埌淇敼鍓嶇殑鐘舵€併€傛敮鎸佸苟鍙戝啓鍏ユ搷浣滐細鍦∕vcc涓紝姣忎釜浜嬪姟瀵规暟鎹簱鐨勪慨鏀规搷浣滀細鐢熸垚涓€涓柊鐨勭増鏈紙version锛夛紝鑰屼笉鏄洿鎺ヤ慨鏀瑰師濮嬫暟鎹€傝繖鏍峰彲浠ラ伩鍏嶅涓簨鍔″悓鏃朵慨鏀瑰悓涓€浠芥暟鎹椂浜х敓鍐茬獊锛屼粠鑰屾彁楂樺苟鍙戝啓鍏ユ搷浣滅殑鏁堢巼銆傞伩鍏嶉攣绔炰簤锛氬湪mvcc涓紝姣忎釜浜嬪姟瀵规暟鎹簱鐨勪慨鏀规搷浣滈兘涓嶄細瀵瑰叾浠栦簨鍔′骇鐢熼攣绔炰簤鐨勫奖鍝嶏紝涓嶄細鍑虹幇姝婚攣绛夐棶棰樸€? 鐩稿叧姒傚康

褰撳墠璇伙細璇诲彇鐨勬槸璁板綍鐨勬渶鏂扮増鏈紝璇诲彇鏃惰繕瑕佷繚璇佸叾浠栧苟鍙戜簨鍔′笉鑳戒慨鏀瑰綋鍓嶈褰曪紝浼氬璇诲彇鐨勮褰曡繘琛屽姞閿併€傚浜庢垜浠棩甯哥殑鎿嶄綔锛屽锛歴elect ... lock in share mode(鍏变韩閿?锛宻elect ...for update銆乽pdate銆乮nsert銆乨elete(鎺掍粬閿?閮芥槸涓€绉嶅綋鍓嶈蹇収璇伙細绠€鍗曠殑select锛堜笉鍔犻攣锛夊氨鏄揩鐓ц锛屽揩鐓ц锛岃鍙栫殑鏄褰曟暟鎹殑鍙鐗堟湰锛屾湁鍙兘鏄巻鍙叉暟鎹紝涓嶅姞閿侊紝鏄潪闃诲

read committed锛氭瘡娆elect锛岄兘鐢熸垚涓€涓揩鐓ц銆俁epeatable read锛氬紑鍚簨鍔″悗绗竴涓猻elect璇彞鎵嶆槸蹇収璇荤殑鍦版柟銆係erializable锛氬揩鐓ц浼氶€€鍖栦负褰撳墠 闅愯棌瀛楁

mvcc鏈哄埗鐨勫疄鐜扮涓嶅紑mysql鎻愪緵鐨勪笁涓殣钘忓瓧娈碉紝姣忓綋鎴戜滑寤虹珛涓€寮犺〃锛岄兘浼氬嚭鐜颁笁涓垨涓や釜闅愯棌瀛楁锛圖b_row_id鍙湁鍦ㄨ〃娌℃湁鎸囧畾涓婚敭鏃舵墠浼氬嚭鐜帮級

瀹炰緥

涔嬪墠鎴戜滑寤虹珛浜嗗簲璇?mysql_study 鐨勬暟鎹簱锛屾垜浠湪璇ユ暟鎹簱涓缓绔嬩簡涓€寮犲悕涓?student 鐨勮〃锛岃琛ㄥ叿鏈変富閿?id锛岀幇鍦ㄥ氨璁╂垜浠潵鐪嬩竴鐪嬪畠鐨勯殣钘忓瓧娈靛惂

step1锛氳繘鍏ユ敼鍙樼殑鏁版嵁瀛樺偍绌洪棿 cd /var/lib/mysql/mysql_study

step2锛氭煡鐪嬭〃鐨勭粨鏋?ibd2sdi student.ibd

鐗堟湰閾?涓嬮潰鏄竴寮犲師濮嬭〃锛?

db_trx_id : 浠h〃鏈€杩戜慨鏀逛簨鍔d锛岃褰曟彃鍏ヨ繖鏉¤褰曟垨鏈€鍚庝竴娆′慨鏀硅璁板綍鐨勪簨鍔d锛屾槸 鑷鐨? db_roll_ptr 锛?鐢变簬杩欐潯鏁版嵁鏄墠鎻掑叆鐨勶紝娌℃湁琚洿鏂拌繃锛屾墍浠ヨ瀛楁鍊间负null 鐒跺悗锛屾湁鍥涗釜骞跺彂浜嬪姟鍚屾椂鍦ㄨ闂繖寮犺〃 a. 绗竴姝? 褰撲簨鍔?鎵ц绗竴鏉′慨鏀硅鍙ユ椂锛屼細璁板綍undo log鏃ュ織锛岃褰曟暟鎹彉鏇翠箣鍓嶇殑鏍峰瓙; 鐒跺悗鏇存柊璁板綍锛?骞朵笖璁板綍鏈鎿嶄綔鐨勪簨鍔d锛屽洖婊氭寚閽堬紝鍥炴粴鎸囬拡鐢ㄦ潵鎸囧畾濡傛灉鍙戠敓鍥炴粴锛屽洖婊氬埌鍝竴涓増鏈? b.绗簩姝? 褰撲簨鍔?鎵ц绗竴鏉′慨鏀硅鍙ユ椂锛屼篃浼氳褰晆ndo log鏃ュ織锛岃褰曟暟鎹彉鏇翠箣鍓嶇殑鏍峰瓙; 鐒跺悗鏇存柊璁?褰曪紝骞朵笖璁板綍鏈鎿嶄綔鐨勪簨鍔d锛屽洖婊氭寚閽堬紝鍥炴粴鎸囬拡鐢ㄦ潵鎸囧畾濡傛灉鍙戠敓鍥炴粴锛屽洖婊氬埌鍝竴涓増鏈? c. 绗笁姝? 褰撲簨鍔?鎵ц绗竴鏉′慨鏀硅鍙ユ椂锛屼篃浼氳褰晆ndo log鏃ュ織锛岃褰曟暟鎹彉鏇翠箣鍓嶇殑鏍峰瓙; 鐒跺悗鏇存柊璁?褰曪紝骞朵笖璁板綍鏈鎿嶄綔鐨勪簨鍔d锛屽洖婊氭寚閽堬紝鍥炴粴鎸囬拡鐢ㄦ潵鎸囧畾濡傛灉鍙戠敓鍥炴粴锛屽洖婊氬埌鍝竴涓増鏈?

鏈€缁堟垜浠彂鐜帮紝涓嶅悓浜嬪姟鎴栫浉鍚屼簨鍔″鍚屼竴鏉¤褰曡繘琛屼慨鏀癸紝浼氬鑷磋璁板綍鐨剈ndolog鐢熸垚涓€鏉?璁板綍鐗堟湰閾捐〃锛岄摼琛ㄧ殑澶撮儴鏄渶鏂扮殑鏃ц褰曪紝閾捐〃灏鹃儴鏄渶鏃╃殑鏃ц褰?/p>

readview

readview锛堣瑙嗗浘锛夋槸 蹇収璇?sql鎵ц鏃禡vcc鎻愬彇鏁版嵁鐨勪緷鎹紝璁板綍骞剁淮鎶ょ郴缁熷綋鍓嶆椿璺冪殑浜嬪姟 锛堟湭鎻愪氦鐨勶級id銆?/p>

readview涓寘鍚簡鍥涗釜鏍稿績瀛楁锛? 鑰屽湪readview涓氨瑙勫畾浜嗙増鏈摼鏁版嵁鐨勮闂鍒欙紝 trx_id 浠h〃褰撳墠undolog鐗堟湰閾惧搴斾簨鍔?id 涓嶅悓鐨勯殧绂荤骇鍒紝鐢熸垚readview鐨勬椂鏈轰笉鍚岋細

read committed (璇诲凡鎻愪氦)锛氬湪浜嬪姟涓瘡涓€娆℃墽琛屽揩鐓ц鏃剁敓鎴怰eadviewrepeatable read锛堝彲閲嶅璇伙級锛氫粎鍦ㄤ簨鍔′腑绗竴娆℃墽琛屽揩鐓ц鏃剁敓鎴怰eadview锛屽悗缁鐢ㄨreadview銆? mvcc瀹炵幇鍘熺悊

rc闅旂绾у埆涓嬶紝鍦ㄤ簨鍔′腑姣忎竴娆℃墽琛屽揩鐓ц鏃剁敓鎴怰eadview 鎴戜滑灏辨潵鍒嗘瀽浜嬪姟5涓紝涓ゆ蹇収璇昏鍙栨暟鎹紝鏄浣曡幏鍙栨暟鎹殑? 鍦ㄤ簨鍔?涓紝鏌ヨ浜嗕袱娆d涓?0鐨勮褰曪紝鐢变簬闅旂绾у埆涓篟ead committed锛屾墍浠ユ瘡涓€娆¤繘琛屽揩鐓ц 閮戒細鐢熸垚涓€涓猂eadview锛岄偅涔堜袱娆$敓鎴愮殑readview濡備笅锛? 閭d箞杩欎袱娆″揩鐓ц鍦ㄨ幏鍙栨暟鎹椂锛屽氨闇€瑕佹牴鎹墍鐢熸垚鐨凴eadview浠ュ強readview鐨勭増鏈摼璁块棶瑙勫垯锛?鍒皍ndolog鐗堟湰閾句腑鍖归厤鏁版嵁锛屾渶缁堝喅瀹氭娆″揩鐓ц杩斿洖鐨勬暟鎹?a. 鍏堟潵鐪嬬涓€娆″揩鐓ц鍏蜂綋鐨勮鍙栬繃绋? 鍦ㄨ繘琛屽尮閰嶆椂锛屼細浠巙ndo log鐨勭増鏈摼锛屼粠涓婂埌涓嬭繘琛屾尐涓尮閰嶏細 1锛夊厛鍖归厤杩欐潯璁板綍锛岃繖鏉¤褰曞搴旂殑 trx_id涓?锛屼篃灏辨槸灏?甯﹀叆鍙充晶鐨勫尮閰嶈鍒欎腑銆?鈶犱笉婊¤冻 鈶′笉婊¤冻 鈶笉婊¤冻 鈶d篃涓嶆弧瓒?锛?閮戒笉婊¤冻锛屽垯缁х画鍖归厤undo log鐗堟湰閾剧殑涓嬩竴鏉?2锛夊啀鍖归厤绗簩鏉★紝杩欐潯 璁板綍瀵瑰簲鐨則rx_id涓?锛屼篃灏辨槸灏?甯﹀叆鍙充晶鐨勫尮閰嶈鍒欎腑銆傗憼涓嶆弧瓒?鈶′笉婊¤冻 鈶笉婊¤冻 鈶d篃 涓嶆弧瓒?锛岄兘涓嶆弧瓒筹紝鍒欑户缁尮閰島ndo log鐗堟湰閾剧殑涓嬩竴鏉?3锛夊啀鍖归厤绗笁鏉¤繖鏉¤ 褰曞搴旂殑trx_id涓?锛屼篃灏辨槸灏?甯﹀叆鍙充晶鐨勫尮閰嶈鍒欎腑銆傗憼涓嶆弧瓒?鈶℃弧瓒?缁堟鍖归厤锛屾娆″揩鐓?璇伙紝杩斿洖鐨勬暟鎹氨鏄増鏈摼涓褰曠殑杩欐潯鏁版嵁 b. 鍐嶆潵鐪嬬浜屾蹇収璇诲叿浣撶殑璇诲彇杩囩▼: 鍦ㄨ繘琛屽尮閰嶆椂锛屼細浠巙ndo log鐨勭増鏈摼锛屼粠涓婂埌涓嬭繘琛屾尐涓尮閰嶏細 1锛夊厛鍖归厤杩欐潯璁板綍锛岃繖鏉¤褰曞搴旂殑 trx_id涓?锛屼篃灏辨槸灏?甯﹀叆鍙充晶鐨勫尮閰嶈鍒欎腑銆?鈶犱笉婊¤冻 鈶′笉婊¤冻 鈶笉婊¤冻 鈶d篃涓嶆弧瓒?锛?閮戒笉婊¤冻锛屽垯缁х画鍖归厤undo log鐗堟湰閾剧殑涓嬩竴鏉?2锛夊啀鍖归厤绗簩鏉¤繖鏉?璁板綍瀵瑰簲鐨則rx_id涓?锛屼篃灏辨槸灏?甯﹀叆鍙充晶鐨勫尮閰嶈鍒欎腑銆傗憼涓嶆弧瓒?鈶℃弧瓒?銆傜粓姝㈠尮閰嶏紝姝ゆ 蹇収璇伙紝杩斿洖鐨勬暟鎹氨鏄増鏈摼涓褰曠殑杩欐潯鏁版嵁 rr闅旂绾у埆涓嬶紝浠呭湪浜嬪姟涓涓€娆℃墽琛屽揩鐓ц鏃剁敓鎴怰eadview锛屽悗缁鐢ㄨreadview銆?鑰孯r 鏄彲 閲嶅璇伙紝鍦ㄤ竴涓簨鍔′腑锛屾墽琛屼袱娆$浉鍚岀殑select璇彞锛屾煡璇㈠埌鐨勭粨鏋滄槸涓€鏍风殑锛岄偅mysql鏄浣曞仛鍒板彲閲嶅璇荤殑鍛? 鎴戜滑绠€鍗曞垎鏋愪竴涓嬪氨鐭ラ亾浜? 鎴戜滑鐪嬪埌锛屽湪rr闅旂绾у埆涓嬶紝鍙槸鍦ㄤ簨鍔′腑绗竴娆″揩鐓ц鏃剁敓鎴怰eadview锛屽悗缁兘鏄鐢ㄨ readview锛岄偅涔堟棦鐒禦eadview閮戒竴鏍凤紝 readview鐨勭増鏈摼鍖归厤瑙勫垯涔熶竴鏍凤紝 閭d箞鏈€缁堝揩鐓ц杩?鍥炵殑缁撴灉涔熸槸涓€鏍风殑

鎵€浠ュ憿锛孧vcc鐨勫疄鐜板師鐞嗗氨鏄€氳繃 innodb琛ㄧ殑闅愯棌瀛楁銆乁ndolog 鐗堟湰閾俱€丷eadview鏉ュ疄鐜扮殑銆?鑰孧vcc 閿侊紝鍒欏疄鐜颁簡浜嬪姟鐨勯殧绂绘€с€?鑰屼竴鑷存€у垯鏄敱redolog 涓?undolog淇濊瘉

7銆丮ysql绠$悊

7.1 mysql绯荤粺鏁版嵁搴?/p>

mysql鏁版嵁搴撳畨瑁呭畬鎴愬悗锛岃嚜甯︿簡涓€涓嬪洓涓暟鎹簱锛屽叿浣撲綔鐢ㄥ涓嬶細

7.2 mysql甯哥敤宸ュ叿

鏈皬鑺傛垜浠皢瑕佸涔燤ysql甯哥敤鐨勫嚑绉嶅伐鍏?/p>

mysqlmysqladminmysqlbinlogmysqlshowmysqldumpmysqlimport/source

mysql 璇ysql骞跺苟涓嶆槸鎸嘙ysql鏈嶅姟锛岃€屾槸鎸嘙ysql瀹㈡埛绔?璇硶:

mysql [options] [database]

閫夐」:

-u, --user=name #鎸囧畾鐢ㄦ埛鍚?/p>

-p, --password[=name] #鎸囧畾瀵嗙爜

-h, --host=name #鎸囧畾鏈嶅姟鍣↖p鎴栧煙鍚?/p>

-p, --port=port #鎸囧畾杩炴帴绔彛

-e, --execute=name #鎵цsql璇彞骞堕€€鍑?/p>

-e閫夐」鍙互鍦∕ysql瀹㈡埛绔墽琛孲ql璇彞锛岃€屼笉鐢ㄨ繛鎺ュ埌mysql鏁版嵁搴撳啀鎵ц锛屽浜庝竴浜涙壒澶勭悊鑴氭湰锛?杩欑鏂瑰紡灏ゅ叾鏂逛究 绀轰緥锛?# 鏌ヨdb01鏁版嵁搴撲腑鐨剆tu琛?/p>

mysql -h127.0.0.1 -p3306 -uroot 鈥損123456 db01 -e "select * from stu";

mysqladmin mysqladmin 鏄竴涓墽琛岀鐞嗘搷浣滅殑瀹㈡埛绔▼搴忋€傚彲浠ョ敤瀹冩潵妫€鏌ユ湇鍔″櫒鐨勯厤缃拰褰撳墠鐘舵€併€佸垱寤哄苟鍒犻櫎鏁版嵁搴撶瓑 璇硶:

mysqladmin [options] command ...

閫夐」:

-u, --user=name #鎸囧畾鐢ㄦ埛鍚?/p>

-p, --password[=name] #鎸囧畾瀵嗙爜

-h, --host=name #鎸囧畾鏈嶅姟鍣↖p鎴栧煙鍚?/p>

-p, --port=port #鎸囧畾杩炴帴绔彛

绀轰緥锛?# 鍒犻櫎鏁版嵁搴搕est01

mysqladmin -uroot 鈥損1234 drop 'test01';

# 鏌ョ湅mysql鐗堟湰

mysqladmin -uroot 鈥損1234 version;

mysqlbinlog 鐢变簬鏈嶅姟鍣ㄧ敓鎴愮殑浜岃繘鍒舵棩蹇楁枃浠朵互浜岃繘鍒舵牸寮忎繚瀛橈紝鎵€浠ュ鏋滄兂瑕佹鏌ヨ繖浜涙枃鏈殑鏂囨湰鏍煎紡锛屽氨浼氫娇 鐢ㄥ埌mysqlbinlog 鏃ュ織绠$悊宸ュ叿 璇硶:

mysqlbinlog [options] log-files1 log-files2 ...

閫夐」:

-d, --database=name 鎸囧畾鏁版嵁搴撳悕绉帮紝鍙垪鍑烘寚瀹氱殑鏁版嵁搴撶浉鍏虫搷浣溿€?/p>

-o, --offset=# 蹇界暐鎺夋棩蹇椾腑鐨勫墠n琛屽懡浠ゃ€?/p>

-r,--result-file=name 灏嗚緭鍑虹殑鏂囨湰鏍煎紡鏃ュ織杈撳嚭鍒版寚瀹氭枃浠躲€?/p>

-s, --short-form 鏄剧ず绠€鍗曟牸寮忥紝 鐪佺暐鎺変竴浜涗俊鎭€?/p>

--start-datatime=date1 --stop-datetime=date2 鎸囧畾鏃ユ湡闂撮殧鍐呯殑鎵€鏈夋棩蹇椼€?/p>

--start-position=pos1 --stop-position=pos2 鎸囧畾浣嶇疆闂撮殧鍐呯殑鎵€鏈夋棩蹇椼€?/p>

绀轰緥锛?# 鏌ョ湅浜岃繘鍒舵棩蹇楁枃浠讹紙濡傛灉浣跨敤cat鏌ョ湅锛屼細鍑虹幇涔辩爜锛?/p>

mysqlbinlog mysql-bin.000101

mysqlshow mysqlshow 瀹㈡埛绔璞℃煡鎵惧伐鍏凤紝鐢ㄦ潵寰堝揩鍦版煡鎵惧瓨鍦ㄥ摢浜涙暟鎹簱銆佹暟鎹簱涓殑琛ㄣ€佽〃涓殑鍒楁垨鑰呯储 寮?璇硶:

mysqlshow [options] [db_name [table_name [col_name]]]

閫夐」:

--count 鏄剧ず鏁版嵁搴撳強琛ㄧ殑缁熻淇℃伅锛堟暟鎹簱锛岃〃 鍧囧彲浠ヤ笉鎸囧畾锛?/p>

-i 鏄剧ず鎸囧畾鏁版嵁搴撴垨鑰呮寚瀹氳〃鐨勭姸鎬佷俊鎭?/p>

绀轰緥锛?# 鏌ヨtest搴撲腑姣忎釜琛ㄤ腑鐨勫瓧娈垫暟锛屽強琛屾暟

mysqlshow -uroot -p2143 test --count

# 鏌ヨtest搴撲腑book琛ㄧ殑璇︾粏鎯呭喌

mysqlshow -uroot -p2143 test book --count

mysqldump mysqldump 瀹㈡埛绔伐鍏风敤鏉ュ浠芥暟鎹簱鎴栧湪涓嶅悓鏁版嵁搴撲箣闂磋繘琛屾暟鎹縼绉汇€傚浠藉唴瀹瑰寘鍚垱寤鸿〃锛屽強 鎻掑叆琛ㄧ殑sql璇彞 璇硶 锛?/p>

mysqldump [options] db_name [tables]

mysqldump [options] --database/-b db1 [db2 db3...]

mysqldump [options] --all-databases/-a

杩炴帴閫夐」 锛?/p>

-u, --user=name 鎸囧畾鐢ㄦ埛鍚?/p>

-p, --password[=name] 鎸囧畾瀵嗙爜

-h, --host=name 鎸囧畾鏈嶅姟鍣╥p鎴栧煙鍚?/p>

-p, --port=# 鎸囧畾杩炴帴绔彛

杈撳嚭閫夐」锛?/p>

--add-drop-database 鍦ㄦ瘡涓暟鎹簱鍒涘缓璇彞鍓嶅姞涓?drop database 璇彞

--add-drop-table 鍦ㄦ瘡涓〃鍒涘缓璇彞鍓嶅姞涓?drop table 璇彞 , 榛樿寮€鍚?; 涓嶅紑鍚?(--skip-add-drop-table)

-n, --no-create-db 涓嶅寘鍚暟鎹簱鐨勫垱寤鸿鍙?/p>

-t, --no-create-info 涓嶅寘鍚暟鎹〃鐨勫垱寤鸿鍙?/p>

-d --no-data 涓嶅寘鍚暟鎹?/p>

-t, --tab=name 鑷姩鐢熸垚涓や釜鏂囦欢锛氫竴涓?sql鏂囦欢锛屽垱寤鸿〃缁撴瀯鐨勮鍙ワ紱涓€涓?txt鏂囦欢锛屾暟鎹枃浠?

绀轰緥锛?# 澶囦唤db01鏁版嵁搴?灏嗘暟鎹簱db01涓殑鏁版嵁鍜岀粨鏋勬嫹璐濆埌db01.sql鏂囦欢)

mysqldump -uroot -p1234 db01 > db01.sql # 鍚玠rop銆乧reate銆乮nsert璇彞

# 澶囦唤db01鏁版嵁搴撲腑鐨勮〃鏁版嵁锛屼笉澶囦唤琛ㄧ粨鏋?-t)

mysqldump -uroot -p1234 -t db01 > db01.sql # 鍙惈insert璇彞

# 灏哾b01鏁版嵁搴撶殑琛ㄧ殑琛ㄧ粨鏋勪笌鏁版嵁鍒嗗紑澶囦唤(-t)

mysqldump -uroot -p1234 -t /root db01 score

娉ㄦ剰锛氱涓変釜绀轰緥浼氱洿鎺ユ姤涓€涓敊璇紝鍥犱负/root鐩綍涓嶆槸mysql瀹樻柟鎸囧畾鐨勭洰褰曪紙mysql璁や负root鐩綍涓嶅畨鍏級锛屾墍浠ラ渶瑕佸皢琚浠界殑鏂囦欢鏀惧埌mysql鎺ㄨ崘鐨勭洰褰?var/lib/mysql-files锛岃鐩綍鍙互閫氳繃show variables like '%secure_file_priv%';杩涜鏌ョ湅 mysqlimport/source

mysqlimport mysqlimport 鏄鎴风鏁版嵁瀵煎叆宸ュ叿锛岀敤鏉ュ鍏ysqldump 鍔?-t 鍙傛暟鍚庡鍑虹殑鏂囨湰鏂囦欢 璇硶:

mysqlimport [options] db_name textfile1 [textfile2...]

绀轰緥锛?# 灏哻ity.txt鏂囦欢涓殑鏁版嵁瀵煎叆鍒皌est琛ㄤ腑

mysqlimport -uroot -p2143 test /tmp/city.txt

source 濡傛灉闇€瑕佸鍏ql鏂囦欢,鍙互浣跨敤mysql涓殑source 鎸囦护 璇硶:

source /root/xxxxx.sql

鎬荤粨

璇︽儏瑙?mysql杩涢樁绡?xmind

鐩稿叧闈㈣瘯棰?/p>

涓轰粈涔堜笉鐢╯elect * 鑰屾槸鏄庣‘鎸囧嚭鍒楀悕锛?/p>

鏁堢巼浣庝笅锛氬鏋?select * 浠庤〃涓绱㈡墍鏈夊垪锛屽垯鏁版嵁搴撴绱㈡墍鏈夊垪涓殑鏁版嵁銆傝繖浼氬鑷存暟鎹簱璇诲彇鍜屼紶杈撳浣欑殑鏁版嵁锛岃繖鍙兘浼氬鍔犳暟鎹簱鐨勫搷搴旀椂闂? 鍐呭瓨浣跨敤锛氬鏋滆〃涓湁澶氫釜澶х殑鍒楋紝鍒欎娇鐢?select * 鍙兘浼氬鑷存€ц兘闂锛屽洜涓哄畠灏嗕粠纾佺洏璇诲彇澶ч噺鏁版嵁鍒板唴瀛樹腑 姝т箟鎬э細select * 涔熷甫鏉ヤ簡妯$硦鎬э紝鍥犱负瀹冧笉鏄剧ず琛ㄤ腑鐨勫垪鍏蜂綋鏄摢浜涖€傝繖寰堝鏄撳鑷村紑鍙戜汉鍛樺湪鍚庣画鐨勫紑鍙戝拰缁存姢涓嚭鐜伴棶棰? 缁存姢鎬э細濡傛灉琛ㄧ殑鍒楀彂鐢熷彉鍖栵紝渚嬪鍒楀悕鏇存敼鎴栧垹闄ゅ垪锛屽垯鍙兘闇€瑕佷慨鏀瑰湪搴旂敤绋嬪簭涓娇鐢⊿elect *鐨勬墍鏈夋煡璇紝骞朵笖鍙兘浼氬鑷村叾浠栭棶棰? inonodb寮曟搸涓嶮yisam寮曟搸鐨勫尯鍒湁鍝簺锛?璇︽儏鐪嬪墠闈?/p>

innodbi寮曟搸锛屾敮鎸佷簨鍔★紝鑰孧yisam涓嶆敮鎸併€侷nnodbi寮曟搸锛屾敮鎸佽閿佸拰琛ㄩ攣锛岃€孧yisam浠呮敮鎸佽〃閿侊紝涓嶆敮鎸佽閿併€侷nnodbi寮曟搸锛屾敮鎸佸閿紝鑰孧yisam鏄笉鏀寔鐨勩€? 涓轰粈涔圛nnodb瀛樺偍寮曟搸閫夋嫨浣跨敤b tree绱㈠紩缁撴瀯? 璇︽儏鐪嬪墠闈?/p>

鐩稿浜庝簩鍒嗘悳绱㈡爲锛屽眰绾ф洿灏戯紝鎼滅储鏁堢巼楂樼浉瀵逛簬绾㈤粦鏍戯紝鍦ㄥ瓨鍌ㄥぇ閲忔暟鎹椂锛屾煡鎵炬晥鐜囨洿楂樺浜嶣-tree锛屾棤璁烘槸鍙跺瓙鑺傜偣杩樻槸闈炲彾瀛愯妭鐐癸紝閮戒細淇濆瓨鏁版嵁锛岃繖鏍峰鑷翠竴椤典腑瀛樺偍鐨勯敭鍊煎噺灏戯紝鎸囬拡璺熺潃鍑忓皯锛岃鍚屾牱淇濆瓨澶ч噺鏁版嵁锛屽彧鑳藉鍔犳爲鐨勯珮搴︼紝瀵艰嚧鎬ц兘闄嶄綆鐩稿hash绱㈠紩锛孊 tree鏀寔鑼冨洿鍖归厤鍙婃帓搴忔搷浣? 浠ヤ笅涓ゆ潯sql璇彞锛岄偅涓墽琛屾晥鐜囬珮锛熶负浠€涔堬紵 璇︽儏鐪嬪墠闈?a. select * from user where id = 10 b. select * from user where name = arm; 澶囨敞锛歩d涓轰富閿紝name瀛楁鍒涘缓鐨勬湁绱㈠紩 绛旀锛欰 璇彞鐨勬墽琛屾€ц兘瑕侀珮浜嶣 璇彞銆?鍥犱负a璇彞鐩存帴璧拌仛闆嗙储寮曪紝鐩存帴杩斿洖鏁版嵁銆?鑰孊璇彞闇€瑕佸厛鏌ヨname瀛楁鐨勪簩绾х储寮曪紝鐒?鍚庡啀鏌ヨ鑱氶泦绱㈠紩锛屼篃灏辨槸闇€瑕佽繘琛屽洖琛ㄦ煡璇€? innodb涓婚敭绱㈠紩鐨凚 tree楂樺害涓簄鏃舵渶澶ц兘瀛樺偍澶氬ぇ鐨勬暟鎹噺? 鍋囪: 涓€琛屾暟鎹ぇ灏忎负 1k锛屼竴椤典腑鍙互瀛樺偍 16琛?杩欐牱鐨勬暟鎹€侷nnodb 鐨勬寚閽堝崰鐢?6涓瓧鑺?鐨勭┖ 闂达紝涓婚敭鍗充娇涓?bigint锛屽崰鐢ㄥ瓧鑺傛暟涓?8銆?楂樺害涓?2锛? n * 8 (n 1) * 6 = 161024$ , 绠楀嚭 n 绾︿负 $1170 $涓妭鐐癸紝鎸囬拡涓鸿妭鐐规暟 1锛屾墍浠ヨ兘瀛樺偍鐨勮褰曚负$1171 16 = 18736 $锛屼篃灏辨槸璇达紝濡傛灉鏍戠殑楂樺害涓?2锛屽垯鍙互瀛樺偍 18000kb 宸﹀彸鐨勮褰曘€?楂樺害涓?3锛? 1171 * 1171 * 16 = 21939856$ 涔熷氨鏄锛屽鏋滄爲鐨勯珮搴︿负3锛屽垯鍙互瀛樺偍 2200wkb 宸﹀彸鐨勮褰曘€?澶囨敞锛?/p>

b 鏍戜腑锛屾寚閽堢殑鏁伴噺=鑺傜偣鐨勬暟閲?1

涓婚敭涓暟

鈭?

涓婚敭瀛楄妭鏁?

鎸囬拡鏁伴噺

鈭?

鎸囬拡鍗犵敤瀛楄妭鏁?

=

椤电殑澶у皬

涓婚敭涓暟*涓婚敭瀛楄妭鏁?鎸囬拡鏁伴噺*鎸囬拡鍗犵敤瀛楄妭鏁?椤电殑澶у皬

涓婚敭涓暟鈭椾富閿瓧鑺傛暟 鎸囬拡鏁伴噺鈭楁寚閽堝崰鐢ㄥ瓧鑺傛暟=椤电殑澶у皬

椤电殑澶у皬

=

椤电殑琛屾暟

鈭?

姣忚鐨勬暟鎹ぇ灏?

椤电殑澶у皬=椤电殑琛屾暟*姣忚鐨勬暟鎹ぇ灏?

椤电殑澶у皬=椤电殑琛屾暟鈭楁瘡琛岀殑鏁版嵁澶у皬楂樺害涓?鐨凚 鏍戠殑澶у皬锛岀洿鎺ョ瓑浜庨珮搴︿负

楂樺害涓?

2

鑳藉瓨鍌ㄧ殑鏈€澶ф暟鎹噺

鈭?

鏍硅妭鐐圭殑鎸囬拡鏍?

楂樺害涓?鑳藉瓨鍌ㄧ殑鏈€澶ф暟鎹噺*鏍硅妭鐐圭殑鎸囬拡鏍?

楂樺害涓?鑳藉瓨鍌ㄧ殑鏈€澶ф暟鎹噺鈭楁牴鑺傜偣鐨勬寚閽堟爲楂樺害涓簄鐨凚 鏍戠殑澶у皬锛?

楂樺害涓?

2

鑳藉瓨鍌ㄧ殑鏈€澶ф暟閲忛噺

鈭?

鏍硅妭鐐圭殑鎸囬拡

鏁?

n

鈭?

2

楂樺害涓?鑳藉瓨鍌ㄧ殑鏈€澶ф暟閲忛噺*鏍硅妭鐐圭殑鎸囬拡鏁癪{n-2}

楂樺害涓?鑳藉瓨鍌ㄧ殑鏈€澶ф暟閲忛噺鈭楁牴鑺傜偣鐨勬寚閽堟暟n鈭? 涓€寮犺〃, 鏈夊洓涓瓧娈?(id, username, password, status), 鐢变簬鏁版嵁閲忓ぇ, 闇€瑕佸 浠ヤ笅sql璇彞杩涜浼樺寲, 璇ュ浣曡繘琛屾墠鏄渶浼樻柟妗? select id,username,password from tb_user where username = 'ghp';

璇︽儏鐪嬪墠闈?绛旀锛氶拡瀵逛簬 username, password寤虹珛鑱斿悎绱㈠紩, sql涓? create index idx_user_name_pass on tb_user(username,password); 杩欐牱鍙互閬垮厤涓婅堪鐨凷ql璇彞锛屽湪鏌ヨ鐨勮繃绋嬩腑锛屽嚭鐜板洖琛ㄦ煡璇€?

鍙傝€冭祫鏂欙細

mysql 8.0 鍙傝€冩墜鍐?mysql闈㈣瘯锛氳皥璋堜綘瀵硅仛绨囩储寮曠殑鐞嗚в oceanstar鐨勫涔犵瑪璁扮殑鍗氬-csdn鍗氬

merge_threshold锛氬悎骞堕〉鐨勯槇鍊硷紝鍙互鑷繁璁剧疆锛屽湪鍒涘缓琛ㄦ垨鑰呭垱寤虹储寮曟椂鎸囧畾 鈫╋笌 鍏冩暟鎹細鏄寚鎻忚堪鏁版嵁鐨勬暟鎹紝涔熷氨鏄暟鎹殑瀹氫箟淇℃伅銆傚湪鏁版嵁搴撲腑锛屽厓鏁版嵁鍖呮嫭鏁版嵁搴撶殑缁撴瀯銆佽〃鐨勭粨鏋勩€佸垪鐨勭被鍨嬨€佺储寮曘€佽е鍙戝櫒绛変俊鎭€?鈫╋笌 蹇収锛圫napshot锛夛細閫氬父鎸囩郴缁熸垨鑰呮暟鎹殑鏌愪釜鏃堕棿鐐圭殑鐘舵€佺殑鍓湰 鈫╋笌

鑻规灉14鏀寔ip68绾ч槻姘达紝閲囩敤鍧氬浐绋嬪害杩滆秴鐜荤拑闈㈡澘鐨勮秴鐡锋櫠闈㈡澘锛屽湪鏈€娣?绫崇殑姘翠笅鍋滅暀鏃堕棿鏈€闀垮彲杈?0鍒嗛挓銆?澶栬灏哄iphone14銆乮phone14plus鍏?绉嶉厤鑹诧紝鍒嗗埆涓哄崍澶滆壊銆佺传鑹层€佹槦鍏夎壊銆佺孩鑹层€佽摑鑹层€傝秴鐡锋櫠闈㈡澘锛岀幓鐠冭儗鏉挎惌閰嶉摑閲戝睘杈规銆?iphone14pro銆乮phone14pro max鏈夋繁绌洪粦鑹层€侀摱鑹层€侀噾鑹层€佹殫绱壊鍥涚閰嶈壊锛岃秴鐡锋櫠闈㈡澘锛屽搼鍏夎川鎰熺幓鐠冨悗鐩栨惌閰嶄笉閿堥挗杈规銆?iphone14:146.7mmx71.5mmx7.8mm锛岄噸閲?72giphone14plus:160.8mmx78.1mmx7.8mm锛岄噸閲?03giphone14pro:147.5mmx71.5mmx7.85mm锛岄噸閲?06g銆?/p>

iphone14绯诲垪鎵嬫満鐖嗛檷1200杩欐椿鍔ㄥお缁欏姏浜?http://iphone.adiannao.cn/2 iphone14promax:160.7mmx77.6mmx7.85mm锛岄噸閲?40gcpu澶勭悊鍣╥phone14銆乮phone14plus缁х画鎼浇涓婁唬婊¤鐗圓15浠跨敓鑺墖锛?nm宸ヨ壓iphone14pro銆乮phone14pro max鎼浇鑻规灉鏈€鏂扮殑銆?/p>

a16澶勭悊鍣紝閲囩敤4nm宸ヨ壓锛孋pu渚濈劧鏄?鏍镐腑澶鐞嗗櫒锛?涓€ц兘鏍稿績 4涓兘鏁堟牳蹇冿級锛?鏍窯pu锛?6鏍哥缁忕綉缁滃紩鎿庯紝浣嗘櫠浣撶鏁伴噺杈惧埌160浜夸釜锛屾瘮a15澧炲姞6.6%锛屽姛鑰楅檷浣庝簡20%銆?/p>

鍐呭瓨閰嶇疆iphone14绯诲垪绉诲姩鍐呭瓨鍏ㄧ郴6gb锛宨phone14銆乮phone14plus涓篖pddr4x锛涙惌閰?28g/256g/512g鏈鸿韩瀛樺偍iphone14pro鍜宨phone14pro max涓恒€?/p>

lpddr5锛涙惌閰?28g/256g/512g/1t鏈鸿韩瀛樺偍灞忓箷鏄剧ずiphone 14绯诲垪灞忓箷鍧囦负oled 鏄剧ず灞忥紝瓒呰缃戣啘xdr锛屽姣斿害2000000:1锛孒dr 鏄剧ず锛屽師褰╂樉绀猴紝骞胯壊鍩?(p3)锛岃е鎰熻е鎺э紝閲囩敤闃叉补娓嶉槻鎸囩汗娑傚眰锛屾敮鎸佸绉嶈瑷€鏂囧瓧鍚屾椂鏄剧ず銆?/p>

iphone 14 灞忓箷涓?6.1 鑻卞锛屽儚绱犲垎杈ㄧ巼涓?2532x1170锛?60ppiiphone 14 plus灞忓箷 6.7鑻卞锛屽儚绱犲垎杈ㄧ巼涓?2778x1284锛?58 ppiiphone 14鍜宨phone 14 plus灞忓箷涓哄垬娴峰睆锛屽埛鏂扮巼60hz锛?00 nits鏈€澶т寒搴?(鍏稿瀷)锛?200 nits宄板€间寒搴?(hdr)銆?/p>

iphone 14 pro灞忓箷涓?6.1 鑻卞锛屽儚绱犲垎杈ㄧ巼涓?2556x1179锛?60ppiiphone 14 promax灞忓箷 6.7鑻卞锛屽儚绱犲垎杈ㄧ巼涓?2796x1290锛?60ppiiphone14pro鍜宨phone14pro max灞忓箷鍗囩骇涓衡€滆嵂涓稿睆鈥濄€?/p>

鐏靛姩宀涳紝鍏ㄥぉ鍊欐樉绀猴紝promotion 鑷€傚簲鍒锋柊鐜囨妧鏈紝鏈€楂樺彲杈?120hz锛?000 灏肩壒鏈€澶т寒搴?(鍏稿瀷)锛?600 灏肩壒宄板€间寒搴?(hdr)锛?000 灏肩壒宄板€间寒搴?(鎴峰)銆?/p>

鎹粙缁嶏紝鈥滅伒鍔ㄥ矝鈥濆彲浠ユ牴鎹笉鍚屽簲鐢ㄣ€佷氦浜掑憟鐜颁笉鍚岀殑鐘舵€侀厤鍚坕os 16鐨勯攣灞忛€氱煡锛岀伒鍔ㄥ矝鍙互鎻愪緵鏇寸粏鍖栫殑浜や簰姣斿闊充箰鐨勪笓杈戙€佺悆璧涜繘绋嬬瓑绛夐兘鍙互鏄剧ず鍦ㄧ伒鍔ㄥ矝涓婃憚鍍忕郴缁焛phone14鍏ㄧ郴鍓嶇疆1200涓囧師娣辨劅鎽勫儚澶达紝茠/1.9 鍏夊湀銆丗ocus pixels 鑷姩瀵圭劍銆佸叚闀滃紡闀滃ご銆佽缃戣啘灞忛棯鍏夌伅銆?k 瑙嗛鎷嶆憚銆?/p>

iphone14鍜宨phone14plus鍚庣疆涓?200涓囧弻鎽?200 涓囧儚绱犱富鎽勶細26 姣背鐒﹁窛锛屍?1.5 鍏夊湀锛屼紶鎰熷櫒浣嶇щ寮忓厜瀛﹀浘鍍忛槻鎶栧姛鑳斤紝涓冮暅寮忛暅澶达紝100% focus pixels1200 涓囧儚绱犺秴骞胯锛?3 姣背鐒﹁窛锛屍?2.4 鍏夊湀鍜?120掳 瑙嗚锛屼簲闀滃紡闀滃ご銆?2 鍊嶅厜瀛﹀彉鐒?(缂╁皬)锛涙渶楂樺彲杈?5 鍊嶆暟鐮佸彉鐒︺€侾ro 绾ф憚鍍忓ご鍚庣疆鍗囩骇涓轰笁鎽勶紝4800涓囦富鎽?1200涓囪秴骞胯 1200涓囬暱鐒﹂暅澶淬€?/p>

4800 涓囧儚绱犱富鎽勶細24 姣背鐒﹁窛锛屍?1.78 鍏夊湀锛岀浜屼唬浼犳劅鍣ㄤ綅绉诲紡鍏夊鍥惧儚闃叉姈鍔熻兘锛屼竷闀滃紡闀滃ご锛?00% focus pixels1200 涓囧儚绱犺秴骞胯锛?3 姣背鐒﹁窛锛屍?2.2 鍏夊湀鍜?120掳 瑙嗚锛屽叚闀滃紡闀滃ご锛?00% focus pixels銆?/p>

1200 涓囧儚绱?2 鍊嶉暱鐒?(閫氳繃鍥涘悎涓€鍍忕礌浼犳劅鍣ㄥ疄鐜?锛?8 姣背鐒﹁窛锛屍?1.78 鍏夊湀锛岀浜屼唬浼犳劅鍣ㄤ綅绉诲紡鍏夊鍥惧儚闃叉姈鍔熻兘锛屼竷闀滃紡闀滃ご锛?00% focus pixels1200 涓囧儚绱?3 鍊嶉暱鐒︼細77 姣背鐒﹁窛锛屍?2.8 鍏夊湀锛屽厜瀛﹀浘鍍忛槻鎶栧姛鑳斤紝鍏暅寮忛暅澶淬€?3 鍊嶅厜瀛﹀彉鐒?(鏀惧ぇ)锛? 鍊嶅厜瀛﹀彉鐒?(缂╁皬)锛? 鍊嶅厜瀛﹀彉鐒﹁寖鍥达紱鏈€楂樺彲杈?15 鍊嶆暟鐮佸彉鐒﹀叏绯昏摑瀹濈煶鐜荤拑闀滃ご琛ㄩ潰銆佸師褰╅棯鍏夌伅銆佸厜鍍忓紩鎿庛€佹繁搴﹁瀺鍚堟妧鏈€佹櫤鑳?hdr 4銆侀暅澶寸暩鍙樻牎姝?(瓒呭箍瑙?銆佸厛杩涚殑绾㈢溂鏍℃鍔熻兘銆佽嚜鍔ㄥ浘鍍忛槻鎶栧姛鑳姐€佽繛鎷嶅揩鐓фā寮忕瓑銆?/p>

缁埅鍏呯數iphone 14鐢垫睜3279姣畨銆乮phone 14 plus鐢垫睜4325姣畨涓ゆ鏀寔20w鏈夌嚎鍏呯數鍜?5w鏃犵嚎鍏呯數iphone 14pro鐢垫睜3200姣畨銆乮phone 14 pro max鐢垫睜4323姣畨銆?/p>

涓ゆ鏀寔30w鏈夌嚎鍏呯數鍜?5w鏃犵嚎鍏呯數鍏跺畠鍏ㄧ郴ip68绛夌骇闃插皹闃叉按銆乄ifi6澧炲己鐗堛€佽摑鐗?.2銆丯fc銆佺珛浣撳0鍙屾壃澹板櫒锛岄瑁卛os16绯荤粺iphone 14鍏锋湁閫氳繃鍗槦瀹炵幇sos绱ф€ヨ仈缁滃姛鑳斤紝鍦ㄦ病鏈変俊鍙风殑鍦板尯锛屽彲鍙戦€佺揣鎬ユ眰鍔╃煭淇★紱瀹氬埗缁勪欢鍜岀壒瀹氳蒋浠跺彲杩炴帴鍗槦鐙壒棰戠巼锛涘畾鍒剁煭鏂囨湰鍦?5绉掑唴鍙彂閫佷竴鍒欎俊鎭紱鍦ㄩ噹澶栨椂锛屼篃鍙互鐢ㄥ崼鏄熷垎浜綅缃€?/p>

鍚屾椂锛宨phone 14杩樻敮鎸佽溅绁告娴嬶紝鍙互妫€娴嬫槸鍚﹀彂鐢熶弗閲嶇殑姹借溅纰版挒锛屽苟鑷姩鍛煎彨绱ф€ユ湇鍔°€?/p>

文章爱游戏平台的版权声明:除非注明,否则均为苗坤旺离型膜原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
applausebadlaughcoffeefabulousfacepalmfecesfrownheyhainsidiouskeepfightingnoprobpigheadshockedslapsocialsweattolaughwatermelonwittywowyeahyellowdog
评论列表 (暂无评论,10人围观)

还没有评论,来说两句吧...

微信二维码
微信二维码
支付宝二维码
网站地图