search

MySQL优化笔记

Jun 07, 2016 pm 04:33 PM
mysqloptimizationInstallNoticenotes

之前安装时没注意MySQL的优化,先想对MySQL做一下优化.首先看一下没有优化之前各个参数:MySQL预编译参数: ./configure --prefix=/usr/local/mysql --with-ssl --with-readline --with-big-tables --enable-assembler top 32553 mysql 20 0 125m 17m 4064 S 0.

之前安装时没注意MySQL的优化,先想对MySQL做一下优化.首先看一下没有优化之前各个参数: MySQL预编译参数:

 ./configure  --prefix=/usr/local/mysql --with-ssl --with-readline --with-big-tables --enable-assembler

top

32553 mysql     20   0  125m  17m 4064 S  0.0  1.7   5:13.01 mysqld

使用mysqlreport获取MySQL运行参数:

MySQL 5.0.40-log         uptime 15 22:1:21      Sat Feb  4 10:04:23 2012
__ Key _________________________________________________________________
Buffer used    62.00k of  16.00M  %Used:   0.38
  Current       1.90M            %Usage:  11.89
Write hit      22.29%
Read hit       99.83%
__ Questions ___________________________________________________________
Total          85.58k     0.1/s
  DMS          77.61k     0.1/s  %Total:  90.69
  Com_          5.37k     0.0/s            6.28
  COM_QUIT      2.52k     0.0/s            2.95
  +Unknown         78     0.0/s            0.09
Slow 10 s           0       0/s            0.00  %DMS:   0.00  Log: OFF
DMS            77.61k     0.1/s           90.69
  SELECT       72.77k     0.1/s           85.03         93.76
  UPDATE        2.68k     0.0/s            3.13          3.45
  INSERT        1.09k     0.0/s            1.27          1.41
  DELETE        1.07k     0.0/s            1.25          1.38
  REPLACE           0       0/s            0.00          0.00
Com_            5.37k     0.0/s            6.28
  set_option    2.60k     0.0/s            3.04
  change_db     2.52k     0.0/s            2.94
  show_fields      77     0.0/s            0.09
__ SELECT and Sort _____________________________________________________
Scan            5.17k     0.0/s %SELECT:   7.10
Range           2.75k     0.0/s            3.77
Full join           0       0/s            0.00
Range check         0       0/s            0.00
Full rng join       0       0/s            0.00
Sort scan       5.97k     0.0/s
Sort range      4.30k     0.0/s
Sort mrg pass       0       0/s
__ Table Locks _________________________________________________________
Waited             24     0.0/s  %Total:   0.03
Immediate      91.00k     0.1/s
__ Tables ______________________________________________________________
Open               36 of   64    %Cache:  56.25
Opened             42     0.0/s
__ Connections _________________________________________________________
Max used            5 of  100      %Max:   5.00
Total           2.52k     0.0/s
__ Created Temp ________________________________________________________
Disk table      4.15k     0.0/s
Table           7.11k     0.0/s    Size:  32.0M
File                5     0.0/s
__ Threads _____________________________________________________________
Running             1 of    1
Cached              0 of    0      %Hit:   0.04
Created         2.52k     0.0/s
Slow                0       0/s
__ Aborted _____________________________________________________________
Clients             0       0/s
Connects            0       0/s
__ Bytes _______________________________________________________________
Sent          226.71M   164.8/s
Received       12.59M     9.2/s
__ InnoDB Buffer Pool __________________________________________________
Usage         304.00k of   8.00M  %Used:   3.71
Read hit       84.42%
Pages
  Free            493            %Total:  96.29
  Data             19                      3.71 %Drty:   0.00
  Misc              0                      0.00
  Latched           0                      0.00
Reads              77     0.0/s
  From file        12     0.0/s           15.58
  Ahead Rnd         1     0.0/s
  Ahead Sql         0       0/s
Writes              0       0/s
Flushes             0       0/s
Wait Free           0       0/s
__ InnoDB Lock _________________________________________________________
Waits               0       0/s
Current             0
Time acquiring
  Total             0 ms
  Average           0 ms
  Max               0 ms
