Archive for March, 2008

2007年夏故国行流水帐之五 — 坐火车回家

在北京待了一个星期后,我们坐火车卧铺回家。儿子很兴奋。在他的世界里,睡在卧铺车厢的上层是再酷不过的事了。一夜无话,早晨四点钟左右到站被乘务员叫醒,到家了。

Train from Beijing to Zaozhuang

我对再上一次坐火车回家的体会更深一些。那是2003年,也是从北京坐火车,是我1995年离开我国后第一次回国。这要感谢老婆的大力支持,是她鼓励我早回来的,虽然她看不懂中文。这次是我一个人回来。

应当是2003年8月份吧(更正,2003年9月8日)。从美国飞回北京之后,我从机场坐大巴直赴北京站,很有些归心似箭的感觉。排队买完车票之后,发现如今的有点粉红色的车票和以前的大不相同。我记忆中的火车票是那种硬纸壳票,像烟盒侧面大小,大概和姜育恒所唱的票根类似:“曾经以为我的家,是一张张的票根。撕开后展开旅程,投入另一个陌生”。其实那种硬梆梆的很有质感的火车票,是不太容易被撕开的。

大概是由于临近中秋节的原因,那次火车的乘客不少,我排队时没买到卧铺,是在车上坐了一个小时后补办的。

我先和从江苏徐州的一家人挤坐在一起。因为鲁南和苏北徐州地区的方言一致,我们就用家乡话啦呱。不明白为什么不少人都对我没忘家乡话,不“蛮了嘎叽”表示奇怪。至少对我来说,家乡话是这辈子都忘不了的。

徐州地区的一家人是带女儿到北京看病的,好像小女孩有肺病,在徐州地区没治好。他们一家人的火车晚餐是常见的方便面,三口人用的是宽身低矮的白瓷杯。父母看着,不时催女儿多吃。女儿吃过后父亲把剩下的汤面干掉。当时没看到母亲吃什么。

我告诉他们我有自己的孩子。在我四、五岁的时候,我二哥在村民办小学的办公室烤火时昏迷,脸贴到了火炉盖上。我记得父亲带着二哥走南闯北给他治脸部的烫伤,所以我很理解他们的心情。那位父亲很感动。当我拿到卧铺票时,他起身想送我一程。我谢绝了。和大多数国人一样,我对挤火车还是有一些经验的。

那天晚上在卧铺上我基本上没合眼。我看窗外的庄稼、村庄、小河、公路、小车站、大车站,和半夜里上下车的人们,虽然看不太清楚。一个很强烈的感觉是:火车上的任何人都可能是我的父母或兄弟姐妹,我可能是他们中的任何一员。当时狠不得张开身上的每一个毛孔,吸收进这块生我、养我的土地上的所有气息。

也记起了1990年去厦大读书第一次出远门。大概是四十小时的旅程吧,那时的我充满了新鲜、好奇,和对自由的大学生活的憧憬,当然也有些迷惑和对外人的防备。如果能有时间机器,回到过去观察那时的自我,不是很有趣吗?

刚刚过去的春运还历历在目。每年春运的火车交通都是大新闻,有不少趣闻、轶事和更多的怨声载道。读者可以说我站着说话不腰疼,我觉得一个正面的结果是它能够加强不同地方的人们的了解和沟通,对提高人们的向心力、凝聚力很有帮助。

以下是去年回国时火车上的另外两张照片:

Pabst Blue Ribbon, with lemonade (it looks like water, but we do vaguely remember it had some lemon flavor) next to it.

Train from Beijing to Zaozhuang

This, after all, is the best beer in America in 1893! That’s what was printed on the label, a somewhat dubious claim, according to this site.
Train from Beijing to Zaozhuang

Comments (2)

My best investment so far

I’ve been using Jon’s excellent tools, ViEmu for Visual Studio, Outlook, Word, and Sql Server Management Studio for close to a year now. They make the default editor in those tools behaves like VI, with regular expression support. I bought the Enterprise Pack. Highly, highly recommended. Of course you need to know VI first.

I had some issues with ViEmu for Sql Server Management Studio on Sql Server 2008 CTP 6. So I wrote Jon. He got back to me very quickly. It turned out that the version I installed was an older one. I downloaded the newer one and it worked out of the box.

