mysql浼樺寲寤鸿

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql浼樺寲寤鸿相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%8f%98%e5%8c%96' title='鍙樺寲'>鍙樺寲   using   order   鐗规畩   file   璧风偣   text   sel   full join   

鍘熸枃寮曠敤https://www.dazhuanlan.com/2019/08/26/5d62f870a7fc2/


1銆丒XPLAIN

鍋歁ySQL浼樺寲锛屾垜浠鍠勭敤EXPLAIN鏌ョ湅SQL鎵ц璁″垝銆?/p>

  • type鍒楋紝杩炴帴绫诲瀷銆備竴涓ソ鐨凷QL璇彞鑷冲皯瑕佽揪鍒皉ange绾у埆銆傛潨缁濆嚭鐜癮ll绾у埆
  • key鍒楋紝浣跨敤鍒扮殑绱㈠紩鍚嶃€傚鏋滄病鏈夐€夋嫨绱㈠紩锛屽€兼槸NULL銆傚彲浠ラ噰鍙栧己鍒剁储寮曟柟寮忋€?/li>
  • key_len鍒楋紝绱㈠紩闀垮害銆?/li>
  • rows鍒楋紝鎵弿琛屾暟銆傝鍊兼槸涓浼板€笺€?/li>
  • extra鍒楋紝璇︾粏璇存槑銆傛敞鎰忥紝甯歌鐨勪笉澶弸濂界殑鍊硷紝濡備笅锛歎sing filesort锛孶sing temporary銆?/li>

SELECT * 澧炲姞寰堝涓嶅繀瑕佺殑娑堣€楋紙CPU銆両O銆佸唴瀛樸€佺綉缁滃甫瀹斤級锛涘鍔犱簡浣跨敤瑕嗙洊绱㈠紩鐨勫彲鑳芥€э紱褰撹〃缁撴瀯鍙戠敓鏀瑰彉鏃讹紝鍓嶆柇涔熼渶瑕佹洿鏂般€傛墍浠ヨ姹傜洿鎺ュ湪select鍚庨潰鎺ヤ笂瀛楁鍚嶃€?/p>

4銆佸綋鍙渶瑕佷竴鏉℃暟鎹殑鏃跺€欙紝浣跨敤limit 1

杩欐槸涓轰簡浣縀XPLAIN涓璽ype鍒楄揪鍒癱onst绫诲瀷

5銆佸鏋滄帓搴忓瓧娈垫病鏈夌敤鍒扮储寮曪紝灏卞敖閲忓皯鎺掑簭

6銆佸鏋滈檺鍒舵潯浠朵腑鍏朵粬瀛楁娌℃湁绱㈠紩锛屽敖閲忓皯鐢╫r

or涓よ竟鐨勫瓧娈典腑锛屽鏋滄湁涓€涓笉鏄储寮曞瓧娈碉紝鑰屽叾浠栨潯浠朵篃涓嶆槸绱㈠紩瀛楁锛屼細閫犳垚璇ユ煡璇笉璧扮储寮曠殑鎯呭喌銆傚緢澶氭椂鍊欎娇鐢╱nion all鎴栬€呮槸union锛堝繀瑕佺殑鏃跺€欙級鐨勬柟寮忔潵浠f浛鈥渙r鈥濅細寰楀埌鏇村ソ鐨勬晥鏋溿€?/p>

7銆佸敖閲忕敤union all浠f浛union

union鍜寀nion all鐨勫樊寮備富瑕佹槸鍓嶈€呴渶瑕佸皢缁撴灉闆嗗悎骞跺悗鍐嶈繘琛屽敮涓€鎬ц繃婊ゆ搷浣滐紝杩欏氨浼氭秹鍙婂埌鎺掑簭锛屽鍔犲ぇ閲忕殑CPU杩愮畻锛屽姞澶ц祫婧愭秷鑰楀強寤惰繜銆傚綋鐒讹紝union all鐨勫墠鎻愭潯浠舵槸涓や釜缁撴灉闆嗘病鏈夐噸澶嶆暟鎹€?/p>

8銆佷笉浣跨敤ORDER BY RAND()

