Archive for November, 2008

Resource database has been moved to a new location in Sql Server 2008

A new resource database was introduced in Sql Server 2005, and it is still with us in Sql Server 2008. In both editions, you will not be able to see it via Sql Server Management Studio. This hidden database is required. Without it, Sql Server cannot start.

In Sql Server 2005, mssqlsystemresource.mdf and mssqlsystemresource.ldf are stored under the DATA folder, along with master, model, msdb, and tempdb. In Sql Server 2008, however, the two files have been moved to the Binn folder, where sqlservr.exe executable is located.

By the way, do you know why the folder is called Binn? Why two “n”s? There is a bit of history there. Back in the 16-bit days, all binary files were stored under the Bin folder. However, as things moved to 32-bit and Windows NT came to the market, this new folder, Binn, was created to accommodate that. The extra n was taken from NT, which stands for “New Technology”.

Comments (2)

What does syspolicy_purge_history job do

After a brand new Sql Server 2008 installation or upgrade from a previous version, a job will be created during the installation/upgrade process: syspolicy_purge_history. What does it do?

Before answering that question, here is a little background. Sql Server 2008 introduced a new feature called Policy Management. For example, one could define a policy that says all tables must have a clustered index. Once defined, the policy can be evaluated against targets to check for compliance. Each evaluation of compliance will be recorded in tables in msdb, syspolicy_policy_execution_history_details_internal, syspolicy_policy_execution_history_internal, and syspolicy_policy_category_subscriptions_internal, specifically. The evaluation condition is built on top of object facets, which is predefined by Microsoft.

The purpose of that job, syspolicy_purge_history, is to remove records in those tables to maintain so it does not pile up and take up space unnecessarily. It has three steps. The first step verifies if Policy Management is enabled. If yes, it will go to the second step, which deletes policy evaluation history prior to cutoff date, defined by HistoryRetentionInDays. If Policy Management is not enabled, the whole job stops with an error. I haven’t quite figured out what the third step does, which is a PowerShell step. It calls the PolicyStore.EraseSystemHealthPhantomRecords method, and no detailed document for it was available as of this writing.

By the way, here is the code to create AllTablesNeedClusteredIndex condition. Another thing that is worth knowing is that if you only want to check a condition against only user databases, not system databases, the database facet to check against is called IsSystemObject.

Declare @condition_id int
EXEC msdb.dbo.sp_syspolicy_add_condition @name=N'AllTablesNeedClusteredIndex', @description=N'', @facet=N'Table', @expression=N'<operator>
  <typeclass>Bool</typeclass>
  <optype>EQ</optype>
  <count>2</count>
  <attribute>
    <typeclass>Bool</typeclass>
    <name>HasClusteredIndex</name>
  </attribute>
  <function>
    <typeclass>Bool</typeclass>
    <functiontype>True</functiontype>
    <returntype>Bool</returntype>
    <count>0</count>
  </function>
</operator>', @is_name_condition=0, @obj_name=N'', @condition_id=@condition_id OUTPUT
Select @condition_id

GO

Comments

Thinking of Ken Henderson during PASS conference

Sql Server 2008 is here, and Microsoft is busy working on the next release. But let me tell you, there are still tons of Sql Server 2000 out there. So far this year, I spent well over 2/3 of my time working on Sql Server 2000 projects. Clients know they need to move to the next version, reluctantly. One of the major roadblock is SSIS. Companies have made huge investment in that technology and there are DTS packages all over the place. Sure, there is DTS Runtime, but upgrading the packages is a huge undertaking with a steep learning curve. Many people love DTS, because it really hits the sweet spot of simplicity and yet still being powerful enough to get things done.

