SQLServer資料庫效能優化
阿新 • • 發佈:2019-01-05
/// <summary> /// 返回 GUID 用於資料庫操作,特定的時間程式碼可以提高檢索效率
/// </summary> /// <returns> COMB (GUID 與時間混合型) 型別 GUID 資料 </returns> public static Guid NewComb()
{
byte [] guidArray = System.Guid.NewGuid().ToByteArray();
DateTime baseDate = new DateTime( 1900 , 1 , 1 );
DateTime now = DateTime.Now;
// Get the days and milliseconds which will be used to build the byte string TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
TimeSpan msecs = new TimeSpan(now.Ticks - ( new DateTime(now.Year, now.Month, now.Day).Ticks));
// Convert to a byte array
// Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333 byte [] daysArray = BitConverter.GetBytes(days.Days);
byte [] msecsArray = BitConverter.GetBytes(( long )(msecs.TotalMilliseconds/ 3.333333 ));
// Reverse the bytes to match SQL Servers ordering Array.Reverse(daysArray);
Array.Reverse(msecsArray);
// Copy the bytes into the guid Array.Copy(daysArray, daysArray.Length - 2 , guidArray, guidArray.Length - 6 , 2 );
Array.Copy(msecsArray, msecsArray.Length - 4 , guidArray, guidArray.Length - 4 , 4 );
return new System.Guid(guidArray);
}
/// <summary> /// 從 SQL SERVER 返回的 GUID 中生成時間資訊
/// </summary> /// <param name="guid"> 包含時間資訊的 COMB </param> /// <returns> 時間 </returns> public static DateTime GetDateFromComb(System.Guid guid)
{
DateTime baseDate = new DateTime( 1900 , 1 , 1 );
byte [] daysArray = new byte [ 4 ];
byte [] msecsArray = new byte [ 4 ];
byte [] guidArray = guid.ToByteArray();
// Copy the date parts of the guid to the respective byte arrays. Array.Copy(guidArray, guidArray.Length - 6 , daysArray, 2 , 2 );
Array.Copy(guidArray, guidArray.Length - 4 , msecsArray, 0 , 4 );
// Reverse the arrays to put them into the appropriate order Array.Reverse(daysArray);
Array.Reverse(msecsArray);
// Convert the bytes to ints int days = BitConverter.ToInt32(daysArray, 0 );
int msecs = BitConverter.ToInt32(msecsArray, 0 );
DateTime date = baseDate.AddDays(days);
date = date.AddMilliseconds(msecs * 3.333333 );
return date;
}
/// </summary> /// <returns> COMB (GUID 與時間混合型) 型別 GUID 資料 </returns> public static Guid NewComb()
{
byte [] guidArray = System.Guid.NewGuid().ToByteArray();
DateTime baseDate = new DateTime( 1900 , 1 , 1 );
DateTime now = DateTime.Now;
// Get the days and milliseconds which will be used to build the byte string TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
TimeSpan msecs = new TimeSpan(now.Ticks - ( new DateTime(now.Year, now.Month, now.Day).Ticks));
// Convert to a byte array
// Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
byte [] msecsArray = BitConverter.GetBytes(( long )(msecs.TotalMilliseconds/ 3.333333 ));
// Reverse the bytes to match SQL Servers ordering Array.Reverse(daysArray);
Array.Reverse(msecsArray);
// Copy the bytes into the guid Array.Copy(daysArray, daysArray.Length -
Array.Copy(msecsArray, msecsArray.Length - 4 , guidArray, guidArray.Length - 4 , 4 );
return new System.Guid(guidArray);
}
/// <summary> /// 從 SQL SERVER 返回的 GUID 中生成時間資訊
/// </summary> /// <param name="guid"> 包含時間資訊的 COMB </param> /// <returns> 時間 </returns> public static DateTime GetDateFromComb(System.Guid guid)
{
DateTime baseDate = new DateTime( 1900 , 1 , 1 );
byte [] daysArray = new byte [ 4 ];
byte [] msecsArray = new byte [ 4 ];
byte [] guidArray = guid.ToByteArray();
// Copy the date parts of the guid to the respective byte arrays. Array.Copy(guidArray, guidArray.Length - 6 , daysArray, 2 , 2 );
Array.Copy(guidArray, guidArray.Length - 4 , msecsArray, 0 , 4 );
// Reverse the arrays to put them into the appropriate order Array.Reverse(daysArray);
Array.Reverse(msecsArray);
// Convert the bytes to ints int days = BitConverter.ToInt32(daysArray, 0 );
int msecs = BitConverter.ToInt32(msecsArray, 0 );
DateTime date = baseDate.AddDays(days);
date = date.AddMilliseconds(msecs * 3.333333 );
return date;
}