Chong's profileChong's pagePhotosBlogLists Tools Help

Blog


    June 20

    How international web site identify your location/language?

    As we know many famouse international web site can support multiple languages and multi-marketization, that means people from different countries will see their own language and culture-specific content when they open the web site, eg. we access www.live.com, if we are in Japan, then we can see Japanese on live.com, if we are in US, we can see English on live.com.
    So how international web site identify your location/language? Here are some popluar ways,
    1. Web site can identify your location/language by your Browse's language setting, the default language setting in your Browse will be the same as your mother language, like en-us is for English language in US, zh-cn is for Symplified Chinese language in China.
    2. Web site can identify your location/language by your perference setting in your profile, this is used for many web site that need your registeration.
    3. Web site can identify your location/language by your IP location, each IP is corresponding to a region, and we call the way that can identify the location by IP as IPGEO
    October 10

    Talking about How IE identify the encoding of html document

     

    Quote

    How IE identify the encoding of html document
    I just summary the priority of IE identify the encoding of html document.
    Note: bulleting number represents the priority by which IE identify the encoding.
    1. IE use the character set by the server code, like the following asp.net code snap:   Response.Charset = "x-IA5-German"; 
    2. IE use the characeter set in META element in the document like:<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=gb2312"> if this element sepcificed, use this element and neglect the IE en-coding set by the user.
    3. If 1 and 2 are not set, IE use the default lanuage setting, frequently it is UTF-8
    4. After user open the page, user can change the encoding of IE, this time IE will use the user's encoding setting.
    I use user scenario to explain:
    User open IE and request one web site like www.microsoft.com, the encoding setting of IE is Simplified Chinese, auto-select is unchecked.
    When the page is loaded to the user IE, IE will first detect the server code and then the META element and finally the default UTF-8. So even if user's setting is simplified Chinese, the encoding of IE will become UTF-8 after the web site is fully loaded.
    May 29

    Expericence share: Process of Localization

    Expericence share: Process of Localization:
    Prerequise:
      1. Should consider localization and globalization in software design phase. Localization and globalization belong to one part of software arithecture design.
        1.1 Design of Aritecture level for ASP.NET(basic)
           Explanation of culture concpetion:
            There are three types of clutures in ASP.NET world,  ASP.NET will 递归查找 the three culture according their priorities.
            1. Netural culture: The default culture, ASP.NET use en-us as netural culture. This culutre's priority is the lowest, that measn ASP.NET will use this culture when there is no culture specified by the user.
            2. Culture: the culture only with language information but no market specified, eg. en the English language, zh chinese. This culture's priority is the second one.
            3. Specific culture: the culture with both language information and maket specificed, eg. en-us English and US market, zh-cn, Simplified Chinese and China mainland market, zh-tw Tranditioinal Chinese and Tai Wan market. This culture's priority is the first one. That means ASP.NET will pick up this cluture as long as it exits.
         1.2 Culture structure
           1.2.1 We create one folder for each culture and use the culture name as the folder name, eg. we create folder zh-cn for zh-cn culture
           1.2.2 We put resource to the culture folder, eg. we put Chinese resource to zh-cn folder.
         1.3 The way to implement the identification of language&market.
           1.3.1 There are two way we can use to identify the language&market. That means we can design the program to identify the user language&market.
              a) User define a langauge&market information and send this information to web server. We can use query string to implement this, like user can input http://abc.com.cn/?mkt=zh-cn, this can tell the server side application the user want to visit a Simplified Chinese China site.
              b) The program read the language setting in IE or firefox. This setting will be sent to web server in http header.
    In coding phase, obviously a) option's priority is higher than b) option, b) option can be a default option.
    Finally, ASP.NET can read these culture according to the user language&market information and locate the culture atuomatically.
    What we shoud do is to
      1. Make the culture folder structure and resources ready
      2. Get the user language&makret information from http header or query string
      3. Assign this language&makret information to ASP.NET
     
      2. All development and feature testing is complete.
    Steps:
    1. Make a sudo build
    What is sudo build?
     Sudo build is the build whose resoures eg. Text, image, are embedded with wild characters, eg. we embed %$# to each side of text 'Hello'.
     We use this way to identify whether the resource has the ability of localization.
    How to make a sudo build?
    Using a localization tool is an efficient way.
    The tool can,
      1. Read resource file, eg. it read each resource in resource file, in ASP.NET the resource is a xml file. We can make each web page corresponding to one culture specific resource file or all web pages corresponding to one resource file. The suggested way is that we make all resources in one resource file for per language-market.
      2. Insert wild characters into each resouse like %$#Hello%$#
      3. Save the resource file to a specific folder like sudo-res.
    2. Do localability testing and internationalization testing
       2.1 Case preparation
       2.2 Run test cases
       2.3 Bug fixing
       2.4 Bug verification
    3. Do localization testing for each specific market.
    March 06

    JAVA平台(之所以这么称呼是因为我个人觉得JAVA作为一个兼容多个操作系统的环境是一个虚拟的可以HOST企业级应用程序的平台)和WINDOWS平台的比较

    接触JAVA平台两月有余, 虽然还是菜鸟级别但对这套平台已经略有感触, JAVA平台(之所以这么称呼是因为我个人觉得JAVA作为一个兼容多个操作系统的环境是一个虚拟的可以HOST企业级应用程序的平台)和WINDOWS平台的比较如下:
    迈克索肤特:视窗集成组件. 啊比木/比一诶:组件集成微波丝非/微波罗捷克
     
    还处在菜鸟级别,希望将来能越写越多
    February 06

    Expericence about agile software development methodology(To be cont)

    虽然只有一年的敏捷软件开发经验, 但我已经是感受颇多, 现把自己的感受写下来,一是让自己回味, 二是给同行分享.
    According my understanding,
    Compared to the tranditional software development method, agile software development has the following merits:
    1. Meet the market requirements on demand(welcome the code changes even in the later of the development).
    2. Make software development process more efficiently compared to the tranditional software development like waterfall
     
    What is the main elements in agile?
    1. Short release circle.
      How?
       Each release only last one month.
     Why?
       Meet the market requirements on demand.
    Disadvantage.
       May cause poor quality product shipped.
       May cause testers busied with re-writing test plan&cases, re-coding automation test program, re-run test cases.
    2. Daily build, deployment and automation test.
      How?
       Do build, deployment and automation test on daily basis.
       It is perfect that we can make build, deploy and auto-test fully automated and run stably on daily basis.
     Why?
       I think this is key point for the success of agile. Because daily build, deployment and auto test can make sure the quality of the product on daily basis, and thus make sure the product shipped with high quality.
    Challenges.
      Build need to be scheduled to run.
      Automation test code's maintance is high cost, normally need at lease one person fully time work on it. So  a good auto-test architecture can reduce the cost of code maintance.
      Deployment is better to be automated to enhance the efficiency, but this will introduce the cost of  auto-deloyment scripts maintance.
      How to make auto-deployment scripts run with less configuration and without errors is a challenge.
     
    3. Test driven.
        How?
     Utilize unin-testing to find most of the critical issues in the code. In .net, there are NUNIT. In java, ther is JUNIT.
        Why?
        Because each sprint only last one to two month, so we need use unit test case to cover most of the execution path in the code, and thus we can find and resolve all critical issues beofre the code is shipped to testing by testers.
        Disadvantage.
        Developer needs to write much more unit test cases, and this will make dev upset.
    4. Scrum meeting.
       How?
        Dev, PM and tester have a daily short meeting together to talk about the progress of the project and sync each member's work status.
      Why?
       A daily short meeting( less than 30 minutes) can make every one sync on the same page timely, make PM and leads know the progress of the project clearly and also know the latest issues in the project.
    5. Postmotom
      Postmotom is a meeting to let all team's member to summary what need to be improved after one sprint.
      Knowing what is the blocking issue during last sprint, thus we can change improve ourselives continuously.
      Why it should be daily basis?
       I think this is because agile software development uses day as the atom work unit. That means we define the schedule on daily basis not weekly or monthly.  So it is much make sense that team meeting's cycle is consistent with the atom work unit.
    Thus daily meeting makes PM and Leads know clearly each work item's progress.
     
    6. The track of each work item in a sprint.
        How?
         Use style sheet excel to track each work item in one sprint, each column in style sheet is like the following format,
         Work item name,  priority, owner, workload, day1, day2, ... , day30
         
         Description of each column.
    •     Work item name: short description of the work item. The work item need to be splited if it takes long time(normal more than 1 week's workload).
    •     Priority: the priority of this work item, 0,1,2,3. 0 means the first priority and need to be done as the first priority.

              0,1,2 priority's work item need to be done in this sprint, 3 and 4 priority's work item is best to be done.

    •      Owner: the owner of this work item. Can be more than 1 owner.
    •      Workload: the assumed total hours of this work item. Normally the workload is 4 hours one day. So for example, if one work item need 3 days, the workload will be 12
         Day1, day2, ... : the workload burn down of each day. For example, A workitem need 12 hours to be done. If day 1 = 8, that means the left hours is 8. So ideally the day 3 should be 0 which means the work item is done on third day.
     
      7. Workload burn down chart.
      8. Need a planning phase at the beginning of each sprint.
         What to do?
    •         List all the work items.
    •         Prioritize all the work items.
    •         Define the work items which should be delivered in the upcoming sprint.
    •         Spilt each work item to make sure each sub-work item's workload do not more than 20 hours.

      9. The atom work unit is on daily basis.

              How?

    •              4 hour represent one day's workload.
    •             Why we just use 4 hours to represents one day's is because that we should exclude the time of meeting, communication and training.
    •              4 hour is the workload of work item and not inlcude the meeting, communcition or training time.

     10. The ownership of a work item

          Unlike the waterfall method, in agile method the ownership of each work item is dev and tester.

          That means in the end of one sprint, the work item is shipped by dev and tester, so dev and tester should make sure the quality of the shipped work item.

         

     

       

    Some expericence about Test process

    Here I give some my expericence about the test process to share.
    During the development phase, not code complete
    1. Test cases preparation and review
    2. Functional test
    After code complete
    3. Regression test begin and at the same time locability test begin
    Resource hand off after localibility test completes
    4. Localization test begin and Regression test continues...
    Resource hand back after localization test completes, regression test continues...
    Regression test  finishes
    5. Bug bash begin
    Bug bash completes.
     
    In the whole process, I think the test leads need to monitor the whole process(including each member's work progress, how many bugs are found and what is the priority of them), do correct decision when the information is not enough, adjust the test schedule agilely, give daily bug&test report, sync information with dev and pm on time and act as a techical consultant to help team member finish their work item more efficiently.
    October 30

    How IE identify the encoding of html document

    I just summary the priority of IE identify the encoding of html document.
    Note: bulleting number represents the priority by which IE identify the encoding.
    1. IE use the character set by the server code, like the following asp.net code snap:   Response.Charset = "x-IA5-German"; 
    2. IE use the characeter set in META element in the document like:<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=gb2312"> if this element sepcificed, use this element and neglect the IE en-coding set by the user.
    3. If 1 and 2 are not set, IE use the default lanuage setting, frequently it is UTF-8
    4. After user open the page, user can change the encoding of IE, this time IE will use the user's encoding setting.
    I use user scenario to explain:
    User open IE and request one web site like www.microsoft.com, the encoding setting of IE is Simplified Chinese, auto-select is unchecked.
    When the page is loaded to the user IE, IE will first detect the server code and then the META element and finally the default UTF-8. So even if user's setting is simplified Chinese, the encoding of IE will become UTF-8 after the web site is fully loaded.
    October 23

    the different creditial used during different stage of http request

    We often encounter such error message 'Cannot connect to the server', 'ASP.NET do not have the access right to specific resource'.
    We are also always confused with these login failure issue when we do development and deployment. Why cannot I login? How does that happen?
     
    Here I will explain the procedure from one request to IIS to ASP.NET.
    1. Client send one HTTP request to destination IIS server.
        We call this anonymous visit, in fact IIS server recoginze this as a anonymous.
        What does IIS server do?
        IIS have three types of authenication.
          a. Anonymous visit
          b. Basic authenication, the username and password are sent with plain text(Not recommended, always not use).
          c. Integrated windows authenication(Recommend to use in enterprise LAN)
        Here we neglect b and c, we just talk about anonymous visit.
        From the word meaning, anonymous visit do not need user name and password. Yes, but it is only for client IE.
        For IIS, it will use user name and passoword. Where is the user name and password? You can find the user name and password below anonymous section in directory security of IIS management console.
       This means IIS use this user name and password as anonymous user to visit resources. We can also change this user to the one with restricted right like a domain user.
    2. After IIS receive the request, it will pass the request to ISAPI which will identify the request type by the file's extension like aspx, htm and send the request to sepcific process to handle.
       Example, we requst http://aaa/abc.aspx, the ISAPI will think this as asp.net request according to its extension aspx and switch the request to asp.net work process. Note: we can customize our extension in IIS and make specific work process to handle our customized file. Eg. we can specific foo extension and order asp.net work process to handle all the request with foo extension.
    3. ASP.NET work process will process the request.
    In windows 2000 and windows XP with .net 1.1, the work process is aspnet_wp.exe.
    In wodnows 203 with .net 2.0, the work process is W3WP.exe
    What account asp.net will use to process request and visit the application resource?
    In windows 2000 and windows XP, the account is ASPNET
    In windows 2003, the account is Network Service
    We can also order asp.net to use one customized account( frequent domain account) to process request and visit resource.
    This is called imperated, we can define this in web.config and ASP.NET also provides tool to encrypt the account and its password.
     
    September 23

    Expericence of testing&test cases(Updating continuously)

     
    Here are some my expericence and pointview about testing and test cases.
    Hope it can do some help for the readers and also welcome feedbacks :-)
     
    On the whole Software testing can be divided into the following sections,
    1. From function pointview --- based on specification
          Function test cases need to verify:
    •            Function works with normal scenario, eg. expected output with good input data.
                             Need to not only verify the result you see but the inside data as well.
                                eg. If you have verified the date time is rended correctly on the web page, you need also to verify the date's stored format in database, is it a UTC formatted? Maybe you can find a design bug after you drill down.
    •            Function works with abnormal scenario,
                          a. Boundary data input( boundary data can also be legal data, eg. all search results are stored with 100 pages, so the boundary page number is 1 and 100)
                          b. Inlegal data input( the inlegal data is referred to the specification)
                          c. Empty value
                          d. Maximun acceptable input
     
    2. From UX pointview
             Put yourself in a user position, ask the following question:
    •            Is the function easy for use?
    •            Is the error message friendly engouh and easy to be understood?
    •            For the error message, I have the following points;
                   a. For those input by mistake, the error message should serve as notification role so that user can correct the mistake after he read it. So if the error message is unclear, we may file a bug for this.
                   b. For those input by hostility, the error message can be unclear so that the bad guy cannot get any userful information from error message.
     
    3. From security pointview
    In design stage:
    •     Need detailed review of dev spec to figure out the secuirty issue duing the design stage.
    •     Need DTD diagram to analysis the secruity issue according to the scenario.
    •     Need to think about the security issue not only on the software architecture pointview but the phyiscal architecture pointview.
    •     Need to use both client and server side validation for user input.

    In testing stage( web seurity )

    Each can-input area needs to have security test cases.

    Each url which has query needs to have security test cases.

       a. XSS test cases.

       b. SQL injection test cases.

              Does each transact-SQL use paramaters as input?

              Does each transact-SQL which make up of string like @sql = 'select * from '+ str1 + 'where a='+str2 use EXECSQL() to execute?

       c. Dirty data test cases. To see whether dirty data can be inserted into database.

       d. DOS test cases. If user can generate a great deal of exception, DOS may occur.

       e. Exception message expsure test cases. To test whether exception message will be exposed to user.

     Secuirty bug has its priority according to its impact on the application,

    •            Lightweight XSS bugs: those bugs which only affect the attackers. Eg. XSS bug breaks the page style but only attacker can view these broken pages.
    •            Heavyweight XSS bugs: those bugs which can affect all users or can steal user's privacy. Eg.  XSS bug breaks the page style and all user can view these broken pages. XSS bug can steal user cookie.
    •           Lightweight DOS bugs: those bugs which can casue deny of service but DOS will disappear after the stop of attack.
    •           Heavyweight DOS bugs: those bugs which can casue deny of serivce and DOS will still exist after the stop of attack.
    •           All SQL injection bugs are heavyweight.
    4. From performance piontview
        In design stage:
    •         Need a test plan which is based on the performance goal of the application

                           For web application, we need to test the perf :

                                a. on the web level

                                b. on the database level

        In test stage:
    •         Test cases can be those scenarios which need high performance and may exist performance issues.
    •         Need a PPE environment to do perf testing. Testing on single-box or dev machine cannot reflect the real performace of the application.
    •         Analysis of the test results are more important than the generation of test results.
    5. From relibility pointview --- stress test
    •     Test cases are based on specific scenarios, only those scenarios which can cause heavy load of application( eg. tons of e-mail sent, mostly viewed pages) need to be tested.
    •     Each scenario in test case need to be tested on different benchmark.
    •     Analysis of the test results are more important than the generation of test results.
    •     Need a PPE environment to do testing. Testing on single-box or dev machine cannot reflect the real stress-handling ability of the application.
    6. From localibility pointview --- for internatianal software
    7. From localization pointview --- for international software
    8. From assessibility pointview
     
    One test case is make up of:
    1. Setup, this describes the preparation before the test case can be executed.
    2. Steps, this describes the steps we can follow to make the test cases executed.
    3. Expected result, this describes the expected result of test case. The expected result can be from spec.
     
    What bugs or risk can be found by testers in design stage?
    •     Design bugs found
    •     Spec issues found
    •     Security bugs found
    •     Physical architecture issues found
    June 19

    By-talk DNS Parse Method, Http header and Cookie

    DNS, Http header and Cookie have much relation.
    Here I just give my expericence, all expericence are from self-sudy and pracitce.
    DNS its full name is domain name domain name system. It is easy for you to type a name instead of IP to visit a web site. So DNS is very important in internet world.
    First let's talk about the parse way of DNS.
    The parse way is from the dns string's end to the head, for example: if we request www.msn.com, DNS server will do the following parse process(we suppose there is no cache on client computer and any dns server). So it means www.msn.com and www.msn.com.cn are in totally two different domains. The former is located in 'com' domain, and the latter is located in 'cn' domain.
    1. Go to the dns of root domain '.' DNS server ( So it means the root is '.' in the whole dns tree, you can try http://spaces.msn.com.)
    2. '.' DNS server find there is a 'com' node( In fact, there are such domains as 'org','cn','net' and so on below the '.' root node.)
    3. And then 'com' dns server will find there is a node 'msn.com' in its dns list. In fact there is a 'NS' record( name server record) in the dns list, like msn.com ns 220.125.33.90, then the dns request will be passed to dns server(msn.com 220.125.33.90)
    4. Finally the dns server msn.com will find a node 'www.msn.com' in its dns list, it is a 'A' record(alias record) like www.msn.com A 220.125.33.91, thus the DNS help our client computer to find the web server successfully. And then the IP of www.msn.com will return to our client computer(note: the return order will from the last dns server to the first dns server we request and then to our client computer). So router will occur and our client will reach the www.msn.com truly.
    In fact the parse like above statement will not always occur, because there will be cache mechanism in our windows system. If we visit one web site before, the site's dns name and IP pair( www.msn.com A 220.125.33.91 ) will be cached in client computer, if you want to clear this cache you can use the command line 'ipconfig/dnsflush' to flush the dns cache. There is another location in client's computer to store the dns name and IP pair, which is located at C:\WINDOWS\system32\driver\hosts. If you open the file you can see the dns name and IP pair. So if the DNS server in your LAN cannot resolve one dns, especially in testing environment, we can add one entry like( www.mysite.com 10.10.11.10) in hosts file instead the DNS server resolution and thus we can visit our test site http://www.mysite.com . In DNS server there will still cache the DNS, for example, there is a DNS server which ever resolve the site www.msn.com, the DNS server will cache the site's name and IP in the cache. When we visit the site next time, the DNS server will return the IP of the site directly.
    So the true parse process will be:
    We request a site like www.msn.com
    1. If client computer cache the dns and IP, return site's IP go to end, else go to 2
    2. If the hosts file store the dns and IP, return site's IP go to end, else go to 3
    3. If the first DNS server can work, Send the dns parse request to the first DNS server in client computer else send the request to the second DNS server. 
    4. If the DNS server caches the dns and IP, return site's IP go to end, else go to 5.
    5. DNS server will send the request to its parent DNS server and its parent DNS server will send to its parent DNS server. This process is iterative untile site www.msn.com is resolved.
    6. Return the IP of www.msn.com from the last requested DNS server to the first requested DNS server
    7.The first requested DNS server return the IP to client computer
    8. End
     
    In IIS we can set one site has several URL by setting the site's http header( note: there are other several ways to make one site with several url), IIS server will distinguish the site's url according the different http headers we request.
     
    Cookie is the information which store on client computer and will be packaged into the http request when we request one specific url. There are following points we need pay attention to:
    1. By default all the cookies related one site will be sent to the web server after we request the site, for example, if we request www.msn.com, all the cookies will be sent to web server of www.msn.com . If there are two cookies one is related to www.msn.com and the other is related to www.msn.com, when we request either of the two site, there will the related cookie sent to the web server. So it means cookie is bound to the DNS( the site's name)
    2. We can set the path of cookies. For example if there is a path /ext in one site in IIS we can set cookie's path to./ext, so only we request www.mysite.com/ext, the cookie will be sent to the web server. But this way has a vital disadvantage, that is the path is case-sensitive, so if we request www.mysite.com/EXT, the cookie will not be sent to the web server. So it is not recommanded to use this way to host multiple same name cookies in one site which hosts multiple markets, like http://www.mysite.com/chn,http://www.mysite.com/us
    3. The recommanded way is as following: Use different site name for example: us.mysite.com, chn.mysite.com, so there is a cookie named 'mycookie' in our application and we need use this cookie to get user's hobby(user may set different hobbies on different site(us.mysite.com or chn.mysite.com). After we request us.mysite.com or chn.mysite.com, we can get the different hobby from the same cookie name 'mycookie'. We can also host both of the sites on one IIS using different http header, and thus there will one web application on one IIS and host two different versions(US market and China market) of the site. 
    4. Maybe you will ask now that cookie is bound to DNS, how do passport's cookies work? Because multiple sites in different domain(spaces.msn.com and www.msn.com.cn), but both of two sites are using passport service. If we signed in www.msn.com.cn before, we can sign in spaces.msn.com without authication. So what happens? Can the auth cookies in www.msn.com.cn be transferred to spaces.msn.com? Actually there are no auth cookies stored in eith www.msn.com.cn or space.msn.com, all the auth cookies are stored in passport.com domain. After we click passport sign in button, the request will be redirected to login.passport.com. After we sign in successfully, the auth cookie will be stored under login.passport.com, and then if we request another passport service site, the cookies will be sent to login.passpot.com to authicate, if the authication passes, login.passport.com will redirect us to the site we want to visited. So it give us a wrong sense, it appears the cookies can be sent between two different sites. 
    April 02

    如何在IIS中设置SSL的HTTP HEADER

    IIS 中的只能通过UI来设置HTTP的HTTP HEADER,但无法设置SSL的HTTP HEADER.要解决这个问题,首先必须具备以下条件:
    1. OS为WINDOWS 2003 SERVER的BOX必须安装SP1
    设置SSL HTTP HEADER的方法:
    1.Open command-line console
    2.go to directory 'C:windows'
    3. Type cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set /w3svc/<site identifier>/SecureBindings ":443:<host header>"
     
    eg. cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set /w3svc/123456789/SecureBindings ":443:shopping.live.com"
     
    Note: <site identifier> is the identify of the web site, each site will have one unique identify and IIS will use site identifier to distinguish sites.
    March 25

    WNLB KNOWLEDGE FROM MSDN

    #How to config NLB to handle http
    To load balance traffic to Web servers, perform the following steps:
    1. Web servers typically listen on port 80. Configure one port rule on all cluster hosts for a port range of 80 to 80 and select Both for protocols (TCP and UDP).
    Click to select Multiple Hosts. Affinity should be set to None, unless the Web server maintains client state in its memory, in which case Affinity must be set to either Single or Class C.
    If you want to load balance HTTP over SSL (encrypted Web traffic), you need to configure a rule for this type of traffic; usually, the port is 443. Us the following steps:
    1. Set up a rule for a port range of 443 to 443, both TCP and UDP.

    Set Affinity to either Single or Class C to ensure that the client connections are always handled by the server that has the SSL session established.

     

    #Key words for NLB

    1. Multiple Hosts Filtering Mode

    2.affinity:

    •  None, WLBS load balances all network requests across the cluster without respect to their source to maximize the scaled performance achieved by load balancing.
    • Single affinity to direct all client requests from the same IP address to the same cluster host. 
    •  Class C affinity to direct all client requests from the same class C address range to the same cluster host. Increased affinity enhances the cluster's ability to support client sessions, although it may somewhat reduce scaled performance.

    3.Single Host Filtering Mode:
    This mode directs all of the port's network traffic to the host with the highest handling priority. In this mode, you specify a set of priorities for handling the port's network traffic in the event of a host failure. This lets you assign the network traffic for different host ports to different hosts in the cluster and customize the failover policy for each port.

        4.Disabled Filtering Mode:
    This mode blocks all traffic to a port to provide a firewall against unwanted network access to your cluster.

       5.启用“网络负载平衡管理器”日志。您可以配置“网络负载平衡”管理器以记录每个“网络负载平衡管理器”事件。使用“网络负载平衡管理器”时,这种日志对于问题或错误的故障诊断十分有用。通过单击“网络负载平衡管理器选项”菜单中的“日志设置”,可以启用“网络负载平衡管理器”日志功能。选中“启用”日志框,并指定日志文件的名称和位置。

      6.所有群集主机上的群集参数和端口规则设置均完全相同。

      7.对负载平衡应用程序使用的所有端口设置端口规则。例如,FTP 使用端口 20、端口 21 及端口 102465535。

      8.每设置一个端口规则后,单击“添加”。否则端口规则将不会出现在规则列表中,且规则也不会起作用。

      9.除虚拟专用网络 (VPN) 外,在“网络负载平衡属性”对话框和“Internet 协议 (TCP/IP) 属性”对话框中进行设置时必须输入专用 IP 地址和群集 IP 地址。确保这两处的地址相同。

     10.确保专用 IP 地址始终列在“Internet 协议 (TCP/IP) 属性”对话框中的第一位,即在群集 IP 地址之前。这可以确保将来自主机的连接响应返回给相同的主机。

    11.确保群集中所有主机均属于同一子网,且群集客户端能够访问此子网。

    12.Windows Server 2003

    如果不同子网上有两个 NIC,您可以选择在任一个 NIC 上绑定 NLV,也可以同时在两个 NIC 上绑定 NLB,无论哪种情况,都不会出现问题。所有通信均通过正确的 NIC (子网) 发送

    如果同一子网上有两个 NIC,通信会被发送到相应的 NIC

    13.both the dedicated IP address and the cluster IP address must be entered during setup in the Network Load Balancing Properties dialog box and also in the Internet Protocol (TCP/IP) Properties dialog box

    14.Use only the TCP/IP network protocol on the cluster adapter.

    15.Ensure that all hosts in a cluster belong to the same subnet and that the cluster's clients are able to access this subnet.

      16.

    When attempting to use Network Load Balancing Manager to connect to a host in your cluster, you receive the error "Host unreachable".

    Cause:  Internet control message protocol (ICMP) to the host is either blocked by a router or firewall, or disabled on the host's network adapter.

    Solution:  Enable ICMP on the host's network adapter or allow ICMP traffic through the firewall or router. Be aware that this may expose your system to additional security risk. You can also use Network Load Balancing Manager's /noping option. For more information, see Nlbmgr .

    17.

    Network Load Balancing is not load balancing applications, and the default host handles all network traffic.

    Possible Cause

    A port rule is missing. By default, Network Load Balancing directs all incoming network traffic not governed by port rules to the default host. This ensures that the application you do not want load balanced behaves properly.

    Solution

    To load balance an application across the cluster, create a port rule on every cluster host for the TCP/IP port(s) serviced by the application.

    How to config port rule? see msdn article: http://technet2.microsoft.com/WindowsServer/en/Library/d88996fd-d2cf-4ace-a056-d89f76c863331033.mspx 


     Reference MSDN article:

    Best practice of NLB: http://technet2.microsoft.com/WindowsServer/en/Library/357a83b5-670d-4eef-96c4-0d69519e5cbc1033.mspx

    NLB troubleshooting:

    http://technet2.microsoft.com/WindowsServer/en/Library/8d2e0b4f-4dfd-4893-9505-124ddf4fc2f01033.mspx

    NLB parameters: http://technet2.microsoft.com/WindowsServer/en/Library/57c24429-0268-4ed8-afdf-fd4b0b6539b71033.mspx

    http://support.microsoft.com/kb/198496/EN-US/ 

    http://www.microsoft.com/china/technet/prodtechnol/windowsserver2003/technologies/clustering/nlbbp.mspx 

     

    December 26

    Summary for Replication

    http://msdn2.microsoft.com/en-us/library/ms151247.aspx

    Understanding components and their roles in replication:

    Publisher

    The Publisher is a database instance that makes data available to other locations through replication. The Publisher can have one or more publications, each defining a logically related set of objects and data to replicate.

    Distributor

    The Distributor is a database instance that acts as a store for replication specific data associated with one or more Publishers. Each Publisher is associated with a single database (known as a distribution database) at the Distributor. The distribution database stores replication status data, metadata about the publication, and, in some cases, acts as a queue for data moving from the Publisher to the Subscribers. In many cases, a single database server instance acts as both the Publisher and the Distributor. This is known as a local Distributor. When the Publisher and the Distributor are configured on separate database server instances, the Distributor is known as a remote Distributor.

    Subscribers

    A Subscriber is a database instance that receives replicated data. A Subscriber can receive data from multiple Publishers and publications. Depending on the type of replication chosen, the Subscriber can also pass data changes back to the Publisher or republish the data to other Subscribers.

    Article

    An article identifies a database object that is included in a publication. A publication can contain different types of articles, including tables, views, stored procedures, and other objects. When tables are published as articles, filters can be used to restrict the columns and rows of the data sent to Subscribers.

    Publication

    A publication is a collection of one or more articles from one database. The grouping of multiple articles into a publication makes it easier to specify a logically related set of database objects and data that are replicated as a unit.

    Subscription

    A subscription is a request for a copy of a publication to be delivered to a Subscriber. The subscription defines what publication will be received, where, and when. There are two types of subscriptions: push and pull. For more information about push and pull subscriptions

    Subscription Characteristics Use When

    Push Subscription

    With a push subscription, the Publisher propagates changes to a Subscriber without a request from the Subscriber. Changes can be pushed to Subscribers on demand, continuously, or on a scheduled basis. The Distribution Agent or Merge Agent runs at the Distributor.

    • Data will typically be synchronized continuously or on a frequently recurring schedule.
    • Publications require near real-time movement of data.
    • The higher processor overhead at the Distributor does not affect performance.
    • Most often used with snapshot and transactional replication.

    Pull Subscription

    With a pull subscription, the Subscriber requests changes made at the Publisher. Pull subscriptions allow the user at the Subscriber to determine when the data changes are synchronized. The Distribution Agent or the Merge Agent runs at the Subscriber.

    • Data will typically be synchronized on demand or on a schedule rather than continuously.
    • The publication has a large number of Subscribers, and/or it would be too resource-intensive to run all the agents at the Distributor.
    • Subscribers are autonomous, disconnected, and/or mobile. Subscribers will determine when they will connect and synchronize changes.
    • Most often used with merge replication.

     

    Transactional Replication Overview  

    Transactional replication typically starts with a snapshot of the publication database objects and data. As soon as the initial snapshot is taken, subsequent data changes and schema modifications made at the Publisher are usually delivered to the Subscriber as they occur (in near real time). The data changes are applied to the Subscriber in the same order and within the same transaction boundaries as they occurred at the Publisher; therefore, within a publication, transactional consistency is guaranteed.

    Transactional replication is typically used in server-to-server environments and is appropriate in each of the following cases:

    • You want incremental changes to be propagated to Subscribers as they occur.
    • The application requires low latency between the time changes are made at the Publisher and the changes arrive at the Subscriber.
    • The application requires access to intermediate data states. For example, if a row changes five times, transactional replication allows an application to respond to each change (such as firing a trigger), not simply the net data change to the row.
    • The Publisher has a very high volume of insert, update, and delete activity.
    • The Publisher or Subscriber is a non-SQL Server database, such as Oracle.

    By default, Subscribers to transactional publications should be treated as read-only, because changes are not propagated back to the Publisher. However, transactional replication does offer options that allow updates at the Subscriber.

     

    Merge Replication Overview  

    Merge replication, like transactional replication, typically starts with a snapshot of the publication database objects and data. Subsequent data changes and schema modifications made at the Publisher and Subscribers are tracked with triggers. The Subscriber synchronizes with the Publisher when connected to the network and exchanges all rows that have changed between the Publisher and Subscriber since the last time synchronization occurred.

    Merge replication is typically used in server-to-client environments. Merge replication is appropriate in any of the following situations:

    • Multiple Subscribers might update the same data at various times and propagate those changes to the Publisher and to other Subscribers.
    • Subscribers need to receive data, make changes offline, and later synchronize changes with the Publisher and other Subscribers.
    • Each Subscriber requires a different partition of data.
    • Conflicts might occur and, when they do, you need the ability to detect and resolve them.
    • The application requires net data change rather than access to intermediate data states. For example, if a row changes five times at a Subscriber before it synchronizes with a Publisher, the row will change only once at the Publisher to reflect the net data change (that is, the fifth value).

    Merge replication allows various sites to work autonomously and later merge updates into a single, uniform result. Because updates are made at more than one node, the same data may have been updated by the Publisher and by more than one Subscriber. Therefore, conflicts can occur when updates are merged and merge replication provides a number of ways to handle conflicts.

     

     

    Snapshot Replication Overview  

    Snapshot replication distributes data exactly as it appears at a specific moment in time and does not monitor for updates to the data. When synchronization occurs, the entire snapshot is generated and sent to Subscribers.

    Note:
    Snapshot replication can be used by itself, but the snapshot process (which creates a copy of all of the objects and data specified by a publication) is also commonly used to provide the initial set of data and database objects for transactional and merge publications. For more information, see Initializing a Subscription with a Snapshot.

    Using snapshot replication by itself is most appropriate when one or more of the following is true:

    • Data changes infrequently.
    • It is acceptable to have copies of data that are out of date with respect to the Publisher for a period of time.
    • Replicating small volumes of data.
    • A large volume of changes occurs over a short period of time.

    Snapshot replication is most appropriate when data changes are substantial but infrequent. For example, if a sales organization maintains a product price list and the prices are all updated at the same time once or twice each year, replicating the entire snapshot of data after it has changed is recommended. Given certain types of data, more frequent snapshots may also be appropriate. For example, if a relatively small table is updated at the Publisher during the day, but some latency is acceptable, changes can be delivered nightly as a snapshot.

    Snapshot replication has a lower continuous overhead on the Publisher than transactional replication, because incremental changes are not tracked. However, if the dataset set being replicated is very large, it will require substantial resources to generate and apply the snapshot. Consider the size of the entire data set and the frequency of changes to the data when evaluating whether to utilize snapshot replication.

     

     

    November 22

    Expericence of code coverage in VS2005 TEAM SUITE

    Code coverage analysis is a structural testing technique (AKA glass box testing and white box testing). Structural testing compares test program behavior against the apparent intention of the source code. This contrasts with functional testing (AKA black-box testing), which compares test program behavior against a requirements specification. Structural testing examines how the program works, taking into account possible pitfalls in the structure and logic. Functional testing examines what the program accomplishes, without regard to how it works internally.
     
    The code coverage in VS2005 TEAM SUITE is powerful! It supplies the  enough detail information about the code coverage iduring an application running.
    Some usage expericence is following:
    1.The test project should be inlcuded in the web application project if you want to test the web code coverage. Because in asp.net 2.0 dll in web application will be generated during the web application running and be cached in the memory, so there is no dll to be instrumented. So only include the test project into the application soultion, the web site will be included into the list of instruments.
    2.You should use static port to run the web test. By default the web appliction will be run under dynamic port, and the web appliction will be hosted on WebDeb.WebServer.
        2.1 type for example: /B webdev.webserver.exe /port:8000
      /path:"c:\documents and settings\MMichael\Local
      Settings\Temp\HelloWorldWebSite"  /vpath:/WebSite1
       2.2 record the web action using the static port: http://localhost:8000/WebSite1/Default.aspx
    3.Add dll to instruments. note: all the dlls should has their PDB files, or the instruments will fail.
    4.Code coverage can only be run on PC with the source code,  so it means it is necessary to install the VS 2005 TEAM SUITE on the test server to run code coverage.(I ever spent 2 hours in uninstalling VS2005 PRO and installing the VS2005 TEAM SUTIE, It is time-consuming, HEHE )
    5.I don't see the function of report generation after the code coverage finishes. There is only a function to export the code coverage result to an xml file. So maybe it need to write code to read the xml file and generate report.
     
    November 20

    Visual Studio 2005 Team System:构建健壮而可靠的软件

    http://www.microsoft.com/china/msdn/library/langtool/vsts/dnvsentvstsdev.mspx

    Tips:

    1.FxCop 是一个静态分析工具,它分析托管代码程序集并报告有关程序集的信息,开发人员可以在项目的 Property Pages 上选择 Run FxCop

    2.

    分析应用程序(Analize the performance of application and generate relevant reports)

    分析应用程序的首选使用模式是首先开始采样,然后根据采样所产生的结果检测应用程序的特定方面。

    分析应用程序的过程相当简单。首先创建一个新的性能会话。在 Visual Studio 2005 Team System 中,可以使用 Performance Session Wizard 创建一个新的性能会话。

    Performance Session Wizard

    Performance Session Wizard 设置分析应用程序的必要环境。在 Visual Studio 2005 Team System 中,该向导为 EXE、DLL 和 ASP.NET 应用程序提供内置支持。

    一旦应用程序执行完毕,系统就会将一个性能会话报告自动添加到 Reports 节点。

    November 14

    Lock types in SQL Server

    SH_INT and EX_INT
    Intent locks that are taken on a higher-level item (for example, a table) before lower-level locks (for example, a page) can be taken, because the lock manager is unaware of the relationship between different types of items (in this case, pages and tables). If an EX_INT lock was not taken on the table before taking EX_PAG locks on the pages, another user could take an EX_TAB lock on the same table and the lock manager would not know that a conflict existed. Currently, SQL Server has intent locks only on tables. There are two kinds of intent locks: shared (SH_INT) and exclusive (EX_INT) locks.

    EX_PAGE
    This is an exclusive page lock that is taken when a page is updated due to a DELETE, UPDATE, or INSERT statement with insert row-level locking (IRL) disabled.

    UP_PAGE
    This is an update page lock that is taken in place of a shared-page lock when a page is scanned and the optimizer knows that the page will be updated (or the UPDLOCK hint is used).

    PR_EXT, NX_EXT, UPD_EXT, and EX_EXT
    These locks are taken when allocating or deallocating disk space. UPD_EXT is taken when allocating or deallocating a page from an existing extent and the others are used when allocating or deallocating entire extents.

    IX_PAGE and LN_PAGE
    These are IRL locks. IX_PAGE is an intent-to-do-row-locking lock on a page. LN_PAGE is taken when a page on which IRL is being done needs to be split.

    RLOCK and XRLOCK
    These short-term locks are taken when traversing an index b-tree. There are two types of this kind of lock: shared (RLOCK) and exclusive (XRLOCK). Shared locks are taken during scan, while exclusive locks are taken on index pages during an update.

    EX_TAB
    This is an exclusive table lock that occurs when the SQL Server optimizer determines that a table scan is the most efficient way to solve an update query (for example, when there are no indexes on a table). EX_TAB locks also appear when you lock the table with TABLOCKX hint or when SQL Server escalates the page locks on a table to a table lock.

    SH_TAB
    This is a shared table lock that is used when the optimizer assumes that most of the table will be scanned (or page locking escalates) or the TABLOCK hint is used.
     
    On the whole, there are two types locks, the shared lock and exclusive lock, just both of the two locks are used on different situation to lock table, page or memory block.
    November 10

    How To: Use SQL Profiler && How To: analysis and avoid Deadlock

     
     
     

    Identifying Execution Duration

    If you want to find out how long a stored procedure took to run, you need to capture the SP:Completed event. To identify the execution duration of individual statements within a stored procedure, use SP:StmtCompleted.

    To identify stored procedure execution duration

    1. Stop the trace if it is already running.
    2. On the File menu, click Properties.
    3. On the Events tab, expand the Stored procedures event class present in the Available Event Class list box and select SP:Completed.
    4. Click Add, and then click Run.
    5. Run the stored procedures you want to analyze. For example, using SQL Query Analyzer and the Pubs database:
      sp_help authors
      

      SQL Query Analyzer displays a list of stored procedure executions.

    To identify statement execution duration within a stored procedure

    1. Stop the trace if it is already running.
    2. On the File menu, click Properties.
    3. On the Events tab, expand the Stored Procedures event class and select SP:StmtCompleted.
    4. Click Add, and then click Run.

      SQL Profiler shows statement and stored procedure execution.

    5. Run the stored procedures you want to analyze. For example, using SQL Query Analyzer and the Pubs database:
      sp_help authors
      

      SQL Query Analyzer displays a list of statement executions.

    October 31

    Deadlock Scenarios

     

    INF: Deadlock Scenarios and Avoidance in SQL

     

    SUMMARY

    This article describes common deadlock scenarios and avoidance techniques.

    In the classic deadlock, a transaction locks record 1 and later locks record 2. If a second transaction attempts to lock the same two records, but in the opposite order, and happens to run just after the first transaction acquired its first lock, a deadlock occurs.

    In a single-server environment, SQL Server detects this kind of deadlock and abnormally terminates one of the transactions. If a single client application is performing updates on multiple SQL servers, permanent deadlocks are possible because the participating SQL Servers do not communicate with each other regarding lock status and requests. This is true even if 2 phase commit service is being used.

    MORE INFORMATION

    Consider the following distributed update example:
    1. An application performs updates on its local server and also on a remote server.
    2. When another copy of that application runs on the other server, a deadlock occurs because "local" and "remote" are relative to each application.
    In absolute terms, the two applications are requesting their locks in opposite order.

    To correct this problem, request locks in the same absolute order.

    It is also possible for two applications to deadlock even if they request locks in the same order, as in the following example:
    1. An application reads a record with holdlock (to prevent others from updating what it has read) and later updates the record.
    2. Two copies of this application can deadlock because both can get the readlock, but neither can update (each is waiting for the other to release the readlock).
    SQL Server detects this kind of deadlock on a single server; however, it cannot do so if the lock dependencies span multiple servers.

    SQL Server阻塞

    http://tech.ccidnet.com/art/1105/20050728/297985_1.html 阻塞定义   当来自应用程序的第一个连接控制锁而第二个连接需要相冲突的锁类型时,将发生阻塞。其结果是强制第二个连接等待,而在第一个连接上阻塞。不管是来自同一应用程序还是另外一台客户机上单独的应用程序,一个连接都可以阻塞另一个连接。   说明:一些需要锁保护的操作可能不明显,例如系统目录表和索引上的锁。大多数阻塞问题的发生是因为一个进程控制锁的时间过长,导致阻塞的进程链都在其它进程上等待锁。 常见的阻塞情形   1. 提交执行时间长的查询。   长时间运行的查询会阻塞其它查询。例如,影响很多行的 DELETE 或 UPDATE 操作能获取很多锁,这些锁不论是否升级到表锁都阻塞其它查询。因此,一般不要将长时间运行的决策支持查询和联机事务处理(OLTP)查询混在一起。解决方案是想办法优化查询,如更改索引、将大的复杂查询分成简单的查询或在空闲时间或单独的计算机上运行查询。   2. 查询不适当地使用游标。游标可能是在结果集中浏览的便利方法,但使用游标可能比使用面向集合的查询慢。   3. 取消没有提交或回滚的查询。   如果应用程序取消查询(如使用开放式数据库连接 (ODBC) sqlcancel 函数)但没有同时发出所需数目的 ROLLBACK 和 COMMIT 语句,则会发生这种情况。取消查询并不自动回滚或提交事务。取消查询后,所有在事务内获取的锁都将保留。应用程序必须提交或回滚已取消的事务,从而正确地管理事务嵌套级。   4. 应用程序没处理完所有结果。   将查询发送到服务器后,所有应用程序必须立即完成提取所有结果行。如果应用程序没有提取所有结果行,锁可能会留在表上而阻塞其他用户。如果使用的应用程序将Transact-SQL 语句透明地提交给服务器,则该应用程序必须提取所有结果行。如果应用程序没这样做(如果无法配置它执行此操作),则可能无法解决阻塞问题。为避免此问题,可以将这些应用程序限制在报表或决策支持数据库上。   5. 分布式客户端/服务器死锁。   与常规死锁不同,分布式死锁无法由 Microsoft SQL Server 2000 自动检测到。如果应用程序打开多个与 SQL Server 的连接并异步提交查询,则可能会发生分布式客户端/服务器死锁。   例如,一个客户端应用程序线程有两个开放式连接。该线程异步启动事务并在第一个连接上发出查询。应用程序随后启动其它事务,在另一个连接上发出查询并等待结果。当 SQL Server 返回其中一个连接的结果时,应用程序开始处理这些结果。应用程序就这样处理结果,直到生成结果的查询被另一个连接上执行的查询阻塞而导致再没有可用的结果为止。此时第一个连接阻塞,无限期等待处理更多的结果。第二个连接没有在锁上阻塞,但仍试图将结果返回给应用程序。然而,由于应用程序阻塞而在第一个连接上等待结果,第二个连接的结果将得不到处理。 避免阻塞的方法   1. 对每个查询使用查询超时。   2. 对每个查询使用锁定超时。有关更多信息,请参见自定义锁超时。   3. 使用绑定连接。有关更多信息,请参见使用绑定连接。   4. SQL Server 本质上是受客户端应用程序操纵的傀儡。客户端应用程序对服务器上获取的锁几乎有完全的控制(并对锁负责)。虽然 SQL Server 锁管理器自动使用锁保护事务,但这受客户端应用程序发出的查询类型和对结果的处理方式的直接鼓动。因此,大多数阻塞问题的解决方案都涉及检查客户端应用程序。   5. 阻塞问题常要求检查应用程序提交的 SQL 语句本身,以及检查与连接管理、所有结果行的处理等有关的应用程序行为本身。如果开发工具不允许显式控制连接管理、查询超时、结果处理等,阻塞问题可能得不到解决。 设计应用程序以避免阻塞的准则   1. 不要使用或设计使用户得以填写编辑框的应用程序,编辑框会生成长时间运行的查询。例如,不要使用或设计提示用户输入的应用程序,允许某些字段保留空白或允许输入通配符。这可能导致应用程序提交运行时间过长的查询,从而导致阻塞问题。   2. 不要使用或设计使用户得以在事务内输入内容的应用程序。   3. 允许取消查询。   4. 使用查询或锁定超时,防止失控查询和避免分布式死锁。   5. 立即完成提取所有结果行。   6. 使事务尽可能简短。   7. 显式控制连接管理。   8. 在所预计的并发用户全负荷下对应用程序进行应力测试。

    How to use sp_executesql

    From: http://www.588188.com/netbook/sqlserver2000/acdata/ac_8_con_04_9uek.htm

     

    使用 sp_executesql

    建议使用 sp_executesql 而不要使用 EXECUTE 语句执行字符串。支持参数替换不仅使 sp_executesql 比 EXECUTE 更通用,而且还使 sp_executesql 更有效,因为它生成的执行计划更有可能被 SQL Server 重新使用。

    自包含批处理

    sp_executesql 或 EXECUTE 语句执行字符串时,字符串被作为其自包含批处理执行。SQL Server 将Transact-SQL 语句或字符串中的语句编译进一个执行计划,该执行计划独立于包含 sp_executesql 或 EXECUTE 语句的批处理的执行计划。下列规则适用于自含的批处理:

    • 直到执行 sp_executesql 或EXECUTE 语句时才将sp_executesql 或 EXECUTE 字符串中的 Transact-SQL 语句编译进执行计划。执行字符串时才开始分析或检查其错误。执行时才对字符串中引用的名称进行解析。

    • 执行的字符串中的 Transact-SQL 语句,不能访问 sp_executesql 或 EXECUTE 语句所在批处理中声明的任何变量。包含 sp_executesql 或 EXECUTE 语句的批处理不能访问执行的字符串中定义的变量或局部游标。

    • 如果执行字符串有更改数据库上下文的 USE 语句,则对数据库上下文的更改仅持续到 sp_executesql 或 EXECUTE 语句完成。

    通过执行下列两个批处理来举例说明:

    /* Show not having access to variables from the calling batch. */
    DECLARE @CharVariable CHAR(3)
    SET @CharVariable = 'abc'
    /* sp_executesql fails because @CharVariable has gone out of scope. */
    sp_executesql N'PRINT @CharVariable'
    GO
    
    /* Show database context resetting after sp_executesql completes. */
    USE pubs
    GO
    sp_executesql N'USE Northwind'
    GO
    /* This statement fails because the database context
       has now returned to pubs. */
    SELECT * FROM Shippers
    GO
    
    替换参数值

    sp_executesql 支持对 Transact-SQL 字符串中指定的任何参数的参数值进行替换,但是 EXECUTE 语句不支持。因此,由 sp_executesql 生成的 Transact-SQL 字符串比由 EXECUTE 语句所生成的更相似。SQL Server 查询优化器可能将来自 sp_executesql 的 Transact-SQL 语句与以前所执行的语句的执行计划相匹配,以节约编译新的执行计划的开销。

    使用 EXECUTE 语句时,必须将所有参数值转换为字符或 Unicode 并使其成为 Transact-SQL 字符串的一部分:

    DECLARE @IntVariable INT
    DECLARE @SQLString NVARCHAR(500)
    /* Build and execute a string with one parameter value. */
    SET @IntVariable = 35
    SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
                     CAST(@IntVariable AS NVARCHAR(10))
    EXEC(@SQLString)
    /* Build and execute a string with a second parameter value. */
    SET @IntVariable = 201
    SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
                     CAST(@IntVariable AS NVARCHAR(10))
    EXEC(@SQLString)
    

    如果语句重复执行,则即使仅有的区别是为参数所提供的值不同,每次执行时也必须生成全新的 Transact-SQL 字符串。从而在下面几个方面产生额外的开销:

    • SQL Server 查询优化器具有将新的 Transact-SQL 字符串与现有的执行计划匹配的能力,此能力被字符串文本中不断更改的参数值妨碍,特别是在复杂的 Transact-SQL 语句中。

    • 每次执行时均必须重新生成整个字符串。

    • 每次执行时必须将参数值(不是字符或 Unicode 值)投影到字符或 Unicode 格式。

    sp_executesql 支持与 Transact-SQL 字符串相独立的参数值的设置:

    DECLARE @IntVariable INT
    DECLARE @SQLString NVARCHAR(500)
    DECLARE @ParmDefinition NVARCHAR(500)
    
    /* Build the SQL string once. */
    SET @SQLString =
         N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
    /* Specify the parameter format once. */
    SET @ParmDefinition = N'@level tinyint'
    
    /* Execute the string with the first parameter value. */
    SET @IntVariable = 35
    EXECUTE sp_executesql @SQLString, @ParmDefinition,
                          @level = @IntVariable
    /* Execute the same string with the second parameter value. */
    SET @IntVariable = 32
    EXECUTE sp_executesql @SQLString, @ParmDefinition,
                          @level = @IntVariable
    

    sp_executesql 示例完成的任务与前面的 EXECUTE 示例所完成的相同,但有下列额外优点:

    • 因为 Transact-SQL 语句的实际文本在两次执行之间未改变,所以查询优化器应该能将第二次执行中的 Transact-SQL 语句与第一次执行时生成的执行计划匹配。这样,SQL Server 不必编译第二条语句。

    • Transact-SQL 字符串只生成一次。

    • 整型参数按其本身格式指定。不需要转换为 Unicode。

    说明  为了使 SQL Server 重新使用执行计划,语句字符串中的对象名称必须完全符合要求。

    重新使用执行计划

    在 SQL Server 早期的版本中要重新使用执行计划的唯一方式是,将 Transact-SQL 语句定义为存储过程然后使应用程序执行此存储过程。这就产生了管理应用程序的额外开销。使用 sp_executesql 有助于减少此开销,并使 SQL Server 得以重新使用执行计划。当要多次执行某个 Transact-SQL 语句,且唯一的变化是提供给该 Transact-SQL 语句的参数值时,可以使用 sp_executesql 来代替存储过程。因为 Transact-SQL 语句本身保持不变仅参数值变化,所以 SQL Server 查询优化器可能重复使用首次执行时所生成的执行计划。

    下例为服务器上除四个系统数据库之外的每个数据库生成并执行 DBCC CHECKDB 语句:

    USE master
    GO
    SET NOCOUNT ON
    GO
    DECLARE AllDatabases CURSOR FOR
    SELECT name FROM sysdatabases WHERE dbid > 4
    
    OPEN AllDatabases
    
    DECLARE @DBNameVar NVARCHAR(128)
    DECLARE @Statement NVARCHAR(300)
    
    FETCH NEXT FROM AllDatabases INTO @DBNameVar
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
       PRINT N'CHECKING DATABASE ' + @DBNameVar
       SET @Statement = N'USE ' + @DBNameVar + CHAR(13)
          + N'DBCC CHECKDB (' + @DBNameVar + N')'
       EXEC sp_executesql @Statement
       PRINT CHAR(13) + CHAR(13)
       FETCH NEXT FROM AllDatabases INTO @DBNameVar
    END
    
    CLOSE AllDatabases
    DEALLOCATE AllDatabases
    GO
    SET NOCOUNT OFF
    GO
    

    当目前所执行的 Transact-SQL 语句包含绑定参数标记时,SQL Server ODBC 驱动程序使用 sp_executesql 完成 SQLExecDirect。但例外情况是 sp_executesql 不用于执行中的数据参数。这使得使用标准 ODBC 函数或使用在 ODBC 上定义的 API(如 RDO)的应用程序得以利用 sp_executesql 所提供的优势。定位于 SQL Server 2000 的现有的 ODBC 应用程序不需要重写就可以自动获得性能增益。有关更多信息,请参见使用语句参数

    用于 SQL Server 的 Microsoft OLE DB 提供程序也使用 sp_executesql 直接执行带有绑定参数的语句。使用 OLE DB 或 ADO 的应用程序不必重写就可以获得 sp_executesql 所提供的优势。