Anyhow, since I work with Sql Server 2000 quite a bit, and may have to review or even code some Visual Basic 6 programs, I figured it was a good time to take a trip down memory lane: I started reading Ken Henderson’s The Guru’s Guide to SQL Server Architecture and Internals. I read it from cover to cover, with the exception of Notification Services, ODSOLE, and a few XML chapters. I’ve also compiled and ran many of his C Win32 API programs to get a better understanding of Windows internals. In addition, I had fun poking under the hood with WinDbg by following his exercises. (Earlier this year I did some exercise with the Linux debugger gdb, figuring out some simple problems with MySQL program written in C. In many ways, they are similar, based on my limited experience.) A hint for you if you decide to follow exercises in his book, for Chapter 3, 4, and 5, where he talked about processes and threads, memory, and I/O system, be sure to change the project to Debug mode. The code files from the CD put you in Release mode in Visual C++ 6.0.

Ken’s book got me interested in the impact of Solid State Disk on Sql Server. In Sql Server, UMS, or SQLOS, tries to keep things running as much as possible in user mode, as opposed to kernel mode, adopting a co-operative as opposed to preemptive model. And it uses mostly asynchronous I/O and scatter-gather I/O. With the emergence of Solid State Disk, I wonder what kind of impact, if any, there is for such an I/O intensive application as Sql Server.

Anyway, I really learned a lot from the book. Ken knew Sql Server inside out. Unfortunately, he passed away this year, and I wrote a tribute here.

I’ve learned from various sources on the web that Ken’s last book is more or less done before he passed away. I don’t know about you, I would love to read it. However, not wanting to be preposterous or disrespectful to his immediate families and friends, I’ve been quiet on it for a while. I did, however, sent an email to Addison-Wesley, asking the possibility of putting the book as Rough Cuts on Safari Books Online, but got no response from them.

In any event, with the PASS conference going on in Seattle now, I thought it is appropriate to honor Ken in some way during the event. Unfortunately I couldn’t make it to the conference due to schedule conflict. Like I said, Ken is such a wonderful, generous, kind-hearted human being who contributed so much to the community. He certainly earned my gratitude and utmost respect. I wonder if it would be a wonderful tribute to Ken, who is beloved by so many of his colleagues and Sql Server professionals who’ve read his Sql Server books, for PASS to somehow make Ken’s last book available.

Comments

2007年故国行流水帐之八——到南方旅游

哇噻,瞧这流水帐写得,都快2009了,我这还在写2007年的事情。这是该系列的最后一个,关门篇。

由于这次回老家是大令和儿子第一次到中国,我们是从芝加哥飞北京,从上海飞回芝加哥,目的是多去一些地方看看。另外一个想法是在北京上海时,可以带上父母和侄子侄女一起玩儿。这样他们和我老婆孩子也会有更多的时间了解和交流。他们后来没有到北京。这样我们回美国时,就一起到南京、上海、和杭州一游。

我们原来的计划是在枣庄买卧铺到上海,然后以上海为根据地到宁杭等地。但枣庄火车站不大,CRH等快车都不停。如周燕老师在这篇博客里所说,火车提速是好事,但也给一些小站的乘客带来了不便。后来决定二哥开着家里的厢型小客车,他一家四口,加上父母,和我们一家三口,一起开车去上海。

这样一来,就是一个9人的小型旅行团了。人多自然就不好安排,也容易产生矛盾,不好做计划甚至根本就没有计划,再加上对于事情的理解、态度、和期望值不同,所以整体来讲这次旅行不算愉快。不过我希望能汲取经验,以后能有机会还想一起旅游一下。

我们早上出发,下午就到了南京。京福高速公路真不错,以我的观点,它的路况比我常开的美国州际高速I55要好不少。但也听到了收费有点高的抱怨。

下午到南京后,我们先去了雨花台。和在北京一样,我们走马观花,不可能慢一些,细细体味这里的历史和风情。那里的一扇影壁墙上刻着多国语言的《国际歌》歌词。老婆说在瑞典民主选举多次获胜执政的社会民主党,在开会后有时唱这首歌。感觉好像让人吃了一惊:民主、自由、富裕的国家执政党竟唱《国际歌》,信奉社会主义,真是咄咄怪事。

南京大火炉,热啊!
南京雨花台

