2010-07-08

SQL Server 2005's new added Ranking functions

Ranking functions return a ranking value for each row in a partition. Depending on the function that is used, some rows might receive the same value as other rows. Ranking functions are nondeterministic.

Transact-SQL provides the following ranking functions:

The following shows the four ranking functions used in the same query. For function specific examples, see each ranking function.

USE AdventureWorks2008R2;
GO
SELECT p.FirstName, p.LastName
,ROW_NUMBER() OVER (ORDER BY a.PostalCode) AS 'Row Number'
,RANK() OVER (ORDER BY a.PostalCode) AS 'Rank'
,DENSE_RANK() OVER (ORDER BY a.PostalCode) AS 'Dense Rank'
,NTILE(4) OVER (ORDER BY a.PostalCode) AS 'Quartile'
,s.SalesYTD, a.PostalCode
FROM Sales.SalesPerson s
INNER JOIN Person.Person p
ON s.BusinessEntityID = p.BusinessEntityID
INNER JOIN Person.Address a
ON a.AddressID = p.BusinessEntityID
WHERE TerritoryID IS NOT NULL
AND SalesYTD <> 0;





Here is the result set.





FirstName



LastName



Row Number



Rank



Dense Rank



Quartile



SalesYTD



PostalCode



Michael



Blythe



1



1



1



1



4557045.0459



98027



Linda



Mitchell



2



1



1



1



5200475.2313



98027



Jillian



Carson



3



1



1



1



3857163.6332



98027



Garrett



Vargas



4



1



1



1



1764938.9859



98027



Tsvi



Reiter



5



1



1



2



2811012.7151



98027



Shu



Ito



6



6



2



2



3018725.4858



98055



José



Saraiva



7



6



2



2



3189356.2465



98055



David



Campbell



8



6



2



3



3587378.4257



98055



Tete



Mensa-Annan



9



6



2



3



1931620.1835



98055



Lynn



Tsoflias



10



6



2



3



1758385.926



98055



Rachel



Valdez



11



6



2



4



2241204.0424



98055



Jae



Pak



12



6



2



4



5015682.3752



98055



Ranjit



Varkey Chudukatil



13



6



2



4



3827950.238



98055







2010-07-06

select Nth to Mth record from a table




select top 10 * from (select top 40 * from tb desc) asc

select top 40 * from A where id not in(select top 30 id from A)

which of above 2 is more efficient? why?


in sql 2005+ ,you should use

SELECT col1, col2
FROM
(
SELECT col1
, col2, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum
FROM
MyTable
) AS MyDerivedTable
WHERE
MyDerivedTable.RowNum BETWEEN @startRow AND @endRow

2010-07-01

Relationships Between Classes

Relationships Between Classes

In a class diagram, obviously you can't have classes just floating around; you need to see the relationship between them. The following table shows the kinds of relationships between classes, their notation, and what they mean.

Sr. No.RelationSymbolDescription
1AssociationWhen two classes are connected to each other in any way, an association relation is established. For example: A "student studies in a college" association can be shown as:
1 a.MultiplicityAn example of this kind of association is many students belonging to the same college. Hence, the relation shows a star sign near the student class (one to many, many to many, and so forth kind of relations).
1 b.Directed AssociationAssociation between classes is bi-directional by default. You can define the flow of the association by using a directed association. The arrowhead identifies the container-contained relationship.
1 c.Reflexive AssociationNo separate symbol. However, the relation will point back at the same class.An example of this kind of relation is when a class has a variety of responsibilities. For example, an employee of a college can be a professor, a housekeeper, or an administrative assistant.
2AggregationWhen a class is formed as a collection of other classes, it is called an aggregation relationship between these classes. It is also called a "has a" relationship.
2 a.CompositionComposition is a variation of the aggregation relationship. Composition connotes that a strong life cycle is associated between the classes.
3Inheritance/GeneralizationAlso called an "is a" relationship, because the child class is a type of the parent class. Generalization is the basic type of relationship used to define reusable elements in the class diagram. Literally, the child classes "inherit" the common functionality defined in the parent class.
4RealizationIn a realization relationship, one entity (normally an interface) defines a set of functionalities as a contract and the other entity (normally a class) "realizes" the contract by implementing the functionality defined in the contract.

2010-06-10

ASP.NET缓存

生成高性能、可缩放的 Web 应用程序最重要的因素之一是能够在首次请求项时将这些项存储在内存中,不管它们是数据对象、页还是页的某些部分。 可以将这些项缓存或存储在 Web 服务器上或请求流中的其他软件上,如代理服务器或浏览器。 使您能够避免重新创建满足先前请求的信息,尤其是那些需要大量处理器时间或资源的信息。 ASP.NET 缓存允许您使用多种技术跨 HTTP 请求存储页输出或应用程序数据并对其进行重复使用。

ASP.NET 提供了两种您可以用来创建高性能 Web 应用程序的缓存类型。 第一种类型是输出缓存,它允许将动态页和用户控件响应存储在输出流(从发起服务器到请求浏览器)中任何具备 HTTP 1.1 缓存功能的设备上。 当后面的请求发生时,不执行页或用户控件代码,缓存的输出用于满足该请求。 第二种类型的缓存是应用程序数据缓存,可以使用它以编程方式将任意对象(如应用程序数据)存储到服务器内存中,这样您的应用程序可以节省重新创建这些对象所需的时间和资源。

输出缓存:

以声明方式设置页的可缓存性

在页中包含 @ OutputCache 指令,并定义 Duration 和 VaryByParam 特性。

在 @ OutputCache 指令中包含 Location 特性,并将其值定义为 OutputCacheLocation 枚举中的下列值之一:Any、Client、Downstream、Server、ServerAndClient 或 None。

下面的代码演示如何将页的可缓存性设置为 60 秒:

<%@ OutputCache Duration="60" VaryByParam="None"%>

保存页面的多个版本:

<%@ OutputCache Duration="60" VaryByParam="productId"%>

这样,形如

www.goodasp.net?productId=15

www.goodasp.net?productId=1357

的页面各自有一个缓存版本

或许我们在页面里一个dropdownlist ddlCustomer,一个grid gridBuyDetail,grid根据所选的dropdownlist的值

呈现详细信息,我们可以设置页面缓存为:

<%@ OutputCache Duration="60" VaryByControl="productId"%>

使用缓存配置文件以声明方式设置页的可缓存性

在应用程序的 Web.config 文件中定义缓存配置文件,在配置文件中包括 duration 和 varyByParam 设置。

下面的 配置元素定义名为 Cache30Seconds 的缓存配置文件,它将在服务器上将页缓存 30 秒之久。

<caching>

  <outputCacheSettings>

    <outputCacheProfiles>

      <add name="Cache30Seconds" duration="30"

        varyByParam="none" />

    </outputCacheProfiles>

  </outputCacheSettings>

</caching>

在使用配置文件的每个 ASP.NET 页中包含 @ OutputCache 指令,并将 CacheProfile 特性设置为 Web.config 文件中定义的缓存配置文件的名称。

下面的代码指定页应当使用名为 Cache30Seconds 的缓存配置文件:

<%@ OutputCache CacheProfile="Cache30Seconds" %>

出于以下原因之一,ASP.NET 可以从缓存中移除数据:

由于服务器上的内存不足,开始一个称为“清理”的过程。
由于缓存中的项已过期。
由于项的依赖项发生了更改