So I am a VI / Vim aficionado. In fact, I consider learning VI to be the second best investment I ever made so far in my life. The best ever happened about 5 years ago, when I finally taught myself touch typing. (I didn’t see or touch a computer and typewriter until my early twenties.) I tried to teach myself touch typing maybe ten years ago, using a high school typing book I borrowed from the local library here. That didn’t work out very well. Five years ago, I purchased a touch typing software off the web, maybe 25 dollars or so, and finally became a decent touch typist. Now I kick myself for not learning touch typing earlier.

After that, typing is not a hindrance anymore. Using Vim actually makes it enjoyable. It has made a huge difference in my life.

There is so much to learn in Vim. So far these are my most-used commands:

w
b
)
(
G
gg
cw and cNumberw
cfSomeAlphaNumericCharacter
ctSomeAlphaNumericCharacter
dw and dNumberw
C
cG
dd
.
yy
p
P
fSomeAlphaNumericCharacter
;
*
i
I
a
A
o
O
/
%
:g/^$/d to remove blank lines
Macros (q, then a letter, followed by actions that you want repeated later. For example, suppose I have a list of tables that need to be truncated, I will go to the first line, start macro recording, press I truncate table, Esc, j, then q to stop recording. For the rest of the lines, I just do Number@MacroName)

That’s all I can think of now. According to Vim Tips Wiki site, this collection of tips is the best. I found that most of the tips need to be followed consciously for a while before they become part of you. What are yours you can share with me?

Comments

TCP Chimney issue on Windows 2003 Service Pack 2

If you are running Sql Server 2005 on Windows Server 2003 SP2, you may get this message from Sql Server native client connection:

An existing connection was forcibly closed by the remote host

This is most likely caused by a “feature” called TCP Chimney offloading. On the server, run:

Netsh int ip set chimney DISABLED

should fix your problem. It seems Visual Basic 6 (VB6) based applications and Heat Call Logging are particularly susceptible to this problem.

KB article here: http://support.microsoft.com/default.aspx/kb/945977

Comments (2)

Analyzing low performance SQL code

As an independent consultant and trainer , I found myself doing a lot of existing code analysis and enhancement, mostly for stored procedures and ad-hoc SQL statements. I suspect a lot of people do the same thing as well, so I am really interested in learning how you do it. That’s the main purpose of this post. This post is tagged with Oracle, Sql Server, and MySQL, as the principals should be the same for all platforms.

Let me share with you how I do it. Notice that I look at table/column statistics and indexes in almost all the steps below. Therefore I purposely left them out in the discussion.

1. I always talk to the original author or the current owner of the code, asking him/her to walk me through it. I listen mostly, trying to understand why s/he writes code this way. I may ask a few questions, just to help me understand. I almost never do any lecturing at this stage. If the code is a stored procedure, I ask the author or stakeholder to give me sample execution statements, with parameters filled with typical values for production load. Sometime it is not possible to talk to the original author, because s/he may have left the company;

2. After that, I will get the database in question and restore it on my own workstation, with the data as close to production as possible. I will start running the code, and gather IO statistics and execution plan, and save the results for benchmark comparison later on;

3. Here is the raw code analysis stage, where code is put into an editor for analysis. In my case, it is VI or VIM. I find myself using the * command on all variable names and tables (make sure you are doing case insensitive search for Sql Server code), to see where they are used. This is also where I weed out the historical garbage code that is there but never being used anymore, such as unused variables, tables, etc. In the case of Sql Server, I mostly look for cursors, temp tables, and user defined functions;

4. Code simplification stage. I found a lot of bad performing code is unwieldy and unnecessarily complex. At this stage, I look for redundant WHERE clauses, convoluted AND and/or OR operators. This can be a tedious and time-consuming process. At this point, it is important to talk to the author or owner of the code, as a good understanding of data and entity relationship is crucial. Also, since I have gained some understanding from the steps above, it is easier to have an intelligent conversation with the code owner, with fresh perspective and momentum. Usually something good will come out of the conversation, and we will have something concrete to work on next;

5. I will make the changes discussed in step 4, once again, collect statistics and execution plan for comparison;

6. Repeat Step 4 and 5 a few times, when necessary. I usually start talking about better database design, code/design refactoring, set based operations, etc., as hopefully I have some credibility and gained the customer’s trust.

So what do you do to break down complex, low performance SQL code? What methodology, tools, tips, tricks you can share with me?

Comments

Calculating Sql Server logical reads using Perl

One important method for performance tuning is to decrease number of logical reads, by adding, updating, and/or refreshing indexes and statistics. To see if those changes make a difference for you, it is important to look at query statistics.

Here is a sample output after set statistics io on:

Table ‘Table1′. Scan count 0, logical reads 1673, physical reads 4, read-ahead reads 8, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table ‘Table2′. Scan count 0, logical reads 1673, physical reads 10, read-ahead reads 8, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table ‘Table3′. Scan count 1, logical reads 11871, physical reads 1, read-ahead reads 11865, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table ‘#TempTable_____________________________________________________________________________________________________________000000000022′. Scan count 2, logical reads 276, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table ‘Table2′. Scan count 156, logical reads 1723, physical reads 1, read-ahead reads 8, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

I used to tally those numbers by hand. This is error prone, and can be time-consuming, especially if the output is big. So I cooked up a Perl script below for this:

#!/usr/bin/perl
use strict;

my $LogicalReads = 0;

Main: {
my $log = shift or die "***Err: $0 expects a file name.n";
open(LOG, "$log") or die "***Err: couldn't open $log.n";
while () {
findPattern($_);
}
close(LOG);
print “Total logical read is $LogicalReadsn”;
} # Main

#######################
sub findPattern {
my $line = shift or die “***Err: function findPattern() expects a string.n”;
if ($line =~ /logicalsreadss+(d+)/ix) {
$LogicalReads = $LogicalReads + $1;
}
} # findPattern

Thanks Linchi and Tom for ideas!

Comments

Drawing Swedish flag with Logo

After our last experiment, we drew a Swedish flag with Logo. Major code improvement:

1. Introduction of variable for flag size;
2. Using the direction keyword instead of turnright and turnleft to make it less ambiguous to the code reader.

Here is the final product:

Swedish flag

Here is the code for it.

# Begin logo code

reset
x=400
canvassize 1.6*x,x
canvascolor 65,105,225
penwidth 0.2*x
pencolor 255,255,0
go 0,0.5*x
direction 90
forward 1.6*x
go 0.6*x,0
direction 180
forward x

# End logo code

Chinese flag is next. This will be a bit challenging. We will need to figure out how to draw the stars, and tilt them in the right direction. That should also give us ideas on how to draw the US flag.

2 things are of interest:

1. The code above works for KTurtle 0.6 on Ubuntu 7.1 with KDE 3.5.8, but the same code does not work on Sabayon Professional 1.1, with KTurtle 0.6, KDE 3.5.7;
2. After the Swedish flag, I thought drawing other Nordic flags is just a matter of changing colors. Turns out that is not the case, because there are subtle design and dimension differences, especially for Norwegian and Icelandic flags.

Comments

HP dv6253cl laptop fixed

It took me a while to fix the wireless problem on my HP dv6253cl.

As documented here, I tried various ways to make the wireless card work on this laptop, to no avail. I tried 3 different distros: Sabayon 3.4, Ubuntu 7.1, and Kubuntu 7.1, with different Broadcomm driver combinations I found at ndiswrapper’s wiki site. I even downloaded and compiled different ndiswrapper packages.

HP had asked me to send it back for repair in December last year. It sent me a box for packing with a prepaid FedEx label, since the laptop is still under warranty. I held on to it until late February. I got the box back 2 days ago. In all, I was pretty impressed with HP’s speed, considering the horror stories I’ve read on the web.

So I wiped everything out earlier, and installed Sabayon x86-64-3.4 MiniEdition (a flavor of Gentoo distro) on it. I am happy to report that I got wireless working on this Linux machine.

Here are the steps:

1. The first startup after Linux installation, the wireless indicator is blue;

2. I logged in as root and fired up WPA_Supplicant Administration GUI to see if I could scan and detect my wireless network. It didn’t work;

3. lspci command has this to say about the Broadcomm wireless card

03:00.0 Network controller: Broadcom Corporation BCM94311MCG wlan mini-PCI (rev 01)

4. I pretty much followed documentation here. First I removed the existing Broadcom driver (as root). After this command, the indicator turned orange:

# rmmod bcm43xx

5. Google for dv6253cl, follow the HP link to download wireless driver. Run sp36684.exe to expand the installtion file on a Windows machine. By default, the expanded files are in C:\SWSetup\SP36684A

6. Grad bcmwl5.inf and BCMWL564.SYS and transfer them to your Linux. Make sure they are in the same directory. If you have 32-bit Linux, get bcmwl5.sys instead

7. Run the following:

# ndiswrapper -i /path/to/bcmwl5.inf

8. Verify the installation:
localhost haidong # ndiswrapper -l
bcmwl5 : driver installed
device (14E4:4311) present (alternate driver: bcm43xx)

9. Run this and the indicator turned blue:

# modprobe ndiswrapper

10. Set the WEP key:

# iwconfig wlan0 key restricted 123456789A

11. Set the SSID:

# iwconfig wlan0 essid MySsidName

12. Bring up the interface:

# dhclient wlan0

Update: This morning I installed Sabayon Professional 1.1. I got wireless working again, but the strange thing is that the indicator is orange.

The wireless scanning function is absolutely necessary. Anybody out there knows which program works to scan available wireless networks?

Comments

Migrating from one RDBMS to another

Here is some of my thoughts on migrating MySQL to Sql Server. It came out of an email discussion. I’d love to hear your thoughts on migrating to a different database platform, not just MySQL to Sql Server.

I actually thought about writing a white paper or even a course on migrating from MySQL to Sql Server, but never got the time to do it. Sometimes a project doing similar things can serve as a launchpad for this endeavour, but that never came along, at least not yet. I am very interested in database interoperability field though. I’ve done MySQL and Oracle admin in the past and have published some MySQL and Oracle stuff in blogs. I have much better technical skills on Sql Server than any other RDBMS platforms, primarily because I’ve worked on it longer.

Here are some of my thoughts. I think most of it applies equally on migration from Oracle, DB2, Sybase, Postgresql, etc., to Sql Server, or the other way around. It might be slightly easier to migrate from Sybase to Sql Server, considering their common root.

1. It is not easy to migrate existing app, unless the app is a simple one. Even for that, there are enough quirks that can throw people off and cause enough frustration to derail the whole projects. I’ve seen that happening twice, having engaged in moving 2 apps from Sql Server to Oracle;

2. Therefore, the best way to migrating to a new database RDBMS, in my opinion, is to start from a new initiative, probably not big initially. When you start things from a clean slate, you don’t have the historical garbage to worry about. Furthermore, you will give the team enough time to learn the new platform, and prepare the team for future migration, if you choose to do so;

3. Having open-minded team members is crucial to a migration project’s success. Too often people have emotional attachments to the platform they are familiar with, possibly out of job security concerns and lack of general curiosity toward new things.

I generally adopt a platform agnostic attitude, and don’t get religious and too carried away on the platform I work on. Having said that, I think these are points that marketing people can spin for persuasion purposes:

1. MySQL has too many storage platforms: MyISAM, InnoDB, MaxDb, and the newly introduced Maria. This can be viewed as a plus, as it provides choice. The downside of it is that it causes confusion for end users;

2. MySQL’s support for relational model is fairly recent. For example, for a long time, MySQL didn’t support Stored Procedures, Views, Triggers, Foreign Keys, etc. One could argue that MySQL is not mature in this area since it is new for them, but I think it is difficult to find evidence to substantiate that claim. Also, running the risk of offending some people, I think the importance of relational model got overblown a bit;

3. Sql Server offers the CLR integration. This can be a great selling point;

4. Sql Server offers tight integration with Visual Studio, Windows network, and all other things Microsoft. This is a huge advantage.

5. Too many people find *nix environment intimidating. Although MySQL works on Windows, but the perception in the marketplace is MySQL works better on *nix.

As far as migrating MySQL to Sql Server in a hosting web environment, my honest opinion is Sql Server will be fighting an uphill battle, because MySQL excels in this arena, especially for small and medium-sized, or departmental organizations, with the proliferation of such LAMP app like blogs, wikies, discussion boards, etc. I believe Microsoft’s weapon of choice in this arena should be SharePoint. Given Microsoft’s clout, it is certainly a battle worth fighting.

Comments (5)