我在雨花台游乐场被偷拍,不慎走光,浑然不觉:
南京雨花台

由于时间紧,我们在南京只呆了一夜。当晚上开车到玄武湖,经过市中心,感觉南京真得很繁华。这是季氏父子在南京总统府主理政务:
南京总统府

第二天我们驾车去杭州,住在文二路靠近东方威尼斯的一家旅馆。在钱塘观潮后的那个晚上,我带着老婆孩子到西湖新天地的一家餐馆吃饭。老婆点的比萨饼很好吃,比通常在美国吃到的要好很多。Margarita Pizza
Pizza restaurant near 西湖新天地

说起吃喝,个人以为一个有点讽刺意义的事情是:出产于瑞典和中国的可口可乐、雪碧、百事可乐、芬达等软饮料,要比美国国产的好喝很多。我以为有两个原因:一是美国可乐产品的色素放的太多,这个在芬达里最明显;二,也是最主要的原因,是美国国内的糖分是从玉米中提取,所谓high fructose corn syrup和corn syrup。而美国以外的软饮料的糖分大都从蔗糖里来。(芬达好像是美国出口转内销的产品:我是先在中国和瑞典喝到,最近几年才看到芬达在美国慢慢地多了一些。)顺便说一句,在国内看到那么多的国内外软饮料广告(可乐,佳得乐,红牛,王老吉,娃哈哈等),以华美的包装,靓丽的外形,巨大的经费,营造出时尚、前卫、畅爽、酷的感觉来推销这些经过高科技、深加工的狗皮膏药,给受众洗脑,就想到美国的肥胖症问题。这个症状的原因之一不是过多的软饮料吗?其他发展中国家不能从中汲取点教训吗?

也看到了星巴克的咖啡,觉得Latte这个词儿译得好玩儿:拿铁。这种译出来的东西,好像音义兼失,成了四不象。还不如译成拉忒,至少个人以为这样音更贴近原文。欲知我在翻译学上深深的造诣,请参考小文《波耳波经》

杭州真是个好地方。我们这一大家人,除了我以外,是第一次来杭州,很高兴能和他们一起分享这人间天堂的婉约和秀美。走在西湖边上,想起杨万里的诗:

毕竟西湖六月中,
风光不与四时同。
接天莲叶无穷碧,
映日荷花别样红。

小时候背书歌子,觉得挺枯燥的。但现在品味起来,这诗写得多美呀!

西湖
杭州西湖

西湖小桥边的雕塑
杭州西湖

同时觉得杭州的公交专用道做得真不错。听说厦门也新开了BRT公交专用线,不知规划和使用的如何。

杭州后我们去上海。上海的新建筑真不简单。东方明珠的外观我不敢恭维,但像世贸中心等大部分新建筑都特棒!我们到人民广场的城市规划博物馆参观时,看到上海的建筑规划和城市模型,还有那个360度环绕电影以漫画人物三毛的视角来看上海,很震撼,也很为上海自豪!

我们参观完东方明珠后,上海已是华灯初上的夜晚。下来后,我们一家人走在南京路上,看灯火辉煌,人群熙熙攘攘。父母走在前面,特别是母亲,背微驼,拎着包和几个装食品、饮料、和杂物的塑料袋。大令捅了捅我的肩膀,说:“Be a gentleman, help your mom carry the bags.”。

我虽不是两手空空,但当时真得觉得很惭愧。实际上自打上大学后,和父母在一起的时间就很少了,尤其是像我这样远在大洋彼岸的人。每个家庭,无论古今中外,都会有自己的矛盾和挣扎。但如史铁生在《我与地坛》里所说:“我不知道她已经找了多久还要找多久,我不知道为什么我决意不喊她——但这绝不是小时候的捉迷藏,这也许是出于长大了的男孩子的倔强或羞涩?但这倔只留给我痛侮,丝毫也没有骄傲。我真想告诫所有长大了的男孩子,千万不要跟母亲来这套倔强,羞涩就更不必,我已经懂了可我已经来不及了。”信哉,斯言!

