网站访问量大 怎样优化mysql数据库

网站访问量大 怎样优化mysql数据库,第1张

如果你有高访问量的网站,可以考虑优化MySQL数据库,以提高性能和流畅度。以下是一些优化MySQL数据库的方法:1 使用索引:索引可以帮助查询过程更快地检索数据。尽量使用单列索引而不是复合索引,因为复合索引增加了查询的复杂度。2 使用缓存:使用缓存可以减少数据库的访问次数,提高查询速度。可以使用内存缓存或外部缓存,例如Memcached。3 优化查询:优化查询可以减少数据库的负担,提高查询速度。可以使用EXPLN来分析查询语句的执行计划,并对语句进行优化。4 使用合适的数据类型:使用合适的数据类型可以减少存储空间和读取时间,提高查询速度。例如,使用整数类型代替字符串类型存储数字数据。5 定期清理数据:定期清理数据可以减少数据库的负担,提高查询速度。可以使用定时任务或脚本自动清理数据。6 配置合适的数据库服务器:使用合适的数据库服务器可以提高数据库性能和稳定性。例如,使用高性能的硬件和网络设备。7 避免使用SQL语句的通配符:通配符可以匹配所有符合条件的数据,但是会增加查询的复杂度和执行时间。尽量避免在查询语句中使用通配符。

为此,数据库引擎获取尽可能多的可用内存,同时保留足够的可用内存以防 *** 作系统交换内存。SQL Server 实例在启动时通常获取8到12MB的内存以完成初始化过程。当实例完成初始化后,就不会再获取更多的内存,直到用户连接到该实例并开始产生工作负荷。这时,该实例根据需要不停地获取内存以支持工作负荷。随着更多的用户连接并运行查询,SQL Server 将获取支持需求所需的额外内存。该实例将继续获取内存直到达到自身的内存分配目标,并且直到达到该目标的下限才会释放任何内存。为了在不产生多余换页 I/O 的情况下获取尽可能多的内存,SQL Server 的每个实例都配置一个内存获取目标,直到电脑的可用物理内存在4MB到10MB的范围内。之所以选择该范围是因为测试表明 Windows NT 和 Windows 2000 都有最小内存交换,直到内存分配等于可用物理内存减去4MB。工作负荷处理任务重的 SQL Server 实例保留的可用物理内存为范围的较低端 (4MB);工作负荷处理任务轻的实例保留的可用物理内存为范围的较高端 (10MB)。SQL Server 实例的目标随工作负荷的改变而变化。当更多的用户连接并产生更多的工作时,该实例倾向于获取更多的内存以使可用的内存保持在4MB的限制以下。当工作负荷减轻时,该实例将其目标调整为 10MB的可用空间,并释放内存给 *** 作系统。将可用空间量保持在10MB和4MB之间可防止 Windows NT 或 Windows 2000 过多执行换页 *** 作,同时使 SQL Server 得以获得尽可能最大的高速缓冲存储器而不至引起额外的交换。实例的目标内存配置和数据库缓冲池的页相对于可用池大小的需求有关。在任何实时点,缓冲区页的总需求取决于满足任何当前执行的查询所需的数据页数。假如相对于高速缓冲存储器内的页数,数据页的需求很大,则当前在缓冲区内的每一页很可能在相对较短的时间内由新页替换。这可由缓冲区管理器对象的页生命期性能计数器来度量。对于相对较小的缓冲区有较高需求的情况将生成短生命期,而纯粹的影响就是使 I/O 增加,因为在页可由多个逻辑读取引用之前往往要被重写。为减轻这个问题,数据库引擎能够获取更多的内存以增加高速缓冲存储器的大小。当页生命期长时,数据库引擎将可用内存定位于目标的高端 (10 MB);而当页生命期短时,数据库引擎定位于目标范围的低端 (4MB)。随着其他应用程式在运行 SQL Server 实例的电脑上启动,他们消耗内存致使可用物理内存量降到 SQL Server 的目标以下。SQL Server 实例于是从其地址空间释放足够内存,以使可用内存量回到 SQL Server 的目标。假如有其他应用程式停止运行而使可用内存增多,SQL Server 实例将增加其内存分配大小。SQL Server 能够每秒释放并获取几MB字节的内存,这使他得以根据内存分配变化作出快速调整。您能够通过配置允许sql server能够使用的最大内存来做限制:最小和最大服务器内存的影响min server memory 和 max server memory 配置选项建立由 SQL Server 数据库引擎使用的内存量的上限和下限。数据库引擎并不立即获取 min server memory 中指定的内存量。数据库引擎启动时只使用初始化所需的内存。随着数据库引擎工作负荷的增加,他将继续获取支持工作负荷所需的内存。数据库引擎直到到达 min server memory 中指定的内存量才会释放任何所需的内存。一旦到达 min server memory,数据库引擎将使用标准算法(使 *** 作系统的可用内存保持在 4MB到 10MB之间)获取和释放所需内存。唯一的区别是数据库引擎从不将内存分配降到 min server memory 所指定的水平下,也从不获取超过max server memory 所指定水平的内存。数据库引擎获取的内存量完全取决于放置在实例上的工作负荷。不处理很多请求的 SQL Server 实例可能永远达不到 min server memory。假如为 min server memory 和 max server memory 指定相同的值,则一旦分配给数据库引擎的内存达到该值,数据库引擎将停止动态释放和获取内存。假如在运行 SQL Server 实例的电脑上频繁启动或停止其他应用程式,启动这些应用程式所需的时间可能会因 SQL Server 实例分配和释放内存而延长。另外,假如 SQL Server 是几个在一台电脑上运行的服务器应用程式中的一个,系统管理员可能需要控制分配给 SQL Server 的内存量。

