1. 程式人生 > >mysql小白系列_09 mysql性能優化關鍵點

mysql小白系列_09 mysql性能優化關鍵點

gre iss flags sch detect perm max into ssd

一 服務器參數調優,有哪些關鍵點?

1. 應用訪問優化

優化方法 性能提升效果 優化成本 說明
減少數據訪問
能不訪問就不訪問-減少磁盤IO
1~1000 緩存服務器緩存mysql數據,Redis、memorycache
返回更少的數據
較少網絡傳輸和磁盤IO
1~100 光折射損耗,請求距離/光速=響應時間
減少交互次數
較少網絡傳輸
1~20 存儲過程(缺乏擴展性),但很少使用,因此應用層實現

IOPS的一些數據

  • SAS的IOPS 100-200,順序讀寫吞吐量大,通常放置順序寫的log文件
  • SSD的IOPS在5W以上,讀比寫高一個數量級,適用讀多寫少的場景,通常放置隨機讀寫的data文件

4種IO調度算法

基本思路:通過合並和排序IO請求隊列中的請求大大降低所需的磁盤尋到時間,從而提高整體IO性能
1.Noop No operation
  • 最簡單的FIFO隊列算法
  • 僅適當合並用戶請求,並不排序請求
2.CFQ Completely Fair Queueing
  • 完全公平隊列算法,目標是保證磁盤IO帶寬的公平分配
  • 使用了多個排序隊列,確認為64
  • 按照IO請求地址進行排序,並不是按照先來後到的順序排序
  • 本質是輪詢IO輸入隊列,選擇第一個非空隊列,依次調度不同隊列的特定個數請求,然後將這些請求移動到調度隊列的末尾
  • 優先處理與上一個所處理的請求的最近的請求,有可能某些隊列的請求永遠得不到調度以至被餓死
原始隊列 100 500 101 10 56 1000
NOOP隊列 100 101 500 10 56 1000
CFQ隊列 100 101,500 1000,10 56(56可能被餓死)
3.Deadline
  • 在CFQ基礎上解決IO請求被餓死的情況
  • 除了CFQ的隊列(調度、排序隊列)之外,新增4個隊列
  • 1個讀IO請求隊列,1個寫IO請求隊列,請求根據起始扇區號排序
  • 2個deadline隊列,包含相同的讀和寫請求,根據請求的deadline排序
  • 引出隊列的目的就是為了解決餓死的問題
  • 默認情況,讀請求超時是500ms,寫請求超時是5s,寫不會堵塞讀,優先保證讀
  • 最後期限保證調度程序照顧等待了很長時間的請求,即使在排序隊列的末尾
(1)查看系統支持的調度算法
[root@docker02 queue]# dmesg |grep -i scheduler
[    0.796082] io scheduler noop registered
[    0.796084] io scheduler deadline registered (default)
[    0.796103] io scheduler cfq registered
(2)linux系統當前的磁盤IO調度算法:
[root@docker02 queue]# cat /sys/block/sda/queue/scheduler 
noop [deadline] cfq 

默認是deadline

(3)修改IO調度算法
[root@docker02 queue]# echo noop > /sys/block/sda/queue/scheduler
[root@docker02 queue]# cat scheduler 
[noop] deadline cfq 
4. Anticipatory
  • CFQ和Deadline考慮滿足零散IO請求
  • 並沒有優化連續的IO請求
  • 預期算法為了滿足隨機IO和順序IO混合場景

https://www.cnblogs.com/zhenjing/archive/2012/06/20/linux_writeback.html

2. 服務器硬件選型

服務器資源無外乎:CPU/MEM/IO/NET

  • CPU 根據CPU線程數進而設定mysql的線程數
  • 線程數=物理CPU個數 * 核數 * 每核線程數,如2*6*2=24

windows查看核數和每核線程數

cmd
wmic
cpu get *
    Name    NumberOfCores   NumberOfEnabledCore
... 2.20GHz 2               2
#NumberOfCores CPU核數
#NumberOfEnableCore 線程數
#筆記本,1物理CPU2核4線程

linux查看核數和每核線程數

(1)查看CPU型號
[root@docker02 queue]# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
      4  Intel(R) Xeon(R) CPU E5-2690 v2 @ 3.00GHz
