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>
3銆丼ELECT璇彞鍔″繀鎸囨槑瀛楁鍚嶇О
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 |
select id from `dynamic` order by rand() limit 1000; |
9銆佸尯鍒唅n鍜宔xists銆乶ot in鍜宯ot exists
1 |
select * from 琛ˋ where id in (select id from 琛˙) |
鍖哄垎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 |
select id,name from product limit 866613, 20 |
13銆佷笉寤鸿浣跨敤%鍓嶇紑妯$硦鏌ヨ
渚嬪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>
1 |
姣斿锛?/span> |
16銆佸浜庤仈鍚堢储寮曟潵璇达紝瑕侀伒瀹堟渶宸﹀墠缂€娉曞垯
涓惧垪鏉ヨ绱㈠紩鍚湁瀛楁id銆乶ame銆乻chool锛屽彲浠ョ洿鎺ョ敤id瀛楁锛屼篃鍙互id銆乶ame杩欐牱鐨勯『搴忥紝浣嗘槸name;school閮芥棤娉曚娇鐢ㄨ繖涓储寮曘€傛墍浠ュ湪鍒涘缓鑱斿悎绱㈠紩鐨勬椂鍊欎竴瀹氳娉ㄦ剰绱㈠紩瀛楁椤哄簭锛屽父鐢ㄧ殑鏌ヨ瀛楁鏀惧湪鏈€鍓嶉潰銆?/p>
17銆佸繀瑕佹椂鍙互浣跨敤force index鏉ュ己鍒舵煡璇㈣蛋鏌愪釜绱㈠紩
鏈夌殑鏃跺€橫ySQL浼樺寲鍣ㄩ噰鍙栧畠璁や负鍚堥€傜殑绱㈠紩鏉ユ绱QL璇彞锛屼絾鏄彲鑳藉畠鎵€閲囩敤鐨勭储寮曞苟涓嶆槸鎴戜滑鎯宠鐨勩€傝繖鏃跺氨鍙互閲囩敤forceindex鏉ュ己鍒朵紭鍖栧櫒浣跨敤鎴戜滑鍒跺畾鐨勭储寮曘€?/p>
19銆佸叧浜嶫OIN浼樺寲
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浼樺寲寤鸿的主要内容,如果未能解决你的问题,请参考以下文章