MySQL数据查询开始慢的量级取决于多个因素,包括但不限于以下几点:
1 数据库大小:随着数据库大小的增加,查询开始慢的可能性也会增加。
2 数据库结构:如果数据库结构复杂,包含多个表、索引、视图等,查询开始慢的可能性也会增加。
3 查询语句复杂度:如果查询语句包含多个表连接、子查询、聚合函数等,查询开始慢的可能性也会增加。
4 数据库服务器配置:如果数据库服务器配置不足,例如内存、CPU等资源不足,查询开始慢的可能性也会增加。
一般来说,当数据库表的数据量达到数百万条或以上时,查询开始慢的可能性就会增加。但具体情况还需要根据实际情况进行评估。

这个问题在PHP的官方网站上叫缓冲查询和非缓冲查询(Buffered and Unbuffered queries)。PHP的查询缺省模式是缓冲模式。也就是说,查询数据结果会一次全部提取到内存里供PHP程序处理。这样给了PHP程序额外的功能,比如说,计算行数,将指针指向某一行等。更重要的是程序可以对数据集反复进行二次查询和过滤等 *** 作。但这种缓冲查询模式的缺陷就是消耗内存,也就是用空间换速度。
相对的,另外一种PHP查询模式是非缓冲查询,数据库服务器会一条一条的返回数据,而不是一次全部返回,这样的结果就是PHP程序消耗较少的内存,但却增加了数据库服务器的压力,因为数据库会一直等待PHP来取数据,一直到数据全部取完。
很显然,缓冲查询模式适用于小数据量查询,而非缓冲查询适应于大数据量查询。

mysql耗内存吗很多人都说MySQL占用了很大的虚拟内存,那么这个问题应该怎么解决呢下面是我收集整理的一些方法,现在分享给大家!

解决mysql耗内存的具体方法一:

在分析的过程中发现最耗内存的是MySQL,其中近1GB的内存被它吞了,而且不在任务管理器体现出来。这个数据库软件是EMS要用到了,所以必须要运行。这个软件在安装的时候会根据机器的实际内存自动进行配置,PC机物理内存越多,它默认占有的内存就越多,难怪3GB的内存被它给吞了近1GB。

优化方法:

1 退出EMS client&server

2 在CMD里运行:net stop mysql

3 找到MySQL\MySQL Server的安装目录,里面有个myini文件,参考附件的配置对参数query_cache_size tmp_table_size myisam_sort_buffer_size key_buffer_size innodb_buffer_pool_size进行修改,注意不要改动innodb_log_file_size,修改前备份myini

4 在CMD里运行:net start mysql,如果提示成功,则说明修改的参数没有什么问题,如果失败,重新调整一下上面的参数

5 找到EMS 安装目录runGUIbat runServerbat脚本,找到-Xmx700m,改为-Xmx256m,注意修改前备份这两个文件,感谢Liping Sun提供帮助

6 重新运行EMS

前后对比,对于3GB的PC,发现可以节省近1GB的内存。对于2GB的PC,也可以节省600-800MB。优化后发现EMS启动稍微慢一些,但是其它的软件运行速度提高了很多,不在经常出现卡机现象了。如果在运行过程中发现EMS特别慢的话,自己也可以适当放大上面提到的一些参数。

