我们仍然使用两个会话,一个会话 run,用于运行主 SQL;另一个会话 ps,用于进行 performance_schema 的观察:
将 performance_schema 中的统计量重置,
临时表的表大小限制取决于参数? tmp_table_size 和 max_heap_table_size 中较小者,我们实验中以设置 max_heap_table_size 为例.
查看内存的分配记录:
查看语句的特征值:
可以看到语句使用了一次需要落磁盘的临时表.
那么这张临时表用了多少的磁盘呢?
我们开启 performance_schema 中 waits 相关的统计项:
重做实验,略过.
再查看 performance_schema 的统计值:
可以看到几个现象:
可以看到写入的线程是 page_clean_thread,是一个刷脏操作,这样就能理解数据为什么是慢慢写入的.
结论:
我们可以看到,
① MySQL 会基本遵守 max_heap_table_size 的设定,在内存不够用时,直接将表转到磁盘上存储.
有一个表引擎叫内存表,你在新建时可以选择,或已有的表可以修改过去
①.、从内存中读取数据是微秒级别的.而从磁盘读则是毫秒级别的.二者相差一个数量级.所以想优化数据库,第一个要做到的就是优化io.
insert ... select ...
insert into ... values ...
生产环境中,MySQL 不经意间吃掉全部的内容,然后开始吃掉 SWAP,性能一降再降,怎么办?
可以从下面三点查看原因:
MySQL 使用内存,有两个途径.
永久占用的内容
比如全局缓冲区(Global Buffer)类别,是在服务器启动期间从操作系统获得的,不会释放到任何一个别的进程.
动态请求的内存
线程缓冲区由MySQL使用,它是在处理新查询时从操作系统请求的内存.在执行查询之后,该内存被释放回操作系统.
这意味着 MySQL 的内存使用,是 全局缓冲区 加上 线程缓冲区 以及 允许的最大连接数 .
检查一下 MySQL 设置,有助于确定内存使用情况,从而为 MySQL 分配合适的值.
一个近似的公式:
当网站受到攻击时,有可能在短时间内建立异常高的连接数量.MySQL 中的 PROCESSLIST 可用于检测顶级用户并阻止对滥用连接的访问.
找出查询需要很长时间才能执行的语句,因为这些查询需要进一步优化服务器才能更好地执行,可以通过服务器查询日志进行识别.由于查询速度慢,导致磁盘读取较多,导致内存和CPU使用率较高,影响服务器性能.
最后,到了加内存条的时候了.虽然在优化数据库设置之后,服务器会不断地路由到使用交换内存,但也必须增加内存.俗话说:"巧妇难为无米之炊",就是这个意思.
上面说的这些方向,大家可以在实际操作中验证体会,希望大家在数据库优化的路上,麻溜顺畅,砥砺前行.
mysql耗内存吗?很多人都说MySQL占用了很大的虚拟内存,那么这个问题应该怎么解决呢?下面是我收集整理的一些方法,现在分享给大家!
解决mysql耗内存的具体方法一:
优化方法:
① 退出EMS clientserver
my.ini
# MySQL Server Instance Configuration File
# ----------------------------------------------------------------------
# Generated by the MySQL Server Instance Configuration Wizard
#
# Installation Instructions
# On Linux you can copy this file to /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options
# (@localstatedir@ for this installation) or to
# ~/.my.cnf to set user-specific options.
# On Windows you should keep this file in the installation directory
# of your server (e.g. C:\Program Files\MySQL\MySQL Server X.Y). To
# make sure the server reads the config file use the startup option
# "--defaults-file".
# To run run the server from the command line, execute this in a
# command line shell, e.g.
# mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
# To install the server as a Windows service manually, execute this in a
# mysqld --install MySQLXY --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
# And then execute this in a command line shell to start the server, e.g.
# net start MySQLXY
# Guildlines for editing this file
# In this file, you can use all long options that the program supports.
# If you want to know the options a program supports, start the program
# with the "--help" option.
# found in the manual.
# CLIENT SECTION
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
[client]
[mysql]
# SERVER SECTION
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
#Path to installation directory. All paths are usually resolved relative to this.
#Path to the database root
# The default character set that will be used when a new schema or table is
# created and no character set is defined
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
# The maximum amount of concurrent sessions the MySQL server will
# allow. One of these connections will be reserved for a user with
# SUPER privileges to allow the administrator to login even if the
# connection limit has been reached.
# Query cache is used to cache SELECT results and later return them
# without actual executing the same query once again. Having the query
# cache enabled may result in significant speed improvements, if your
# have a lot of identical queries and rarely changing tables. See the
# "Qcache_lowmem_prunes" status variable to check if the current value
# is high enough for your load.
# Note: In case your tables change very often or if your queries are
# textually different every time, the query cache may result in a
# slowdown instead of a performance improvement.
# The number of open tables for all threads. Increasing this value
# increases the number of file descriptors that mysqld requires.
# Therefore you have to make sure to set the amount of open files
# section [mysqld_safe]
# Maximum size for internal (in-memory) temporary tables. If a table
# grows larger than this value, it is automatically converted to disk
# based table This limitation is for a single table. There can be many
# of them.
# How many threads we should keep in a cache for reuse. When a client
# disconnects, the client's threads are put in the cache if there aren't
# more than thread_cache_size threads from before. This greatly reduces
# the amount of thread creations needed if you have a lot of new
# connections. (Normally this doesn't give a notable performance
# improvement if you have a good thread implementation.)
#*** MyISAM Specific options
# The maximum size of the temporary file MySQL is allowed to use while
# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.
# If the file-size would be bigger than this, the index will be created
# through the key cache (which is slower).
myisam_max_sort_file_size=100G
# If the temporary file used for fast index creation would be bigger
# than using the key cache by the amount specified here, then prefer the
# key cache method. This is mainly used to force long character keys in
# large tables to use the slower key cache method to create the index.
# Size of the Key Buffer, used to cache index blocks for MyISAM tables.
# is also required by the OS to cache rows. Even if you're not using
# used for internal temporary disk tables.
# Size of the buffer used for doing full table scans of MyISAM tables.
# Allocated per thread, if a full scan is needed.
# This buffer is allocated when MySQL needs to rebuild the index in
# REPAIR, OPTIMZE, ALTER table statements as well as in LOAD DATA INFILE
# into an empty table. It is allocated per thread so be careful with
# large settings.
#*** INNODB Specific options ***
# Use this option if you have a MySQL server with InnoDB support enabled
# but you do not plan to use it. This will save memory and disk space
# and speed up some things.
#skip-innodb
# Additional memory pool that is used by InnoDB to store metadata
# information. If InnoDB requires more memory for this purpose it will
# start to allocate it from the OS. As this is fast enough on most
# recent operating systems, you normally do not need to change this
# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# logs. Value 0 means that the log is only written to the log file and
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
innodb_flush_log_at_trx_commit=1
# The size of the buffer InnoDB uses for buffering log data. As soon as
# it is full, InnoDB will have to flush it to disk. As it is flushed
# once per second anyway, it does not make sense to have it very large
# (even with long transactions).
# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
# row data. The bigger you set this the less disk I/O is needed to
# access data in tables. On a dedicated database server you may set this
# too large, though, because competition of the physical memory may
# set it too high.
# Size of each log file in a log group. You should set the combined size
# unneeded buffer pool flush activity on log file overwrite. However,
# note that a larger logfile size will increase the time needed for the
# recovery process.
# Number of threads allowed inside the InnoDB kernel. The optimal value
# depends highly on the application, hardware as well as the OS
# scheduler properties. A too high value may lead to thread thrashing.
解决mysql耗内存的具体方法二:
更改后如下:
另外mysql安装目录下有几个文件:my-huge.ini 、my-large.ini、my-medium.ini...这几个是根据内存大小作的建议配置,新手在设置的时候也可以参考一下.
innodb_flush_log_at_trx_commit=0 等到innodb_log_buffer_size列队满后再统一储存,默认为1
试验一:
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=1M
试验二:
一般:
经过测试.没有特殊情况,最好还是用默认的.
(设置为0就是等到innodb_log_buffer_size列队满后再统一储存,默认为1)
优化mysql数据库性能的十个参数
(1)、max_connections:
要求 mysql 能有的连接数量.当主要mysql线程在一个很短时间内得到非常多的连接请求,这就起作用,然后主线程花些时间(尽管很短)检查连接并且启动一个新线程.
back_log 值指出在mysql暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中.只有如果期望在一个短时间内有很多连接,你需要增加它,换句话说,这值对到来的tcp/ip连接的侦听队列的大小.你的操作系统在这个队列大小上有它自己的限制.试图设定back_log高于你的操作系统的限制将是无效的.
用mysql进行搜索,目的是能不分大小写,又能用中文进行搜索
(10)、wait_timeout:
注:参数的调整可以通过修改 /etc/my.cnf 文件并重启 mysql 实现.这是一个比较谨慎的工作,上面的结果也仅仅是我的一些看法,你可以根据你自己主机的硬件情况(特别是内存大小)进一步修改.
以上就是土嘎嘎小编为大家整理的mysql怎么用内存库相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!