当前位置 : database

标签 database 下的文章

What to tune in MySQL Server after installation

My favorite question during Interview for people to work as MySQL DBAs or be involved with MySQL Performance in some way is to ask them what should be tuned in MySQL Server straight after installation, assuming it was installed with default settings.

I’m surprised how many people fail to provide any reasonable answer to this question, and how many servers are where in wild which are running with default settings.

Even though you can tune quite a lot of variables in MySQL Servers only few of them are really important for most common workload. After you get these settings right other changes will most commonly offer only incremental performance improvements.

[b]key_buffer_size[/b] - Very important if you use MyISAM tables. Set up to 30-40% of available memory if you use MyISAM tables exclusively. Right size depends on amount of indexes, data size and workload - remember MyISAM uses OS cache to cache the data so you need to leave memory for it as well, and data can be much larger than indexes in many cases. Check however if all of key_buffer is used over time - it is not rare to see key_buffer being set to 4G while combined size of .MYI files is just 1GB. This would be just a waste. If you use few MyISAM tables you’ll want to keep it lower but still at least 16-32Mb so it is large enough to accommodate indexes for temporary tables which are created on disk.

[b]innodb_buffer_pool_size[/b] This is very important variable to tune if you’re using Innodb tables. Innodb tables are much more sensitive to buffer size compared to MyISAM. MyISAM may work kind of OK with default key_buffer_size even with large data set but it will crawl with default innodb_buffer_pool_size. Also Innodb buffer pool caches both data and index pages so you do not need to leave space for OS cache so values up to 70-80% of memory often make sense for Innodb only installations. Same rules as for key_buffer apply - if you have small data set and it is not going to grow dramatically do not oversize innodb_buffer_pool_size you might find better use for memory available.

[b]innodb_additional_pool_size[/b] This one does not really affect performance too much, at least on OS with decent memory allocators. Still you might want to have it 20MB (sometimes larger) so you can see how much memory Innodb allocates for misc needs.

[b]innodb_log_file_size[/b] Very important for write intensive workloads especially for large data sets. Larger sizes offer better performance but increase recovery times so be careful. I normally use values 64M-512M depending on server size.

[b]innodb_log_buffer_size[/b] Default for this one is kind of OK for many workloads with medium write load and shorter transactions. If you have update activity spikes however or work with blobs a lot you might want to increase it. Do not set it too high however as it would be waste of memory - it is flushed every 1 sec anyway so you do not need space for more than 1 sec worth of updates. 8MB-16MB are typically enough. Smaller installations should use smaller values.

[b]innodb_flush_logs_at_trx_commit[/b] Crying about Innodb being 100 times slower than MyISAM ? You probably forgot to adjust this value. Default value of 1 will mean each update transaction commit (or each statement outside of transaction) will need to flush log to the disk which is rather expensive, especially if you do not have Battery backed up cache. Many applications, especially those moved from MyISAM tables are OK with value 2 which means do not flush log to the disk but only flush it to OS cache. The log is still flushed to the disk each second so you normally would not loose more than 1-2 sec worth of updates. Value 0 is a bit faster but is a bit less secure as you can lose transactions even in case MySQL Server crashes. Value 2 only cause data loss with full OS crash.

[b]table_cache[/b] - Opening tables can be expensive. For example MyISAM tables mark MYI header to mark table as currently in use. You do not want this to happen so frequently and it is typically best to size your cache so it is large enough to keep most of your tables open. It uses some OS resources and some memory but for modern hardware it is typically not the problem. 1024 is good value for applications with couple hundreds tables (remember each connection needs its own entry) if you have many connections or many tables increase it larger. I’ve seen values over 100.000 used.

[b]thread_cache[/b] Thread creation/destructions can be expensive, which happen at each connect/disconnect. I normally set this value to at least 16. If application has large jumps in amount of concurrent connections and I see fast growth of
Threads_Created variable I boost it higher. The goal is not to have threads created in normal operation.

[b]query_cache[/b] If your application is read intensive and you do not have application level caches this can be great help. Do not set it too large as it may slow things down as its maintenance may get expensive. Values from 32M to 512M normally make sense. Check it however after a while and see if it is well used. For certain workloads cache hit ratio is lower than would justify having it enabled.