myini

# MySQL Server Instance Configuration File

# ----------------------------------------------------------------------

# Generated by the MySQL Server Instance Configuration Wizard

#

#

# Installation Instructions

# ----------------------------------------------------------------------

#

# On Linux you can copy this file to /etc/mycnf to set global options,

# mysql-data-dir/mycnf to set server-specific options

# (@localstatedir@ for this installation) or to

# ~/mycnf to set user-specific options

#

# On Windows you should keep this file in the installation directory

# of your server (eg C:\Program Files\MySQL\MySQL Server XY) 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, eg

# mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server XY\myini"

#

# To install the server as a Windows service manually, execute this in a

# command line shell, eg

# mysqld --install MySQLXY --defaults-file="C:\Program Files\MySQL\MySQL Server XY\myini"

#

# And then execute this in a command line shell to start the server, eg

# 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

#

# More detailed information about the individual options can also be

# 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]

port=3306

[mysql]

default-character-set=utf8

# 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

port=3306

#Path to installation directory All paths are usually resolved relative to this

basedir="D:/Program Files/MySQL/MySQL Server 51/"

#Path to the database root

datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 51/Data/"

# The default character set that will be used when a new schema or table is

# created and no character set is defined

character-set-server=utf8

# 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

max_connections=1510

# 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

query_cache_size=16M

# 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

# allowed to at least 4096 in the variable "open-files-limit" in

# section [mysqld_safe]

table_cache=3020

# 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

tmp_table_size=4M

# 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)

thread_cache_size=64

# 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

myisam_sort_buffer_size=4M

# Size of the Key Buffer, used to cache index blocks for MyISAM tables

# Do not set it larger than 30% of your available memory, as some memory

# is also required by the OS to cache rows Even if you're not using

# MyISAM tables, you should still set it to 8-64M as it will also be

# used for internal temporary disk tables

key_buffer_size=16M

# Size of the buffer used for doing full table scans of MyISAM tables

# Allocated per thread, if a full scan is needed

read_buffer_size=64K

read_rnd_buffer_size=256K

# 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

sort_buffer_size=256K

# 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

# value SHOW INNODB STATUS will display the current amount used

innodb_additional_mem_pool_size=9M

# 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

# transactions, you may set this to 0 or 2 to reduce disk I/O to the

# logs Value 0 means that the log is only written to the log file and

# the log file flushed to disk approximately once per second Value 2

# 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_log_buffer_size=5M

# 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

# parameter up to 80% of the machine physical memory size Do not set it

# too large, though, because competition of the physical memory may

# cause paging in the operating system Note that on 32bit systems you

# might be limited to 2-35G of user level memory per process, so do not

# set it too high

innodb_buffer_pool_size=32M

# Size of each log file in a log group You should set the combined size

# of log files to about 25%-100% of your buffer pool size to avoid

# 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

innodb_log_file_size=88M

# 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

innodb_thread_concurrency=8

解决mysql耗内存的具体方法二:

更改后如下:

innodb_buffer_pool_size=576M ->256M InnoDB引擎缓冲区占了大头,首要就是拿它开刀

query_cache_size=100M ->16M 查询缓存

tmp_table_size=102M ->64M 临时表大小

key_buffer_size=256m ->32M

重启mysql服务后,虚拟内存降到200以下

另外mysql安装目录下有几个文件:my-hugeini 、my-largeini、my-mediumini这几个是根据内存大小作的建议配置,新手在设置的时候也可以参考一下。

2G内存的MYSQL数据库服务器 myini优化 (myini)

2G内存,针对站少,优质型的设置,试验特:

table_cache=1024 物理内存越大,设置就越大默认为2402,调到512-1024最佳

innodb_additional_mem_pool_size=8M 默认为2M

innodb_flush_log_at_trx_commit=0 等到innodb_log_buffer_size列队满后再统一储存,默认为1

innodb_log_buffer_size=4M 默认为1M

innodb_thread_concurrency=8 你的服务器CPU有几个就设置为几,默认为8

key_buffer_size=256M 默认为218 调到128最佳

tmp_table_size=64M 默认为16M 调到64-256最挂

read_buffer_size=4M 默认为64K

read_rnd_buffer_size=16M 默认为256K

sort_buffer_size=32M 默认为256K