1
2
3
select id from `dynamic` order by rand() limit 1000;
涓婇潰鐨凷QL璇彞锛屽彲浼樺寲涓猴細
select id from `dynamic` t1 join (select rand() * (select max(id) from `dynamic`) as nid) t2 on t1.id > t2.nidlimit 1000;

9銆佸尯鍒唅n鍜宔xists銆乶ot in鍜宯ot exists

1
2
3
select * from 琛ˋ where id in (select id from 琛˙)
涓婇潰SQL璇彞鐩稿綋浜庯細
select * from 琛ˋ where exists(select * from 琛˙ where 琛˙.id=琛ˋ.id)

鍖哄垎in鍜宔xists涓昏鏄€犳垚浜嗛┍鍔ㄩ『搴忕殑鏀瑰彉锛堣繖鏄€ц兘鍙樺寲鐨勫叧閿級锛屽鏋滄槸exists锛岄偅涔堜互澶栧眰琛ㄤ负椹卞姩琛紝鍏堣璁块棶锛屽鏋滄槸IN锛岄偅涔堝厛鎵ц瀛愭煡璇€傛墍浠N閫傚悎浜庡琛ㄥぇ鑰屽唴琛ㄥ皬鐨勬儏鍐碉紱EXISTS閫傚悎浜庡琛ㄥ皬鑰屽唴琛ㄥぇ鐨勬儏鍐点€?/p>

鍏充簬not in鍜宯ot exists锛屾帹鑽愪娇鐢╪ot exists锛屼笉浠呬粎鏄晥鐜囬棶棰橈紝not in鍙兘瀛樺湪閫昏緫闂銆傚浣曢珮鏁堢殑鍐欏嚭涓€涓浛浠ot exists鐨凷QL璇彞锛?/p>