Note: as you can see all of these are global variables. These variables depend on hardware and mix of storage engines, while per session variables are typically workload specific. If you have simple queries there is no reason to increase sort_buffer_size even if you have 64GB of memory to waste. Furthermore doing so may decrease performance.
I normally leave per session variable tuning to second step after I can analyze workload.

P.S Note MySQL distribution contains bunch of sample my.cnf files which may be great templates to use. Typically they would already be much better than defaults if you chose correct one.

Oracle 10gR2 + RedHat Enterprise Linux AS4 安装

Oracle 10gR2 + RedHat Enterprise Linux AS4 安装
(1)安装linux ,所由服务都不选择,只是选择安装开发工具,不要安装防火墙,不然有的端口没打开会提示连接超时.
(2)打开终端,执行如下命令,检查安装包,没有的都要安装(一般在dis2 和dis3上)

rpm -q make gcc glibc compat-db compat-gcc compat-gcc-c++ compat-libstdc++ compat-libstdc++-devel openmotif21 setarch libaio

(3)创建 Oracle 组和用户帐户

# /usr/sbin/groupadd oinstall
# /usr/sbin/groupadd dba
# /usr/sbin/useradd -m -g oinstall -G dba oracle
# id oracle
uid=501(oracle) gid=501(oinstall) groups=501(oinstall),502(dba)

设置 oracle 帐户的口令:

passwd oracle

- 阅读剩余部分 -

专家答疑:SQL Server开发问题前十名

在SQL Server中进行开发会让你身处险地,并且寻找快速解决方案。我们编辑了前十名关于SQL Server开发的常见问题。对常见的针对表和字段的名字约束进行探究。学习如何解决并发问题,并且在不了解T-SQL的情况下编写存储过程。查看这些解决方案的快速参考,并且了解更多内容。  花点时间来会见我们的全体专家,然后看看有关SQL Server主体的全体列表. 

Take a moment to meet all of our experts and view the complete list of SQL Server topics.

你可能会问的前十名的SQL Server开发问题:



3、T-SQL 中如何比较CLR 存储过程和函数的性能?


5、我如何解决SQL Server 2005的并发问题?

6、在SQL Server 2005中用什么工具替代了查询分析器?

7、C你能提供一些有关SQL 和T-SQL的详细信息吗?

8、SQL Server 2005 有没有新的索引类型?





SQL Server 2000下的表和字段名称有1到128字节的限制,并且遵循用于标识的规则。


? Unicode Standard 2.0中规定的字母。