今年五月母亲节左右听到了许巍的《礼物》。在YouTube上看《鲁豫有约》访问许巍时,说许巍的一个朋友或熟人在第一次在录音棚里听到这首歌时,感动的留下了眼泪。我听了后,也挺感动的。他写的那种情感,是很美的东西!挺喜欢这陕西哥们儿的歌。

这儿是许巍的《礼物》,献给我亲爱的父母,亲人,和祖国。

Comments (4)

Convert character set to utf8 in MySQL

Say, you had a MySQL database that stores texts in languages such as Chinese, Japanese, Korean, Hebrew, Arabic, and so on, and recently there was a server move or upgrade, and all of sudden text in the above mentioned languages didn’t display properly. What do we do to fix it then?

That was what happened to me recently. My hosting company migrated this site to a new LAMP server, and managed to screw up the character set so my Chinese entries looked like gibberish. I spent some time researching, and was able to convert my WordPress database character set to utf8 successfully. Below is the notes for my own consumption. If it helps you, dear reader, all the better!

Please follow the procedures below step by step in the order presented here. If you need help running sql script using command line tools, I have a little discussion here. I am assuming you have access to command line tools or things like phpMyAdmin. Please remember to replace MyDb with your database name.

1. Switch to information_schema database by:

USE information_schema;

2. Run this statement below. It will generate sql statements to be executed later. Save the result in a sql script file.

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', REPLACE(column_type, 'char', 'binary'), ';') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%char%';

3. Run this statement below. It will generate sql statements to be executed later. Save the result in the same sql script file, below the sql statements generated earlier;

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', REPLACE(column_type, 'text', 'blob'), ';') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%text%';

4. Run this statement below. It will generate sql statements to be executed later. Save the result in the same sql script file, below the sql statements generated earlier;

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%char%';

5. Run this statement below. It will generate sql statements to be executed later. Save the result in the same sql script file, below the sql statements generated earlier;

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%text%';

6. Add the line below to the sql script file;

ALTER DATABASE MyDb CHARACTER SET utf8;

7. Switch to MyDb by:

USE MyDb;

8. Run the script file generated from the above steps, and you should be good.

Enjoy! My procedure above is especially useful for upgrading databases from MySQL 4 and earlier to MySQL 5 and later, as discussed here. It appears that for MySQL 5 and later, it is possible to convert character set of all columns in one command, like:

ALTER TABLE wp_posts CONVERT TO CHARACTER SET utf8;

However, I didn’t test it so I am not sure.

By the way, if you are interested in collation in MySQL, especially for Chinese, I wrote a little note here.

Comments (2)

Use NAnt to replace values in other xml config files

NAnt looks like a nice deployment and configuration tool. You can put important parameters in the build file and/or pass values via command line to overwrite values inside the build file, do file copy, compile code, log progress, and all kinds of stuff. This gives you great flexibility in your build and config process. But what if you need to change a setting in other related xml config file? For instance, sometimes you need to change a virtual folder name, web or database server name, or database user name that are defined in a different file.

In that case, we can use xmlpoke. Here is a sample of it:

Build file sample

<project name="HaidongProject" default="MyTarget">
	<target name="MyTarget">
		<xmlpoke file="App.config"
    		xpath="/configuration/appSettings/add[@key = 'server']/@value"
    		value="NewDatabaseServer" />
    	</target>
</project>

Config file sample

< ?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appsettings>
    <add key="server" value="OldDatabaseServer" />
  </appsettings>
</configuration>

Run

nant.exe -buildfile:build.xml

And it will change the server name in the App.config file.

By the way, today I used Windows findstr, which, like grep, supports regular expressions.

C:\Program Files\NAnt\doc\help>findstr /s /i /m xmlpoke *.*
tasks\index.html
tasks\xmlpoke.html

C:\Program Files\NAnt\doc\help>findstr /s /i /m xmlpeek *.*
fundamentals\listeners.html
tasks\index.html
tasks\xmlpeek.html

Comments