max_connections=1024 默认为1210

试验一:

table_cache=512或1024

innodb_additional_mem_pool_size=2M

innodb_flush_log_at_trx_commit=0

innodb_log_buffer_size=1M

innodb_thread_concurrency=8 你的服务器CPU有几个就设置为几,默认为8

key_buffer_size=128M

tmp_table_size=128M

read_buffer_size=64K或128K

read_rnd_buffer_size=256K

sort_buffer_size=512K

max_connections=1024

试验二:

table_cache=512或1024

innodb_additional_mem_pool_size=8M

innodb_flush_log_at_trx_commit=0

innodb_log_buffer_size=4M

innodb_thread_concurrency=8

key_buffer_size=128M

tmp_table_size=128M

read_buffer_size=4M

read_rnd_buffer_size=16M

sort_buffer_size=32M

max_connections=1024

一般:

table_cache=512

innodb_additional_mem_pool_size=8M

innodb_flush_log_at_trx_commit=0

innodb_log_buffer_size=4M

innodb_thread_concurrency=8

key_buffer_size=128M

tmp_table_size=128M

read_buffer_size=4M

read_rnd_buffer_size=16M

sort_buffer_size=32M

max_connections=1024

经过测试没有特殊情况,最好还是用默认的

2G内存,针对站多,抗压型的设置,最佳:

table_cache=1024 物理内存越大,设置就越大默认为2402,调到512-1024最佳

innodb_additional_mem_pool_size=4M 默认为2M

innodb_flush_log_at_trx_commit=1

(设置为0就是等到innodb_log_buffer_size列队满后再统一储存,默认为1)

innodb_log_buffer_size=2M 默认为1M

innodb_thread_concurrency=8 你的服务器CPU有几个就设置为几,建议用默认一般为8

key_buffer_size=256M 默认为218 调到128最佳

tmp_table_size=64M 默认为16M 调到64-256最挂

read_buffer_size=4M 默认为64K

read_rnd_buffer_size=16M 默认为256K

sort_buffer_size=32M 默认为256K

max_connections=1024 默认为1210

thread_cache_size=120 默认为60

query_cache_size=64M

优化mysql数据库性能的十个参数

(1)、max_connections:

允许的同时客户的数量。增加该值增加 mysqld 要求的文件描述符的数量。这个数字应该增加,否则,你将经常看到 too many connections 错误。 默认数值是100,我把它改为1024 。

(2)、record_buffer:

每个进行一个顺序扫描的线程为其扫描的每张表分配这个大小的一个缓冲区。如果你做很多顺序扫描,你可能想要增加该值。默认数值是131072(128k),我把它改为16773120 (16m)

(3)、key_buffer_size:

索引块是缓冲的并且被所有的线程共享。key_buffer_size是用于索引块的缓冲区大小,增加它可得到更好处理的索引(对所有读和多重写),到你能负担得起那样多。如果你使它太大,系统将开始换页并且真的变慢了。默认数值是8388600(8m),我的mysql主机有2gb内存,所以我把它改为 402649088(400mb)。

4)、back_log:

要求 mysql 能有的连接数量。当主要mysql线程在一个很短时间内得到非常多的连接请求,这就起作用,然后主线程花些时间(尽管很短)检查连接并且启动一个新线程。

back_log 值指出在mysql暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中。只有如果期望在一个短时间内有很多连接,你需要增加它,换句话说,这值对到来的tcp/ip连接的侦听队列的大小。你的 *** 作系统在这个队列大小上有它自己的限制。试图设定back_log高于你的 *** 作系统的限制将是无效的。

当你观察你的主机进程列表,发现大量 264084 | unauthenticated user | xxxxxxxxxxxx | null | connect | null | login | null 的待连接进程时,就要加大 back_log 的值了。默认数值是50,我把它改为500。

(5)、interactive_timeout:

服务器在关闭它前在一个交互连接上等待行动的秒数。一个交互的客户被定义为对 mysql_real_connect()使用 client_interactive 选项的客户。 默认数值是28800,我把它改为7200。

(6)、sort_buffer:

每个需要进行排序的线程分配该大小的一个缓冲区。增加这值加速order by或group by *** 作。默认数值是2097144(2m),我把它改为 16777208 (16m)。

(7)、table_cache:

为所有线程打开表的数量。增加该值能增加mysqld要求的文件描述符的数量。mysql对每个唯一打开的表需要2个文件描述符。默认数值是64,我把它改为512。