(2)查看物理CPU個數,也就是插槽數
[root@docker02 queue]# cat /proc/cpuinfo|grep "physic"
physical id     : 0
address sizes   : 40 bits physical, 48 bits virtual
physical id     : 0
address sizes   : 40 bits physical, 48 bits virtual
physical id     : 1
address sizes   : 40 bits physical, 48 bits virtual
physical id     : 1
address sizes   : 40 bits physical, 48 bits virtual
[root@docker02 queue]# cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
2
(3)查看CPU核數,每個CPU多少核
[root@docker02 queue]# cat /proc/cpuinfo| grep "cpu cores"| uniq
cpu cores       : 2
(4)查看所有的CPU線程數
[root@docker02 queue]# cat /proc/cpuinfo| grep "processor"| wc -l
4

這裏的核數可設置為mysql的線程數: thread_concurrency

3. 操作系統優化

(1)調整內核減少swap使用

查看當前swap使用率
[root@docker02 ~]# sysctl -a |grep swappiness
vm.swappiness = 30
[root@docker02 ~]# cat /proc/sys/vm/swappiness
30
修改swap使用率
[root@docker02 ~]# sysctl vm.swappiness=10
vm.swappiness = 10
[root@docker02 ~]# vi etc/sysctl.conf
  • 可以設置成10,當內存剩余10%時才是用swap,盡可能不用swap
  • 如果為0,可能發生OOM錯誤

(2)單實例關閉NUMA,雙實例(2/4)打開NUMA

技術分享圖片

SMP Symmetric Multi Processing 對稱多處理
  • 所有CPU共享所有資源,如總線bus、memory、IO系統
  • 問題:隨著CPU的增多,CPU對等訪問相同的內存地址會造成沖突,造成CPU資源浪費
NUMA Non-Uniform Memory Access
  • 非統一內存訪問
  • 把幾十上百的CPU組合在一個服務器內
  • CPU訪問本地內存速度將高於遠地內存
  • 問題:訪問遠地地址造成CPU時間浪費

https://www.cnblogs.com/yubo/archive/2010/04/23/1718810.html

MPP Massive Parallel Processing
  • 由多個SMP服務器通過一定的節點互聯網絡進行連接,協同工作,完成相同的任務
  • 用戶角度看起來是一個服務器系統
  • 每個節點只訪問自己的本地資源-內存、存儲
查看NUMA

需要安裝numactl工具: yum -y install numactl

1.查看NUMA是否打開
  • dmesg判斷
[root@docker02 grub2]# dmesg | grep -i numa
[    0.000000] NUMA: Node 0 [mem 0x00000000-0x0009ffff] + [mem 0x00100000-0x0fffffff] -> [mem 0x00000000-0x0fffffff]
[    0.000000] NUMA: Node 0 [mem 0x00000000-0x0fffffff] + [mem 0x10000000-0xbfffffff] -> [mem 0x00000000-0xbfffffff]
[    0.000000] NUMA: Node 0 [mem 0x00000000-0xbfffffff] + [mem 0x100000000-0x13fffffff] -> [mem 0x00000000-0x13fffffff]

[root@docker02 device]# grep -i numa /var/log/dmesg
[    0.000000] NUMA: Node 0 [mem 0x00000000-0x0009ffff] + [mem 0x00100000-0x0fffffff] -> [mem 0x00000000-0x0fffffff]
[    0.000000] NUMA: Node 0 [mem 0x00000000-0x0fffffff] + [mem 0x10000000-0xbfffffff] -> [mem 0x00000000-0xbfffffff]
[    0.000000] NUMA: Node 0 [mem 0x00000000-0xbfffffff] + [mem 0x100000000-0x13fffffff] -> [mem 0x00000000-0x13fffffff]

說明numa為enable

2.查看numa狀態numastat
[root@docker02 grub2]# numastat
                           node0
