柚子快報(bào)邀請(qǐng)碼778899分享:DBA筆記-第一部分
柚子快報(bào)邀請(qǐng)碼778899分享:DBA筆記-第一部分
優(yōu)化排序占用內(nèi)存(order by)
--查看全局global show global variables like 'sort_buffer_size%'; --修改當(dāng)前內(nèi)存占用為256兆 ?注意每個(gè)會(huì)話都可能會(huì)占用,需要根據(jù) 實(shí)際用戶來判斷設(shè)置為多少(線程使用到了才會(huì)分配給會(huì)話) set sort_buffer_size = 256*1024*1204 ----查看狀態(tài) ?包含順序 ? (global 可看全局) show status like 'sort%'; +-------------------+-------+ | Variable_name ? ? | Value | +-------------------+-------+ | Sort_merge_passes | 360 ? | ?內(nèi)存不足導(dǎo)致多次調(diào)用內(nèi)存排序次數(shù) 越小越好 | Sort_range ? ? ? ?| 0 ? ? | | Sort_rows ? ? ? ? | 20000 | | Sort_scan ? ? ? ? | 1 ? ? | +-------------------+-------+ ?
?優(yōu)化臨時(shí)表查詢(group by)
--查看全局global show global variables like '%tmp%'; +----------------------------------+----------+ | Variable_name ? ? ? ? ? ? ? ? ? ?| Value ? ?| +----------------------------------+----------+ | default_tmp_storage_engine ? ? ? | InnoDB ? | | innodb_tmpdir ? ? ? ? ? ? ? ? ? ?| ? ? ? ? ?| | internal_tmp_disk_storage_engine | InnoDB ? | | max_tmp_tables ? ? ? ? ? ? ? ? ? | 32 ? ? ? | | slave_load_tmpdir ? ? ? ? ? ? ? ?| /tmp ? ? | | tmp_table_size ? ? ? ? ? ? ? ? ? | 16777216 | 臨時(shí)表大小 | tmpdir ? ? ? ? ? ? ? ? ? ? ? ? ? | /tmp ? ? | +----------------------------------+----------+
---可根據(jù)需求增大,修改為1g?? set tmp_table_size = 1024*1024*1024;
show variables like '%tmp%'; +----------------------------------+------------+ | Variable_name ? ? ? ? ? ? ? ? ? ?| Value ? ? ?| +----------------------------------+------------+ | default_tmp_storage_engine ? ? ? | InnoDB ? ? | | innodb_tmpdir ? ? ? ? ? ? ? ? ? ?| ? ? ? ? ? ?| | internal_tmp_disk_storage_engine | InnoDB ? ? | | max_tmp_tables ? ? ? ? ? ? ? ? ? | 32 ? ? ? ? | | slave_load_tmpdir ? ? ? ? ? ? ? ?| /tmp ? ? ? | | tmp_table_size ? ? ? ? ? ? ? ? ? | 1073741824 | | tmpdir ? ? ? ? ? ? ? ? ? ? ? ? ? | /tmp ? ? ? | +----------------------------------+------------+
show status like '%tmp%'; +-------------------------+-------+ | Variable_name ? ? ? ? ? | Value | +-------------------------+-------+ | Created_tmp_disk_tables | 0 ? ? |內(nèi)存不足使用硬盤次數(shù) | Created_tmp_files ? ? ? | 6 ? ? | | Created_tmp_tables ? ? ?| 5 ? ? | +-------------------------+-------+
查詢小技巧
-----count說明----
count(1)=count(2)=count(3)=count(*)查詢數(shù)據(jù)條數(shù)包含null 且count幾都是一樣的沒區(qū)別
count(字段名)查詢數(shù)據(jù)條數(shù)不包含null
-----null的判斷------
子查詢要加is not null 否則會(huì)導(dǎo)致查詢不到想要的結(jié)果
a表? ? ? ? ? ? ?b表
id? ?列1? ? ? ?id? ?列1?
1? ? 1? ? ? ? ? ?1? ? ?1
2? ? ?2? ? ? ? ??2? ? ?2
3? ? ?null? ? ? ?3? ? 3
select 列1 from b? where 列1? not in (select 列1 from a)? 結(jié)果為null
select 列1 from b? where 列1? not in (select 列1 from a where 列1 is not null)? 結(jié)果為3
-----增加行號(hào)查詢-----
注意要給表取別名 使用*會(huì)有問題
SELECT @a := @a + 1 AS row_number, e.* FROM employees e,(select @a:=0) a;
面試能寫出來就過的,需要sql天賦過人,但實(shí)際沒啥用的相關(guān)子查詢行號(hào)寫法,一般人理解不了。
(求和小于等于每行數(shù)據(jù)值得數(shù)量)配合mysql官網(wǎng)中employees數(shù)據(jù)庫(kù)練習(xí)
SELECT emp_no,( SELECT count( 1 ) FROM employees t2 WHERE t2.emp_no <= t1.emp_no ) AS rownum? FROM ?? ?employees t1? GROUP BY ?? ?emp_no? ?? ?LIMIT 10
------limit 解釋------
limit 10 取得是隨機(jī)數(shù)據(jù),不是按順序取,取得是一個(gè)集合中的數(shù),集合的定義是無序數(shù)據(jù)的合集。
柚子快報(bào)邀請(qǐng)碼778899分享:DBA筆記-第一部分
推薦鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。