__ InnoDB Data, Pages, Rows ____________________________________________
Data
  Reads            25     0.0/s
  Writes            3     0.0/s
  fsync             3     0.0/s
  Pending
    Reads           0
    Writes          0
    fsync           0
Pages
  Created           0       0/s
  Read             19     0.0/s
  Written           0       0/s
Rows
  Deleted           0       0/s
  Inserted          0       0/s
  Read              0       0/s
  Updated           0       0/s

首先在预编译参数上进行优化

./configure --prefix=/usr/local/mysql \
--without-debug \                                 # 取消调试模式提高性能
--with-extra-charsets=utf8,gbk \                  # 仅仅指定需要的默认字符集提高性能
--enable-assembler \                              # 使用汇编模式提高性能
--with-mysqld-ldflags=-all-static \               # 以静态方式编译提高性能
--with-client-ldflags=-all-static \
--with-unix-socket-path=/tmp/mysql.sock \         # 使用unix socket提高性能
--with-ssl

安装完成后进一步优化my.cnf: 因为MySQL 只会 Cache 索引(*.MYI),因此您只要将数据库中所有的 MYI 档案加总起来就是key buffer 的值,计算MYI档案的总大小:

du -hc `find /usr/local/mysql/var/ -name *.MYI`
4.0K    /usr/local/mysql/var/myblog/wp_term_taxonomy.MYI
8.0K    /usr/local/mysql/var/myblog/wp_posts.MYI
8.0K    /usr/local/mysql/var/myblog/wp_usermeta.MYI
8.0K    /usr/local/mysql/var/myblog/wp_commentmeta.MYI
16K     /usr/local/mysql/var/myblog/wp_options.MYI
12K     /usr/local/mysql/var/myblog/wp_postmeta.MYI
8.0K    /usr/local/mysql/var/myblog/wp_comments.MYI
4.0K    /usr/local/mysql/var/myblog/wp_links.MYI
4.0K    /usr/local/mysql/var/myblog/wp_term_relationships.MYI
4.0K    /usr/local/mysql/var/myblog/wp_users.MYI
8.0K    /usr/local/mysql/var/myblog/wp_terms.MYI
16K     /usr/local/mysql/var/mysql/help_relation.MYI
4.0K    /usr/local/mysql/var/mysql/time_zone_name.MYI
16K     /usr/local/mysql/var/mysql/help_keyword.MYI
4.0K    /usr/local/mysql/var/mysql/func.MYI
4.0K    /usr/local/mysql/var/mysql/time_zone.MYI
20K     /usr/local/mysql/var/mysql/help_topic.MYI
4.0K    /usr/local/mysql/var/mysql/columns_priv.MYI
4.0K    /usr/local/mysql/var/mysql/procs_priv.MYI
4.0K    /usr/local/mysql/var/mysql/time_zone_leap_second.MYI
4.0K    /usr/local/mysql/var/mysql/user.MYI
4.0K    /usr/local/mysql/var/mysql/tables_priv.MYI
4.0K    /usr/local/mysql/var/mysql/host.MYI
4.0K    /usr/local/mysql/var/mysql/time_zone_transition_type.MYI
4.0K    /usr/local/mysql/var/mysql/proc.MYI
4.0K    /usr/local/mysql/var/mysql/help_category.MYI
4.0K    /usr/local/mysql/var/mysql/db.MYI
4.0K    /usr/local/mysql/var/mysql/time_zone_transition.MYI
192K    total

修改my.cnf参数大小:

vi /etc/my.cnf
# 降低key_buffer的值
key_buffer = 4M

重启MySQL执行top命令:

18125 mysql     20   0  109m  11m 2152 S  0.0  1.1   0:00.08 mysqld

看到MySQL的内存利用率降低到1.1,这时候还不适宜执行mysqlreport查看等待启动一天后查看.由于现在访问量较低,所以参数适量调低,需要实时监控MySQL运行状况适当运行参数.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.