numa_hit                10677303
numa_miss                      0
numa_foreign                   0
interleave_hit             14453
local_node              10677303
other_node                     0
  • numa_hit 打算在該節點上分配內存,最後從這個節點分配的次數
  • num_miss 打算在該節點上分配內存,最後卻從其它節點分配的次數
  • num_foregin 打算在其他節點分配內存,最後卻從這個節點分配的次數
  • interleave_hit 打算采用interleave策略最後從該節點分配的次數
  • local_node 該節點上的進程在該節點上分配的次數
  • other_node 其它節點進程在該節點上分配的次數
3.查看numa相關信息,node內存大小,每個node中的邏輯CPU
[root@docker02 grub2]# numactl --hardwar
available: 1 nodes (0)
node 0 cpus: 0 1 2 3
node 0 size: 4095 MB
node 0 free: 1769 MB
node distances:
node   0 
  0:  10 

問題:在esxi上分配的是2 * 2的CPU,這裏只能看到1個節點4個CPU,是否意味這4個CPU實際上在一個物理CPU內?
如果每個插槽的內核數 (cpuid.coresPerSocket) 大於 1,且虛擬機中的虛擬內核數大於 8,則虛擬 NUMA 節點大小與虛擬插槽大小相匹配。如果每個插槽的內核數小於或等於 1,則會創建虛擬 NUMA 節點以匹配首個打開虛擬機電源的物理主機的拓撲。
http://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.vsphere.resmgmt.doc%2FGUID-17B629DE-75DF-4C23-B831-08107007FBB9.html

[root@docker02 grub2]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    2
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 62
Model name:            Intel(R) Xeon(R) CPU E5-2690 v2 @ 3.00GHz
Stepping:              4
CPU MHz:               3000.000
BogoMIPS:              6000.00
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              25600K
NUMA node0 CPU(s):     0-3

查看每個邏輯CPU的繁忙程度:yum -y install sysstat

[root@docker02 grub2]# mpstat -P ALL
Linux 3.10.0-229.el7.x86_64 (docker02.bluemoon.com.cn)  03/01/2018      _x86_64_        (4 CPU)

01:37:49 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
01:37:49 PM  all    0.12    0.00    0.11    0.02    0.00    0.00    0.00    0.00    0.00   99.75
01:37:49 PM    0    0.11    0.00    0.10    0.02    0.00    0.00    0.00    0.00    0.00   99.77
01:37:49 PM    1    0.13    0.00    0.11    0.02    0.00    0.00    0.00    0.00    0.00   99.73
01:37:49 PM    2    0.12    0.00    0.10    0.02    0.00    0.00    0.00    0.00    0.00   99.75
01:37:49 PM    3    0.12    0.00    0.11    0.01    0.00    0.01    0.00    0.00    0.00   99.75
4.查看網卡的numa node
[root@docker02 device]# cat /sys/class/net/eth0/device/numa_node 
-1

http://blog.csdn.net/shaoyunzhe/article/details/53606584
https://www.cnblogs.com/wjoyxt/p/4804081.html

ESXi設置成2 * 8 CPU時
[root@docker01 ~]# numactl --hardware
available: 2 nodes (0-1)
node 0 cpus: 0 1 2 3 4 5 6 7
node 0 size: 2047 MB
node 0 free: 1405 MB
node 1 cpus: 8 9 10 11 12 13 14 15
node 1 size: 2047 MB
node 1 free: 1642 MB
node distances:
node   0   1 
  0:  10  20 
  1:  20  10 
[root@docker01 ~]# numastat
                           node0           node1
numa_hit                  202582          254284
numa_miss                      0               0
numa_foreign                   0               0
interleave_hit              7283            7172
local_node                200189          245831
other_node                  2393            8453
[root@docker01 ~]# numactl --hardwar
available: 2 nodes (0-1)
node 0 cpus: 0 1 2 3 4 5 6 7
node 0 size: 2047 MB
node 0 free: 1405 MB
node 1 cpus: 8 9 10 11 12 13 14 15
node 1 size: 2047 MB
node 1 free: 1642 MB
node distances:
node   0   1 
  0:  10  20 
  1:  20  10 