(8)、thread_cache_size:

可以复用的保存在中的线程的数量。如果有,新的线程从缓存中取得,当断开连接的时候如果有空间,客户的线置在缓存中。如果有很多新的线程,为了提高性能可以这个变量值。通过比较 connections 和 threads_created 状态的变量,可以看到这个变量的作用。我把它设置为 80。

(9)mysql的搜索功能

用mysql进行搜索,目的是能不分大小写,又能用中文进行搜索

只需起动mysqld时指定 --default-character-set=gb2312

(10)、wait_timeout:

服务器在关闭它之前在一个连接上等待行动的秒数。 默认数值是28800,我把它改为7200。

注:参数的调整可以通过修改 /etc/mycnf 文件并重启 mysql 实现。这是一个比较谨慎的工作,上面的结果也仅仅是我的一些看法,你可以根据你自己主机的硬件情况(特别是内存大小)进一步修改。

我们仍然使用两个会话,一个会话 run,用于运行主 SQL;另一个会话 ps,用于进行 performance_schema 的观察:

主会话线程号为 29,

将 performance_schema 中的统计量重置,

临时表的表大小限制取决于参数  tmp_table_size 和 max_heap_table_size 中较小者,我们实验中以设置 max_heap_table_size 为例。

我们将会话级别的临时表大小设置为 2M(小于上次实验中临时表使用的空间),执行使用临时表的 SQL:

查看内存的分配记录:

会发现内存分配略大于 2M,我们猜测临时表会比配置略多一点消耗,可以忽略。

查看语句的特征值:

可以看到语句使用了一次需要落磁盘的临时表。

那么这张临时表用了多少的磁盘呢?

我们开启 performance_schema 中 waits 相关的统计项:

重做实验,略过。

再查看 performance_schema 的统计值:

可以看到几个现象:

1 临时表空间被写入了 792MiB 的数据。

2 这些数据是语句写入后,慢慢逐渐写入的。

来看看这些写入 *** 作的特征,该方法我们在 实验 03 使用过:

可以看到写入的线程是 page_clean_thread,是一个刷脏 *** 作,这样就能理解数据为什么是慢慢写入的。

也可以看到每个 IO *** 作的大小是 16K,也就是刷数据页的 *** 作。

结论:

我们可以看到,

1 MySQL 会基本遵守 max_heap_table_size 的设定,在内存不够用时,直接将表转到磁盘上存储。

2 由于引擎不同(内存中表引擎为 heap,磁盘中表引擎则跟随 internal_tmp_disk_storage_engine 的配置),本次实验写磁盘的数据量和 实验 05 中使用内存的数据量不同。

3 如果临时表要使用磁盘,表引擎配置为 InnoDB,那么即使临时表在一个时间很短的 SQL 中使用,且使用后即释放,释放后也会刷脏页到磁盘中,消耗部分 IO。

Microsoft SQL Server 2000 的内存管理组件消除了对 SQL Server 可用的内存进行手工管理的需要。SQL Server 在启动时根据 *** 作系统和其它应用程序当前正在使用的内存量,动态确定应分配的内存量。当计算机和SQL Server 上的负荷更改时,分配的内存也随之更改。有关更多信息,请参见内存构架。
下列服务器配置选项可用于配置内存使用并影响服务器性能:
min server memory
max server memory
max worker threads
index create memory
min memory per query