1
2
3
4
5
鍘烻QL璇彞锛?/span>
select colname 鈥?from A琛?where a.id not in (select b.id from B琛?
楂樻晥鐨凷QL璇彞锛?/span>
select colname 鈥?from A琛?Left join B琛?on where a.id = b.id where b.id is null
鍙栧嚭鐨勭粨鏋滈泦涓篈琛ㄤ笉鍦˙琛ㄤ腑鐨勬暟鎹?/span>

10銆佷娇鐢ㄥ悎鐞嗙殑鍒嗛〉鏂瑰紡浠ユ彁楂樺垎椤电殑鏁堢巼

1
2
3
4
select id,name from product limit 866613, 20
浣跨敤涓婅堪SQL璇彞鍋氬垎椤电殑鏃跺€欙紝鍙兘鏈変汉浼氬彂鐜帮紝闅忕潃琛ㄦ暟鎹噺鐨勫鍔狅紝鐩存帴浣跨敤limit鍒嗛〉鏌ヨ浼氳秺鏉ヨ秺鎱€?/span>
浼樺寲鐨勬柟娉曞涓嬶細鍙互鍙栧墠涓€椤电殑鏈€澶ц鏁扮殑id锛岀劧鍚庢牴鎹繖涓渶澶х殑id鏉ラ檺鍒朵笅涓€椤电殑璧风偣銆傛瘮濡傛鍒椾腑锛屼笂涓€椤垫渶澶х殑id鏄?span class="number">866612銆?span class="keyword">SQL
鍙互閲囩敤濡備笅鐨勫啓娉曪細
select id,name from product where id> 866612 limit 20

渚嬪LIKE鈥?name鈥濇垨鑰匧IKE鈥?name%鈥濓紝杩欑鏌ヨ浼氬鑷寸储寮曞け鏁堣€岃繘琛屽叏琛ㄦ壂鎻忋€備絾鏄彲浠ヤ娇鐢↙IKE 鈥渘ame%鈥濄€?/p>

铏界劧缁檚ecret瀛楁娣诲姞浜嗙储寮曪紝浣嗗湪explain缁撴灉骞舵病鏈変娇鐢?/p>

閭d箞濡備綍瑙e喅杩欎釜闂鍛紝绛旀锛氫娇鐢ㄥ叏鏂囩储寮曘€?/p>

鍦ㄦ垜浠煡璇腑缁忓父浼氱敤鍒皊elect id,fnum,fdst from dynamic_201606 where user_name like 鈥?zhangsan%鈥? 銆傝繖鏍风殑璇彞锛屾櫘閫氱储寮曟槸鏃犳硶婊¤冻鏌ヨ闇€姹傜殑銆傚簡骞哥殑鏄湪MySQL涓紝鏈夊叏鏂囩储寮曟潵甯姪鎴戜滑銆?/p>

1
2
3
4
5
鍒涘缓鍏ㄦ枃绱㈠紩鐨凷QL璇硶鏄細
ALTER TABLE `dynamic_201606` ADD FULLTEXT INDEX `idx_user_name` (`user_name`);
浣跨敤鍏ㄦ枃绱㈠紩鐨凷QL璇彞鏄細
select id,fnum,fdst from dynamic_201606 where match(user_name) against(鈥榸hangsan鈥?/span> in boolean mode);
娉ㄦ剰锛氬湪闇€瑕佸垱寤哄叏鏂囩储寮曚箣鍓嶏紝璇疯仈绯籇BA纭畾鑳藉惁鍒涘缓銆傚悓鏃堕渶瑕佹敞鎰忕殑鏄煡璇㈣鍙ョ殑鍐欐硶涓庢櫘閫氱储寮曠殑鍖哄埆銆?/span>

涓惧垪鏉ヨ绱㈠紩鍚湁瀛楁id銆乶ame銆乻chool锛屽彲浠ョ洿鎺ョ敤id瀛楁锛屼篃鍙互id銆乶ame杩欐牱鐨勯『搴忥紝浣嗘槸name;school閮芥棤娉曚娇鐢ㄨ繖涓储寮曘€傛墍浠ュ湪鍒涘缓鑱斿悎绱㈠紩鐨勬椂鍊欎竴瀹氳娉ㄦ剰绱㈠紩瀛楁椤哄簭锛屽父鐢ㄧ殑鏌ヨ瀛楁鏀惧湪鏈€鍓嶉潰銆?/p>

17銆佸繀瑕佹椂鍙互浣跨敤force index鏉ュ己鍒舵煡璇㈣蛋鏌愪釜绱㈠紩

鏈夌殑鏃跺€橫ySQL浼樺寲鍣ㄩ噰鍙栧畠璁や负鍚堥€傜殑绱㈠紩鏉ユ绱QL璇彞锛屼絾鏄彲鑳藉畠鎵€閲囩敤鐨勭储寮曞苟涓嶆槸鎴戜滑鎯宠鐨勩€傝繖鏃跺氨鍙互閲囩敤forceindex鏉ュ己鍒朵紭鍖栧櫒浣跨敤鎴戜滑鍒跺畾鐨勭储寮曘€?/p>

EFT JOIN A琛ㄤ负椹卞姩琛紝INNER JOIN MySQL浼氳嚜鍔ㄦ壘鍑洪偅涓暟鎹皯鐨勮〃浣滅敤椹卞姩琛紝RIGHT JOIN B琛ㄤ负椹卞姩琛ㄣ€?/p>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
娉ㄦ剰锛?/span>
1锛塎ySQL涓病鏈塮ull join锛屽彲浠ョ敤浠ヤ笅鏂瑰紡鏉ヨВ鍐筹細
select * from A left join B on B.name = A.namewhere B.name is nullunion allselect * from B;
2锛夊敖閲忎娇鐢╥nner join锛岄伩鍏峫eft join锛?/span>
鍙備笌鑱斿悎鏌ヨ鐨勮〃鑷冲皯涓?寮犺〃锛屼竴鑸兘瀛樺湪澶у皬涔嬪垎銆傚鏋滆繛鎺ユ柟寮忔槸inner join锛屽湪娌℃湁鍏朵粬杩囨护鏉′欢鐨勬儏鍐典笅MySQL浼氳嚜鍔ㄩ€夋嫨灏忚〃浣滀负椹卞姩琛紝浣嗘槸left join鍦ㄩ┍鍔ㄨ〃鐨勯€夋嫨涓婇伒寰殑鏄乏杈归┍鍔ㄥ彸杈圭殑鍘熷垯锛屽嵆left join宸﹁竟鐨勮〃鍚嶄负椹卞姩琛ㄣ€?/span>
3锛夊悎鐞嗗埄鐢ㄧ储寮曪細
琚┍鍔ㄨ〃鐨勭储寮曞瓧娈典綔涓簅n鐨勯檺鍒跺瓧娈点€?/span>
4锛夊埄鐢ㄥ皬琛ㄥ幓椹卞姩澶ц〃锛?/span>
濡傛灉鑳藉鍑忓皯椹卞姩琛ㄧ殑璇濓紝鍑忓皯宓屽寰幆涓殑寰幆娆℃暟锛屼互鍑忓皯 IO鎬婚噺鍙奀PU杩愮畻鐨勬鏁般€?/span>
5锛夊阀鐢⊿TRAIGHT_JOIN锛?/span>
inner join鏄敱MySQL閫夋嫨椹卞姩琛紝浣嗘槸鏈変簺鐗规畩鎯呭喌闇€瑕侀€夋嫨鍙︿釜琛ㄤ綔涓洪┍鍔ㄨ〃锛屾瘮濡傛湁group by銆乷rder by绛夆€淯sing filesort鈥濄€佲€淯sing temporary鈥濇椂銆係TRAIGHT_JOIN鏉ュ己鍒惰繛鎺ラ『搴忥紝鍦⊿TRAIGHT_JOIN宸﹁竟鐨勮〃鍚嶅氨鏄┍鍔ㄨ〃锛屽彸杈瑰垯鏄椹卞姩琛ㄣ€傚湪浣跨敤STRAIGHT_JOIN鏈変釜鍓嶆彁鏉′欢鏄鏌ヨ鏄唴杩炴帴锛屼篃灏辨槸inner join銆傚叾浠栭摼鎺ヤ笉鎺ㄨ崘浣跨敤STRAIGHT_JOIN锛屽惁鍒欏彲鑳介€犳垚鏌ヨ缁撴灉涓嶅噯纭€?/span>
SELECT `rl`.`fid`,
`rl`.`update_time` as `add_time`,
`f`.`fnum`,
`f`.`dst_parking`,
`f`.`aircraft_num`,
`f`.`touch_down_runway`,
`f`.`forg`,
`f`.`fdst`,
`f`.`flight_status_code`,
`fd`.`fdst` as `fd_fdst`,
`ds`.`touch_down_runway_source`,
`ds`.`touch_down_runway_update_time`,
`u`.`truename`,
`a`.`aircraft_model`
FROM `goms_flight_runway_log` as `rl`
STRAIGHT_JOIN `goms_flight` as `f` ON `rl`.`fid` = `f`.`fid`
LEFT JOIN `goms_aircraft` as `a` ON `f`.`aircraft_num` = `a`.`aircraft_num`
LEFT JOIN `goms_flight_link` as `fl` ON `f`.`fid` = `fl`.`arr_fid`
LEFT JOIN `goms_flight` as `fd` ON `fl`.`dept_fid` = `fd`.`fid`
LEFT JOIN `goms_flight_field_data_source` as `ds` ON `f`.`fid` = `ds`.`fid`
LEFT JOIN `goms_user` as `u` ON `ds`.`touch_down_runway_source` = `u`.`uid`
WHERE `rl`.`op_type` = 0
AND `f`.`fdst` = 鈥楶VG鈥?/span>
AND `f`.`fid` != 鈥樷€?/span>
GROUP BY `rl`.`fid`
ORDER BY `rl`.`update_time` DESC
LIMIT 20

杩欎釜鏂瑰紡鏈夋椂鑳藉噺灏?鍊嶇殑鏃堕棿銆?/p>

以上是关于mysql浼樺寲寤鸿的主要内容,如果未能解决你的问题,请参考以下文章

杈撳叆浼樺寲妯℃澘

绋嬪簭鎬ц兘浼樺寲涔嬬綉缁滀紶杈撲笌鏁版嵁瀛樺偍浼樺寲锛堜簲锛変笅

鏂滅巼浼樺寲

璇诲叆浼樺寲

Hive Join浼樺寲

[鎵撳寘浼樺寲]Webpack浣撶Н鍘嬬缉