2008年5月小记(??, #, DataContractJsonSerializer, CTE Ranking top)
生活随笔
收集整理的這篇文章主要介紹了
2008年5月小记(??, #, DataContractJsonSerializer, CTE Ranking top)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、??運算符
string?str?=?null;
str?=?str????"123";
Console.WriteLine(str????"234"); variable ?? defaultValue
相當于
variable == null ? defaultValue : variable
2、 使用href="javascript:void(0)"?代替href="#",避免url產生沒定義的錨點定位。
3、使用System.Runtime.Serialization.Json.DataContractJsonSerializer代替System.Web.Script.Serialization.JavaScriptSerializer
執行JSON的序列化和反序列化
????public?static?class?JSONExtension
????{
????????public?static?string?ToJSON(this?object?obj)
????????{
????????????DataContractJsonSerializer?serializer?=?new?DataContractJsonSerializer(obj.GetType());
????????????using?(MemoryStream?ms?=?new?MemoryStream())
????????????{
????????????????serializer.WriteObject(ms,?obj);
????????????????return?Encoding.UTF8.GetString(ms.ToArray());
????????????}
????????}
????????public?static?T?ParseJSON<T>(this?string?str)
????????{
????????????T?obj?=?Activator.CreateInstance<T>();
????????????using?(MemoryStream?ms?=?new?MemoryStream(Encoding.UTF8.GetBytes(str)))
????????????{
????????????????DataContractJsonSerializer?serializer?=?new?DataContractJsonSerializer(obj.GetType());
????????????????return?(T)serializer.ReadObject(ms);
????????????}
????????}
????} 注意:引用如下組件System.Runtime.Serialization,System.ServiceModel,System.ServiceModel.Web
4、解決一例數據庫服務器CPU占用資源100%的問題。
????最近根據用戶技能搜索好友的功能根據新的需求作了調整,要求搜索的結果增加like用戶名的結果集。例如:本來是以asp.net這種技能搜索出擁有這種技能的用戶列表以供用戶尋找好友,現在還要增加一個結果集是以asp.net開頭的用戶列表如:asp.net01,asp.net02。這兩個結果集的排序以論壇專家分的多少為排序的依據。
現在的情況是UserPost表具有15000000的數據量,并且對該表有一個全文檢索。根據技能搜索用戶列表是通過全文檢索進行(contains)得到,而根據用戶相擬是通過對表的搜索([UserName] like @key+'%')得到,并且還以order by communityscore desc進行排序。
????當這個新功能上線后數據庫的CPU總是高居不下,后來通過在數據庫端跟蹤SQL和存儲過程的調用情況就大致找到出問題的語句。分析了原來的語句的寫法是把contains和like都寫在一個where里了,查看一下執行計劃,對于全文檢索的搜索的調用耗資非常之大。同時從需求上分析,這個搜索的意圖主要還是以技能搜用戶為主,以用戶名相擬性搜索的用戶列表為輔,所以邏輯上可以調整為先從全文檢索中獲取擁有這此技能的用戶列表,再以用戶名相擬搜索用戶列表,然后進行組合。新的sp如下:
ALTER?PROCEDURE?[dbo].[ec_UserInfo_FindUsers2]?
????@top?int,
????@key?nvarchar(20)
AS
BEGIN
????SET?NOCOUNT?ON;
????WITH?temp(username,?communityscore)?AS
????(
???????select?top(@top)?username,communityscore?
???????from?dbo.userinfo?with?(nolock)?
???????where?contains(speciality,?@key)?
???????order?by?communityscore?desc
????),
????temp2(username,?communityscore,?theCount)?AS
????(
???????select?username,?communityscore,?row_number()?over(order?by?communityscore??desc)?as?row?from?temp
????)
????,temp3(username,?communityscore)?AS
????(
???????select?username,?communityscore?from?temp2?UNION?ALL?
???????select?top(?select?COALESCE((select?top?1?@top?-?theCount?from?temp2?order?by?theCount?desc),@top))?username,?communityscore?
???????????from?dbo.userinfo?with?(nolock)?where?[UserName]?like?@key+'%'?order?by?communityscore?desc
????)
????select?username?from?temp3?order?by?communityscore?desc
END
經過這樣的處理后就能把cpu降到正常水平了。
這里用到了幾個SQL2005的新功能。
a、CTE
b、Ranking函數集。row_number() over (order by xxx desc)
c、top(select ....)函數
string?str?=?null;
str?=?str????"123";
Console.WriteLine(str????"234"); variable ?? defaultValue
相當于
variable == null ? defaultValue : variable
2、 使用href="javascript:void(0)"?代替href="#",避免url產生沒定義的錨點定位。
3、使用System.Runtime.Serialization.Json.DataContractJsonSerializer代替System.Web.Script.Serialization.JavaScriptSerializer
執行JSON的序列化和反序列化
????public?static?class?JSONExtension
????{
????????public?static?string?ToJSON(this?object?obj)
????????{
????????????DataContractJsonSerializer?serializer?=?new?DataContractJsonSerializer(obj.GetType());
????????????using?(MemoryStream?ms?=?new?MemoryStream())
????????????{
????????????????serializer.WriteObject(ms,?obj);
????????????????return?Encoding.UTF8.GetString(ms.ToArray());
????????????}
????????}
????????public?static?T?ParseJSON<T>(this?string?str)
????????{
????????????T?obj?=?Activator.CreateInstance<T>();
????????????using?(MemoryStream?ms?=?new?MemoryStream(Encoding.UTF8.GetBytes(str)))
????????????{
????????????????DataContractJsonSerializer?serializer?=?new?DataContractJsonSerializer(obj.GetType());
????????????????return?(T)serializer.ReadObject(ms);
????????????}
????????}
????} 注意:引用如下組件System.Runtime.Serialization,System.ServiceModel,System.ServiceModel.Web
4、解決一例數據庫服務器CPU占用資源100%的問題。
????最近根據用戶技能搜索好友的功能根據新的需求作了調整,要求搜索的結果增加like用戶名的結果集。例如:本來是以asp.net這種技能搜索出擁有這種技能的用戶列表以供用戶尋找好友,現在還要增加一個結果集是以asp.net開頭的用戶列表如:asp.net01,asp.net02。這兩個結果集的排序以論壇專家分的多少為排序的依據。
現在的情況是UserPost表具有15000000的數據量,并且對該表有一個全文檢索。根據技能搜索用戶列表是通過全文檢索進行(contains)得到,而根據用戶相擬是通過對表的搜索([UserName] like @key+'%')得到,并且還以order by communityscore desc進行排序。
????當這個新功能上線后數據庫的CPU總是高居不下,后來通過在數據庫端跟蹤SQL和存儲過程的調用情況就大致找到出問題的語句。分析了原來的語句的寫法是把contains和like都寫在一個where里了,查看一下執行計劃,對于全文檢索的搜索的調用耗資非常之大。同時從需求上分析,這個搜索的意圖主要還是以技能搜用戶為主,以用戶名相擬性搜索的用戶列表為輔,所以邏輯上可以調整為先從全文檢索中獲取擁有這此技能的用戶列表,再以用戶名相擬搜索用戶列表,然后進行組合。新的sp如下:
ALTER?PROCEDURE?[dbo].[ec_UserInfo_FindUsers2]?
????@top?int,
????@key?nvarchar(20)
AS
BEGIN
????SET?NOCOUNT?ON;
????WITH?temp(username,?communityscore)?AS
????(
???????select?top(@top)?username,communityscore?
???????from?dbo.userinfo?with?(nolock)?
???????where?contains(speciality,?@key)?
???????order?by?communityscore?desc
????),
????temp2(username,?communityscore,?theCount)?AS
????(
???????select?username,?communityscore,?row_number()?over(order?by?communityscore??desc)?as?row?from?temp
????)
????,temp3(username,?communityscore)?AS
????(
???????select?username,?communityscore?from?temp2?UNION?ALL?
???????select?top(?select?COALESCE((select?top?1?@top?-?theCount?from?temp2?order?by?theCount?desc),@top))?username,?communityscore?
???????????from?dbo.userinfo?with?(nolock)?where?[UserName]?like?@key+'%'?order?by?communityscore?desc
????)
????select?username?from?temp3?order?by?communityscore?desc
END
經過這樣的處理后就能把cpu降到正常水平了。
這里用到了幾個SQL2005的新功能。
a、CTE
b、Ranking函數集。row_number() over (order by xxx desc)
c、top(select ....)函數
轉載于:https://www.cnblogs.com/chenjunbiao/archive/2008/05/09/1760215.html
總結
以上是生活随笔為你收集整理的2008年5月小记(??, #, DataContractJsonSerializer, CTE Ranking top)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 毕业证
- 下一篇: linux dip 命令详解