min server memory 服务器配置选项可用于确保 SQL Server 至少以最小的分配内存量启动,并且内存低于该值时不会释放内存。可以基于 SQL Server 的大小及活动将该配置选项设置为特定的值。始终将 min server memory 服务器配置选项设置为某个合理的值,以确保 *** 作系统不向 SQL Server 请求太多内存而影响 SQL Server 性能。
max server memory 服务器配置选项可用于:在 SQL Server 启动及运行时,指定 SQL Server 可以分配的最大内存量。如果知道有多个应用程序与 SQL Server 同时运行,而且想保障这些应用程序有足够的内存运行,可以将该配置选项设置为特定的值。如果这些其它应用程序(如 Web 服务器或电子邮件服务器)只根据需要请求内存,则 SQL Server 将根据需要给它们释放内存,因此不要设置 max server memory 服务器配置选项。然而,应用程序通常在启动时不假选择地使用可用内存,而如果需要更多内存也不请求。如果有这种行为方式的应用程序与 SQL Server 同时运行在相同的计算机上,则将 max server memory 服务器配置选项设置为特定的值,以保障应用程序所需的内存不由 SQL Server 分配出。
不要将 min server memory 和 max server memory 服务器配置选项设置为相同的值,这样做会使分配给 SQL Server 的内存量固定。动态内存分配可以随时间提供最佳的总体性能。有关更多信息,请参见服务器内存选项。
max worker threads 服务器配置选项可用于指定为用户连接到 SQL Server 提供支持的线程数。255 这一默认设置对一些配置可能稍微偏高,这要具体取决于并发用户数。由于每个工作线程都已分配,因此即使线程没有正在使用(因为并发连接比分配的工作线程少),可由其它 *** 作(如高速缓冲存储器)更好地利用的内存资源也可能是未使用的。一般情况下,应将该配置值设置为并发连接数,但不能超过 1,024。有关更多信息,请参见max worker threads 选项。
说明当 SQL Server 运行在 Microsoft Windowsreg; 95 或 Microsoft Windows 98 上时,最大工作线程服务器配置选项不起作用。
index create memory 服务器配置选项控制创建索引时排序 *** 作所使用的内存量。在生产系统上创建索引通常是不常执行的任务,通常调度为在非峰值时间执行的作业。因此,不常创建索引且在非峰值时间时,增加该值可提高索引创建的性能。不过,最好将 min memory per query 配置选项保持在一个较低的值,这样即使所有请求的内存都不可用,索引创建作业仍能开始。有关更多信息,请参见 index create memory 选项。
min memory per query 服务器配置选项可用于指定分配给查询执行的最小内存量。当系统内有许多查询并发执行时,增大 min memory per query 的值有助于提高消耗大量内存的查询(如大型排序和哈希 *** 作)的性能。不过,不要将 min memory per query 服务器配置选项设置得太高,尤其是在很忙的系统上,因为查询将不得不等到能确保占有请求的最小内存、或等到超过 query wait 服务器配置选项内所指定的值。如果可用内存比执行查询所需的指定最小内存多,则只要查询能对多出的内存加以有效的利用,就可以使用多出的内存。
附实例一篇:
最近,为了能在数据库服务器中运行其他应用程序,在保持数据库 *** 作系统版本不变的前提下对数据库服务器进行了软、硬件上的升级。在软件上,将 *** 作系统从Windows 2000升级到Windows Server 2003;在硬件上,将服务器中的内存由原来的512MB增加到1GB(1024MB)。
在升级后的开始几个星期之内,服务器在使用中表现良好。但是不久后就发现,在服务器上同时运行的其他应用程序却出现了异常,不时地报出内存分配不足的警告。经过几次跟踪后发现,原来是SQL Server吞去了大部分内存所致。被SQL Server占用的内存由升级前的不到400MB一下子增加到现在的900MB,并且有不断增长的趋势。
通过查找原因才知道这是SQL Server 缓冲池的预期行为。默认情况下,在启动 SQL Server之后,SQL Server会根据 *** 作系统报告的物理内存数来动态增大或缩小高速缓冲存储器的容量。只要可用物理内存大小保持在4MB到10MB之间,SQL Server 缓冲池就会继续增大(保留可用物理内存在4MB到10MB之间是为了避免 *** 作系统因为缺少内存而频繁地换页)。如果物理可用内存变得较少的时候,则SQL Server会将一些内存释放给 *** 作系统。
为了使运行在服务器上的应用程序都能达到比较满意的效果,同时也为了能给其他应用程序分配足够的内存,需要采取措施限制SQL Server 的内存使用量。我们可以通过设置SQL Server 数据库引擎使用的内存的上下限来达到此目的。其具体步骤是:
1打开企业管理器,展开服务器组。
2单击该服务器,点击鼠标右键,单击属性菜单。
3在d出的对话框中单击内存选项卡。
内存设置方法有两种:
1设置min server memory和max server memory 在一个范围段内。
比如,我们将它设置成最小0MB,最大255MB。这种方法在为一台服务器中运行多个应用程序分配内存时非常有用。
2设置 min server memory 和 max server memory 为同一数值。
比如,可以将它最大和最小值都设置成255MB。这样的设置方法与窗口中的另一个选项“使用固定的内存大小” 相一致。
虽然内存最小值和最大值设置是高级选项,但在设置完毕之后,最好还是先将SQL Server服务停止后再重新运行,以便SQL Server能更好地对内存进行合理安排。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/13377862.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存