? 下划线(_),at符号(@),或者数字符号(#)

在SQL Server中以这些符号作为标识符的开始具有特殊的含义。一个以at符号(@)开头的标识符表示一个本地的变量或者参数。一个以数字符号(#)开头的标识符代表一个临时表或者过程。一个以两个数字符号(##)开头的标识符标识的是一个全局临时对象。



? Unicode Standard 2.0定义的字母

? 来自基础拉丁文或者其他语音的十进制数字

? at符号(@),美元符号($),数字符号(#),或者下划线

标识符绝对不能是Transact-SQL的保留字。SQL Server保留了一些大写和小写的保留字。内建的空间或者特殊的字母都不允许出现,尽管你可以在好的老版本的Northwind中看到它们包含了内建的空间。你必须通过把它们括在括号中才可以访问。

我可以在不具有任何T-SQL知识的情况下编写SQL Server2005存储过程吗?

作为过去几年里面微软试图用SQL Server 2005的.NET集成来称霸市场的野心的结果,许多程序员都认为创建SQL Server存储过程不再必需T-SQL了。不幸的是(或者并非如此,这根据你的观点),这并不全是事实。在技术上是可以在不了解T-SQL的情况下创建存储过程的,但是没有T-SQL的话则无法访问任何的数据。

在CLR存储过程内部进行数据访问,是通过使用标准的ADO.NET类来完成的。开发人员会在应用程序层发现很多同样的没有用处的数据访问代码,这些代码会很轻易地转移到SQLCLR例程中去。当中间层的这些ADO.NET类需要使用T-SQL来访问数据的时候,在 CLR主机提供的环境中就会使用同样的类。

我要强调的是,从技术角度来说,不使用T-SQL来编写存储过程是可能的。那么有没有理由这么做呢?一种情况就是这是一个用来从普通文件或者网络服务中检索数据的CLR存储过程,并将数据格式设置为行集。这里可能就会用到不需要T-SQL的操作――但是这并不是对T -SQL存储过程能力的一个很好的比喻。

CLR存储过程vs. T-SQL存储过程





SQL Server 的专家Gustavo Larriera编辑了如下一些关于这个主题的有用链接:

?在 SQL Server 2005中使用CLR Integration

?简单介绍在 SQL Server 2005中的 CLR Integration

?在 SQL Server 2005中对CLR 和T-SQL做出选择

?介绍 SQL Server 2005中的 CLR Integration

?SQL Server 闲谈: SQL Server 2005 Beta 2 CLR问题与回答




我编写了一个存储过程在Crystal Report中使用。它工作得很好。我还想在另外一个存储过程中使用这个存储过程产生的结果。我该怎么做?


只要存储过程只产生了一个单个的结果,要在另外一个存储过程中使用这个存储过程产生的输出,这个技术是非常直接的。这个技术就是使用一个临时表来装载存储过程的巨额iguo,然后通过INSERT EXEC语句来执行这个过程并保存结果。一旦结果保留在临时表中了,他们就可以像使用其它表数据一样来使用它了。

CREATE PROC usp_Demo_AllAuthors as select * from pubs..authors

CREATE proc usp_Demo_SPUser as CREATE TABLE #Authors (
au_lname varchar (40) NOT NULL ,
au_fname varchar (20) NOT NULL ,
phone char (12) NOT NULL,
address varchar (40) NULL ,
city varchar (20) NULL ,
state char (2) NULL ,
zip char (5) NULL ,
contract bit NOT NULL
)C Execute usp_Demo_AllAuthors storing the
C results in #Authors
insert into #Authors
exec usp_Demo_AllAuthorsC Here we use the #Authors table. This example only
C only selects from the temp table but you could do much
C more such as use a cursor on the table or join with
C other data.
SELECT au_fName + ‘ ‘ + au_lname as [name]
, address+’, ‘+city+’, ‘+state+’ ‘+zip [Addr]
from #AuthorsDROP TABLE #Authors

SQL Server 2005中的存储过程并发问题


我在SQL Server2005中遇到了并发问题。我持有车票的公共汽车上有一些空闲的座位。我在插入销售的查票之前,需要查看是否还有空闲的座位。我的存储过程做的事情如下所示:

CREATE PROCEDURE add_ticket ― parameters DECLARE free_seats int BEGIN TRANSACTION SELECT free_seats = COUNT(*) FROM tickets WHERE seat_is_not_taken IF free_seats <> 0 INSERT INTO tickets VALUES(…) ― some other statements END TRANSACTION

问题就是两个过程可以同时读取空闲票数,并且都可以预约一张票,即使是那里已经没有空余的了。我需要一种方法来防止一个过程在另一个过程运行add_ticket程序,但是还没有插入一张新票的时候读取空票的数量。在这种情况下,SET TRANSACTION ISOLATION LEVEL不管用了,我说的对吗?


你是正确的;更高的隔离级别也不会保证多个读者去同时去读取同一个数据行。然而,还有几种方法你可以完成这项工作。例如,你可以给每个座位分配一个惟一的标识符(意思是,惟一键――不一定是GUID),并且创建一个描述哪些座位已经被预订了的表。在表上放一个 UNIQUE约束,你就可以确保同一个座位不会被插入两次了。

就是说,我认为一个更有趣的方法就是使用SQL Service Broker。你可以为每个公交建立一个会话,并且将这个会话的句柄存放在一个表中,读者在执行RECEIVE之前可以参考这个表。通过这种方式,读者就可以正确地过滤。公共汽车上的每个座位都插一个消息到队列中。读者就可以简单地RECEIVE到所需的消息(在这个过程中,预定公共汽车上的座位)。 Service Broker会确保没有消息会被接受两次,也就是说你不会再遇到并发问题了。

SQL Server2005中取代了查询分析器


我知道SQL Server2005中不再会有查询分析器了。那么还有什么工具具有类似的功能?


你是对的。查询分析器和企业管理器都从SQL Server中删除了。取代它们的是一个工具,SQL Server 管理套件(SQL Server Management Studio)。这个工具具有前任的大多数特性,但是拥有升级后的用户界面和很多经过改善的功能。我觉得大多数的数据库管理员都会发现这是一个很好的升级。

如果你想要了解更多有关SQL Server管理套件的新特性的信息,请阅读SearchSQLServer.com 上有关这个话题的文章。



亲爱的Adam Machanic:你能给我一些有关SQL和 T-SQL相比较的详细信息吗?使用这两者之间的区别和分别的好处是什么?


SQL是结构化查询语言,是ANSI/ISO 认可的标准数据库语言。SQL Server的实现语言叫做Transact-SQL (T-SQL)。T-SQL基本上是根据1992年发表的ISO标准出现的,在1999年的标准上稍加修改。此外,微软还进行了各种私有的加强。

标准SQL和T-SQL之间有很多区别――太多了,这里就不说了。还有,如果你在SQL Server上工作,那么使用这些私有的扩展是有好处的。由于许多SQL Server的特性的本质,你不使用非标准的命令的话,将会有很多强大的功能无法实现。如果你想要看看你的SQL是否符合标准,你可以使用SET FIPS_FLAGGER命令。

SQL Server 2005中的索引类别


SQL Server 2005中是否有新的索引类别了?


SQL Server 2005没有为关系表引入新的索引类型。基本上――聚簇和非聚簇索引是以B-trees的方式实现的――还仍然在应用。然而,SQL Server 2005确实包含了一些索引上的加强,不论是完全文本索引,还是对于XML数据,此外这些加强还可以改善一些与关系型索引有关的问题。

SQL Server 2005的完全文本索引特性是全新的,并且是重新编写的。要获得这个特性的信息,请阅读Nimish Khanolkar的MSDN广播文档,介绍SQL Server 2005中的全文本查找。

XML是SQL Server 2005中另外一个在方式上发生了巨大转变的内容。现在对于开发人员来说有第一流的XML数据类型可用了。这个类型支持XQuery查询语言,使用了这个类型的字段可以通过特殊格式的XML索引被索引到。要了解更多有关XML类型的信息,请查找MSDN 上的文章。

还有各种各样的有关T-SQL索引命令的加强。也许最令人感兴趣的就是新的“在线”索引类型,它允许数据库管理员在不需要把用户锁到表外面的情况下执行索引维护任务。这个很有可能标记着数据库管理员需要等到夜里3点才能打开维护窗口修正问题的状况的终结!要了解更多有关这个特性的信息,请查找SQL Server Worldwide Users Group 上的文章。



prod_key item_key pack_key last_sale
LM001 1029 AD100 2004/12/05
LM870 1029 AD100 2005/09/20
PE789 1030 BC400 2003/07/12
PE312 1030 BC400 2004/08/07

我想要选出哪些在item_key 和pack_key相等的情况下,日期比较大的那一行。换句话说,我想要:
LM870 1029 AD100 2005/09/20
PE312 1030 BC400 2004/08/07



SELECT prod_key,item_key,pack_key,last_sale
FROM (SELECT item_key,pack_key,MAX(last_sale) AS last_sale FROM tablex GROUP BY item_key,pack_key) AS MaxDateTable
WHERE tablex.item_key = MaxDateTable.item_key
AND tablex.pack_key = MaxDateTable.pack_key
AND tablex.last_sale = MaxDateTable.last_sale



我创建了一个动态的SQL Server查询来输出表中行的数量。我的目标是列出在数据库中没有记录的表。查询如下所示:
declare @strsql varchar(100)
declare @tablename varchar(50)
@tablename=’table123′@strsql=’select count(*) from ‘ + @tablename exec(@strsql)


/* @countvariable=0



declare @strsql varchar(256)
create table #emptytables (tablename varchar(128), table_rowcount int)select @strsql=’select distinct o.name as TableName, x.rowcnt as Table_RowCount
from sysobjects o
inner join sysindexes x
on o.id = x.id
where x.rowcnt = 0 and
o.type = ‘’U”’insert #emptytables (TableName, Table_rowcount) exec (@strsql)
select * from #emptytables
drop table #emptytables