[root@docker01 ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                16
On-line CPU(s) list:   0-15
Thread(s) per core:    1
Core(s) per socket:    8
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 62
Model name:            Intel(R) Xeon(R) CPU E5-2690 v2 @ 3.00GHz
Stepping:              4
CPU MHz:               3000.000
BogoMIPS:              6000.00
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              25600K
NUMA node0 CPU(s):     0-7
NUMA node1 CPU(s):     8-15
關閉NUMA
1.BIOS設置關閉

http://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.vsphere.resmgmt.doc%2FGUID-3E956FB5-8ACB-42C3-B068-664989C3FF44.html

2.OS內核中設置關閉
[root@docker01 grub2]# vi /boot/grub2/grub.cfg
linux16 /vmlinuz-3.10.0-229.el7.x86_64 root=/dev/mapper/centos-root ro net.ifnames=0 biosdevname=0 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap numa=off rhgb quiet
[root@docker01 ~]# cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-3.10.0-229.el7.x86_64 root=/dev/mapper/centos-root ro net.ifnames=0 biosdevname=0 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap numa=off rhgb quiet

[root@docker01 grub2]# dmesg |grep -i numa
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-229.el7.x86_64 root=/dev/mapper/centos-root ro net.ifnames=0 biosdevname=0 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap numa=off rhgb quiet
[    0.000000] NUMA turned off
[    0.000000] Kernel command line: BOOT_IMAGE=/vmlinuz-3.10.0-229.el7.x86_64 root=/dev/mapper/centos-root ro net.ifnames=0 biosdevname=0 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap numa=off rhgb quiet

lscpu NUMA節點也變成1個了:

[root@docker01 grub2]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                16
On-line CPU(s) list:   0-15
Thread(s) per core:    1
Core(s) per socket:    8
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 62
Model name:            Intel(R) Xeon(R) CPU E5-2690 v2 @ 3.00GHz
Stepping:              4
CPU MHz:               3000.000
BogoMIPS:              6000.00
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              25600K
NUMA node0 CPU(s):     0-15

[root@docker01 grub2]# numactl --hardware
available: 1 nodes (0)
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
node 0 size: 4095 MB
node 0 free: 3111 MB
node distances:
node   0 
  0:  10 
3.mysql啟動時關閉

啟動的時候加入 --innodb-numa-interleave

[root@docker01 ~]# mysqld --verbose --help|grep numa
2018-03-01 14:23:29 0 [Note] mysqld (mysqld 5.6.39-log) starting as process 1950 ...
2018-03-01 14:23:29 1950 [Note] Plugin ‘FEDERATED‘ is disabled.
  --innodb-numa-interleave 
innodb-numa-interleave                                     FALSE
2018-03-01 14:23:29 1950 [Note] Binlog end
2018-03-01 14:23:29 1950 [Note] Shutting down plugin ‘MyISAM‘
2018-03-01 14:23:29 1950 [Note] Shutting down plugin ‘CSV‘

https://www.cnblogs.com/conanwang/p/6180894.html

4.網卡優化

(1)多張網卡聚合,提高負載或者冗余

1.配置網卡
[root@docker01 network-scripts]# cat ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
MASTER=bond0
SLAVE=yes
ONBOOT=yes
[root@docker01 network-scripts]# cat ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth1
DEVICE=eth1
MASTER=bond0
SLAVE=yes
ONBOOT=yes
[root@docker01 network-scripts]# cat ifcfg-bond0 
TYPE=Ethernet
BOOTPROTO=no
NAME=bond0
DEVICE=bond0
ONBOOT=yes
IPADDR=172.16.3.153
NETMASK=255.255.255.0
GATEWAY=172.16.3.254
DNS1=172.16.13.13
2.加載模塊
[root@docker01 network-scripts]# cat /etc/modprobe.d/mlx4.conf
alias bond0 bonding
options bond0 miimon=100 mode=1
[root@docker01 network-scripts]# modprobe bonding
  • mode=0 表示load balancing(round-robin)為負載均衡方式
  • mode=1 表示fault-tolerance(active-backup)提供冗余功能
  • 如果在ifcfg-bond0裏配置了BONDING_OPTS="mode=5 miimon=100",這裏可以不配置
3.重啟系統init 6
4.查看結果
[root@docker01 network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
    link/ether 00:50:56:a3:30:bb brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
    link/ether 00:50:56:a3:30:bb brd ff:ff:ff:ff:ff:ff
4: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 00:50:56:a3:30:bb brd ff:ff:ff:ff:ff:ff
    inet 172.16.3.153/24 brd 172.16.3.255 scope global bond0
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fea3:30bb/64 scope link 
       valid_lft forever preferred_lft forever

eth0 eth1 bond0三張網卡MAC地址一樣

[root@docker01 network-scripts]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:50:56:a3:30:bb
Slave queue ID: 0

Slave Interface: eth1
MII Status: up
Speed: 10000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:50:56:a3:8a:c2
Slave queue ID: 0

(2)調整網絡參數

1.查看網絡的各種連接狀態
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}‘
2.查看網絡網絡流量
[root@docker02 device]# sar -n DEV
Linux 3.10.0-229.el7.x86_64 (docker02)  03/01/2018      _x86_64_        (4 CPU)

01:30:01 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
01:40:01 PM      eth0      4.96      3.11      0.34      0.22      0.00      0.00      0.00
01:40:01 PM        lo      5.96      5.96      0.34      0.34      0.00      0.00      0.00
01:50:01 PM      eth0      5.20      3.25      0.37      0.25      0.00      0.00      0.00
01:50:01 PM        lo      5.96      5.96      0.34      0.34      0.00      0.00      0.00
02:00:01 PM      eth0      1.72      0.12      0.11      0.01      0.00      0.00      0.00
02:00:01 PM        lo      6.04      6.04      0.34      0.34      0.00      0.00      0.00
02:10:01 PM      eth0      2.06      0.08      0.13      0.00      0.00      0.00      0.00
02:10:01 PM        lo      5.96      5.96      0.34      0.34      0.00      0.00      0.00
02:20:01 PM      eth0      2.47      0.07      0.15      0.00      0.00      0.00      0.00
02:20:01 PM        lo      5.95      5.95      0.34      0.34      0.00      0.00      0.00
02:30:01 PM      eth0      2.66      0.68      0.20      0.30      0.00      0.00      0.00
02:30:01 PM        lo      6.13      6.13      0.35      0.35      0.00      0.00      0.00
02:40:01 PM      eth0      1.90      0.07      0.13      0.00      0.00      0.00      0.00
02:40:01 PM        lo      5.95      5.95      0.34      0.34      0.00      0.00      0.00
02:50:01 PM      eth0      1.92      0.08      0.12      0.00      0.00      0.00      0.00
02:50:01 PM        lo      5.96      5.96      0.34      0.34      0.00      0.00      0.00
03:00:01 PM      eth0      1.74      0.07      0.11      0.00      0.00      0.00      0.00
03:00:01 PM        lo      6.00      6.00      0.34      0.34      0.00      0.00      0.00
Average:         eth0      2.74      0.84      0.19      0.09      0.00      0.00      0.00
Average:           lo      5.99      5.99      0.34      0.34      0.00      0.00      0.00
3.查看網絡雙工模式
[root@docker02 device]# ethtool eth0
Settings for eth0:
        Supported ports: [ TP ]
        Supported link modes:   1000baseT/Full 
                                10000baseT/Full 
        Supported pause frame use: No
        Supports auto-negotiation: No
        Advertised link modes:  Not reported
        Advertised pause frame use: No
        Advertised auto-negotiation: No
        Speed: 10000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: off
        MDI-X: Unknown
        Supports Wake-on: uag
        Wake-on: d
        Link detected: yes

https://www.cnblogs.com/digdeep/p/4869010.html

4.增加MTU大小,默認1500
[root@docker02 device]# ifconfig eth0 mtu 9000 up
[root@docker02 device]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9000
5.增加網絡緩存
  • /proc/sys/net/ipv4/tcp_mem 系統全局參數,所有TCP的buffer配置,單位為內存頁(4k),超過上限時可能丟棄報文

    [root@docker02 device]# sysctl -a |grep tcp_mem
    net.ipv4.tcp_mem = 92451        123271  184902
  • /proc/sys/net/ipv4/tcp_rmen 第一個值為TCP接收buffer的最少字節數,第二個值是默認值,會被rmem_default覆蓋,第三個值是接收buffer的最大字節數,會被rmem_max覆蓋

    [root@docker02 device]# sysctl -a |grep tcp_rmem
    net.ipv4.tcp_rmem = 4096        87380   6291456
  • /proc/sys/net/ipv4/tcp_wmem 上面是接收,這裏是發送字節數

    [root@docker02 device]# sysctl -a |grep tcp_wmem
    net.ipv4.tcp_wmem = 4096        16384   4194304
  • /proc/sys/net/core/wmem_default TCP數據發送窗口默認字節數

    [root@docker02 device]# sysctl -a |grep wmem_default
    net.core.wmem_default = 212992
  • /proc/sys/net/core/wmem_max: TCP數據發送窗口最大字節數

    [root@docker02 device]# sysctl -a |grep wmem_max
    net.core.wmem_max = 212992
  • /proc/sys/net/core/rmem_default: TCP數據接收窗口默認字節數

    [root@docker02 device]# sysctl -a |grep rmem_default
    net.core.rmem_default = 212992
  • /proc/sys/net/core/rmem_max: TCP數據接收窗口最大字節數

    [root@docker02 device]# sysctl -a |grep rmem_max
    net.core.rmem_max = 212992
6.調整文件系統掛載選項

使用XFS或者EXT4的文件系統格式

(1)mount -o noatime
  • 性能提升5% 10% nobarrier
  • 不更新文件系統的訪問時間記錄
       noatime
              Do not update inode access times on this filesystem (e.g., for  faster
              access on the news spool to speed up news servers).
(2)mount -o nobarrier
  • 數據僅寫到磁盤緩沖區就返回IO響應
       barrier|nobarrier
              Enables/disables the use of block layer write barriers for writes into
              the  journal and for data integrity operations.  This allows for drive
              level write caching to be enabled, for devices that support write bar‐
              riers.

4. 數據庫優化

二 MySQL性能調優有哪些關鍵點/經驗?

1.實例優化

  • innodb_buffer_pool_size 類似SGA,決定了總的IO能力上限,一般設置為物理內存的60%-80%,內存時間和SSD時間響應相差1000倍
  • innodb_thread_concurrency 線程並發數量,一般為CPU的線程總數,如果為0則由innodb自動控制
  • query_cache_type = 0 緩存結果集
  • query_cache_size = 0 緩存大小 這兩個一般不用,直接通過nosql緩存服務器實現
  • max_user_connections 最大應用連接數,如果為300,則10個應用就是300,每個連接都需要消耗內存,每個會話占用內存最小是512k,最大是16M
  • interactive_timeout 交互超時時間,比如mysql mysqldump默認8小時,可設置為120s
  • wait_timeout 應用超時時間,比如通過連接池連接,jdbc,默認8小時,可設置為120s
  • innodb_io_capacity 設置為IOPS的75%左右,innodb後臺進程每秒處理的IO操作數據頁
  • innodb_flush_log_at_trx_commit = 1
  • 0 the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit. 每秒刷到磁盤,不受事務提交影響
  • 1 the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. 每事務刷新到磁盤
  • 2 the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. 每事務刷新到IO緩存,存在丟失風險
  • sync_binlog = 1 1個提交就寫到binlog,flush日誌到磁盤
  • innodb_log_file_size 日誌文件大小,ssd設置4-8G,sas設置1-2G
  • innodb_log_files_in_group 日誌文件個數
  • innodb_flush_method O_DIRECT,跳過IO緩存,直接落盤
  • innodb_max_dirty_pages_pct 50%,將innodb_buffer_pool_size刷到磁盤
  • innodb_flush_neighbors 刷新相鄰塊,如果是SSD關閉,SAS打開
  • tx_isolation 事務隔離級別,用RC提高並發

2.內存分配

(1)All thread buffer所有會話線程級別內存分配總和

tatol memory size=max_threads(當前活躍連接數) * (
read_buffer_size        + #順序讀緩沖,提高順序讀效率
read_rnd_buffer_size    + #隨機讀緩沖,提高隨機讀效率
sort_buffer_size        + #排序緩沖,提高排序效率
join_buffer_size        + #表連接緩沖,提高表連接效率
binlog_cache_size       + #二進制日誌緩沖,提高二進制日誌寫入效率
tmp_table_size          + #內存臨時表,提高臨時表存儲效率
thread_stack            + #線程堆棧,暫時寄存SQL語句或者存儲過程
thread_cache_size       + #線程緩沖池,避免多次反復打開線程開銷
net_buffer_length       + #線程池連接緩沖以及讀取結果緩沖
bulk_insert_buffer_size + #MyISAM表批量寫入數據緩沖
) 

(2)global buffer全局內存分配總和

global buffer size=
innodb_buffer_pool_size         + # innodb高速緩沖,行數據、索引緩沖、事務鎖、自適應哈希
innodb_additional_mem_pool_size + #innodb數據字典額外內存,緩存所有表數據字典
innodb_log_buffer_size          + #innodb redo日誌緩沖,提高redo日誌寫入效率
key_buffer_size                 + #MyISAM表索引高速緩沖,提高MyISAM表索引讀寫效率
query_cache_size                + #高速查詢韓村,緩存大汛結果,提高反復查詢返回效率
table_cache                     + #表空間文件描述緩存,提高數據表打開效率
table_definition_cache          + #表定義文件描述符緩存,就是緩存表結構,提高數據表打開效率

3.innodb_flush_method

http://blog.csdn.net/gua___gua/article/details/44916207

4.編寫高效SQL

  • 1.減少多表join,盡量用單表查詢,需要應用做配合
  • 2.select獲取準確的字段,不需要不獲取
  • 3.innodb避免使用count(*)
  • (1)實時統計要求高的可以使用緩存服務器memcache、redis
  • (2)要求低的可以使用單獨統計表,定時更新
  • 4.避免多余的排序
  • (1)使用group by時,默認升序排序
  • (2)不需要排序時,可以使用order by null
  • 5.全模糊查詢無法使用index,也不需要一開始就使用%like ‘%keyvalue%‘或者like ‘%keyvalue
    • 木板原理
  • 6.使用in代替or
    • SQL語句中的in包含的值不應該過多,盡量少於1000個
  • 7.禁止隱式轉換
  • (1)數值類型禁止加引號
  • (2)字符串類型必須加引號
  • 8.禁止使用反向查詢
  • 例如not in、!=、not like
  • 9.select for update語法,重點review流程
  • 10.盡量少or
  • (1)當where字句中存在多個條件以or並存的時候,mysql優化器並沒有很好的解決其執行計劃優化問題
  • (2)再加上mysql特有的sql和storage存儲分層架構方式,造成了其性能比較低下
  • (3)更多用的是union all或者是union的方式來替代or會得到更好的效果
  • 11.盡量用union all代替union
  • (1)union需要建多個結果集合並後再進行唯一性過濾操作
  • (2)因此會涉及到排序,增加大量的CPU運算,加大資源消耗和延遲
  • (3)確認結果集唯一或者結果集重復無關緊要時,盡量用union all
  • 12.盡量早過濾
  • (1)常見於索引的優化設計中,將過濾性更好的字段放得更靠前
  • (2)盡量使用join代替子查詢
  • 13.禁止在主庫上執行後臺管理和統計類功能的QUERY,必須要申請統計類在從庫執行

select id * 10.2 from t1 where id>0 -> select id from t1 where id >0;

盡量減少在DB上的計算

select id,name from t1 limit 100000,10; -> select id,name from t1 where id>100000 limit 10;

先返回10W條數據再分頁10條 直接返回10W後的10條

5.執行計劃

6.索引設計

(1)覆蓋索引
  • 1.查詢謂詞都能夠通過index進行掃描
  • 2.排序謂詞都能夠利用index的有些性
  • 3.index包含了查詢所需要的所有字段
(2)不能使用索引
  • 1.不要給選擇率低的字段建索引,通過索引掃描的記錄數超過30%,變成全表掃描
  • 2.聯合索引中,第一個索引列使用範圍查詢,第一個查詢條件不是最左索引列
  • 3.like最左以%開始
  • 4.兩個獨立索引,其中一個用於檢索,一個用於排序,索引不是越多越好,盡量合並索引
  • 5.表關聯字段類型不一樣,包含長度不一樣
  • 6.索引字段條件上使用函數
  • 7.不要使用外鍵索引

mysql小白系列_09 mysql性能優化關鍵點