提问人:David C 提问时间:11/17/2023 更新时间:11/17/2023 访问量:31
不同服务器上的 SQL Server 还原数据库时差
SQL Server Restore Database Time Difference on Different Servers
问:
我有两台服务器:
服务器 A - 安装了 64 GB RAM 的 Windows 2019 Standard 服务器。 - 具有 1.26 TB 内存的 D 驱动器
服务器 B - 安装了 200 GB RAM 的 Windows 2022 Standard 服务器。 - 具有 2.5 TB 内存的 D 驱动器
作为代理作业的一部分,我使用 azcopy 将 .bak 文件 (~90gb) 从服务器 C 复制到 服务器 A 和 B。复制到任一服务器所需的时间相同。 在服务器 A 上还原备份大约需要 40 分钟。在服务器 B 上 恢复始终需要 60 分钟以上。服务器 A 上其余的 ETL 比 B 快 40 分钟左右,它们运行相同的作业。
什么原因可能导致恢复速度的差异?
答:
本文可能会解释性能差异。https://www.diskpart.com/server-2022/windows-server-2022-very-slow-0725.html
Server 2022 对硬件有更高的要求,以实现更高的性能;如果您的 Windows Server 性能较差,则可能与硬件和其他原因有关。
呃,我讨厌人们试图猜测哪些字符是有效的。除了完全不可移植(总是考虑 Mono)之外,前面的两条评论都遗漏了更多的 25 个无效字符。
foreach (var c in Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(c, '-');
}
或者在 VB 中:
'Clean just a filename
Dim filename As String = "salmnas dlajhdla kjha;dmas'lkasn"
For Each c In IO.Path.GetInvalidFileNameChars
filename = filename.Replace(c, "")
Next
'See also IO.Path.GetInvalidPathChars
评论
我同意 Grauenwolf 的观点,并强烈推荐Path.GetInvalidFileNameChars()
这是我的 C# 贡献:
string file = @"38?/.\}[+=n a882 a.a*/|n^%$ ad#(-))";
Array.ForEach(Path.GetInvalidFileNameChars(),
c => file = file.Replace(c.ToString(), String.Empty));
p.s. -- 这比它应该的更隐晦 -- 我试图简明扼要。
评论
Array.ForEach
foreach
Path.GetInvalidFileNameChars().Aggregate(file, (current, c) => current.Replace(c, '-'))
如果您想快速去除所有特殊字符,这有时对于文件名来说更易读,这很好用:
string myCrazyName = "q`w^e!r@t#y$u%i^o&p*a(s)d_f-g+h=j{k}l|z:x\"c<v>b?n[m]q\\w;e'r,t.y/u";
string safeName = Regex.Replace(
myCrazyName,
"\W", /*Matches any nonword character. Equivalent to '[^A-Za-z0-9_]'*/
"",
RegexOptions.IgnoreCase);
// safeName == "qwertyuiopasd_fghjklzxcvbnmqwertyu"
评论
\W
[^A-Za-z0-9_]
.
这是我现在正在使用的函数(感谢 jcollum 的 C# 示例):
public static string MakeSafeFilename(string filename, char replaceChar)
{
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
filename = filename.Replace(c, replaceChar);
}
return filename;
}
为了方便起见,我只是把它放在“助手”类中。
要去除无效字符:
static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
// Builds a string out of valid chars
var validFilename = new string(filename.Where(ch => !invalidFileNameChars.Contains(ch)).ToArray());
要替换无效字符:
static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
// Builds a string out of valid chars and an _ for invalid ones
var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? '_' : ch).ToArray());
要替换无效字符(并避免潜在的名称冲突,如 Hell* vs Hell$):
static readonly IList<char> invalidFileNameChars = Path.GetInvalidFileNameChars();
// Builds a string out of valid chars and replaces invalid chars with a unique letter (Moves the Char into the letter range of unicode, starting at "A")
var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? Convert.ToChar(invalidFileNameChars.IndexOf(ch) + 65) : ch).ToArray());
这个问题以前已经问过很多次了,而且正如之前多次指出的那样,是不够的。IO.Path.GetInvalidFileNameChars
首先,有许多名称(如 PRN 和 CON)是保留的,不允许用于文件名。仅在根文件夹中不允许使用其他名称。也不允许使用以句点结尾的名称。
其次,存在各种长度限制。在此处阅读 NTFS 的完整列表。
第三,您可以附加到具有其他限制的文件系统。例如,ISO 9660 文件名不能以“-”开头,但可以包含它。
第四,如果两个进程“任意”选择相同的名称,你会怎么做?
通常,使用外部生成的文件名名称是一个坏主意。我建议生成自己的私有文件名,并在内部存储人类可读的名称。
评论
我发现使用它既快速又易于理解:
<Extension()>
Public Function MakeSafeFileName(FileName As String) As String
Return FileName.Where(Function(x) Not IO.Path.GetInvalidFileNameChars.Contains(x)).ToArray
End Function
这之所以有效,是因为 a 是数组,并且有一个接受数组的构造函数字符串。string
IEnumerable
char
string
char
static class Utils
{
public static string MakeFileSystemSafe(this string s)
{
return new string(s.Where(IsFileSystemSafe).ToArray());
}
public static bool IsFileSystemSafe(char c)
{
return !Path.GetInvalidFileNameChars().Contains(c);
}
}
这是我刚刚添加到 ClipFlair (http://github.com/Zoomicon/ClipFlair) StringExtensions 静态类(Utils.Silverlight 项目)中的内容,基于从上面 Dour High Arch 发布的相关 stackoverflow 问题的链接中收集的信息:
public static string ReplaceInvalidFileNameChars(this string s, string replacement = "")
{
return Regex.Replace(s,
"[" + Regex.Escape(new String(System.IO.Path.GetInvalidPathChars())) + "]",
replacement, //can even use a replacement string of any length
RegexOptions.IgnoreCase);
//not using System.IO.Path.InvalidPathChars (deprecated insecure API)
}
评论
这是我的版本:
static string GetSafeFileName(string name, char replace = '_') {
char[] invalids = Path.GetInvalidFileNameChars();
return new string(name.Select(c => invalids.Contains(c) ? replace : c).ToArray());
}
我不确定 GetInvalidFileNameChars 的结果是如何计算的,但“Get”表明它很重要,所以我缓存了结果。此外,这只会遍历输入字符串一次,而不是多次,就像上面的解决方案一样,迭代无效字符集,一次替换源字符串中的一个字符。另外,我喜欢基于 Where 的解决方案,但我更喜欢替换无效的字符而不是删除它们。最后,我的替换正好是一个字符,以避免在我遍历字符串时将字符转换为字符串。
我说了所有没有做分析的事情——这个对我来说只是“感觉”很好。: )
评论
new HashSet<char>(Path.GetInvalidFileNameChars())
private void textBoxFileName_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = CheckFileNameSafeCharacters(e);
}
/// <summary>
/// This is a good function for making sure that a user who is naming a file uses proper characters
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
internal static bool CheckFileNameSafeCharacters(System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar.Equals(24) ||
e.KeyChar.Equals(3) ||
e.KeyChar.Equals(22) ||
e.KeyChar.Equals(26) ||
e.KeyChar.Equals(25))//Control-X, C, V, Z and Y
return false;
if (e.KeyChar.Equals('\b'))//backspace
return false;
char[] charArray = Path.GetInvalidFileNameChars();
if (charArray.Contains(e.KeyChar))
return true;//Stop the character from being entered into the control since it is non-numerical
else
return false;
}
为什么不将字符串转换为 Base64 等效项,如下所示:
string UnsafeFileName = "salmnas dlajhdla kjha;dmas'lkasn";
string SafeFileName = Convert.ToBase64String(Encoding.UTF8.GetBytes(UnsafeFileName));
如果你想把它转换回来,以便你可以阅读它:
UnsafeFileName = Encoding.UTF8.GetString(Convert.FromBase64String(SafeFileName));
我用它来保存PNG文件,这些文件具有随机描述中的唯一名称。
评论
许多 anwer 建议使用这对我来说似乎是一个糟糕的解决方案。我鼓励您使用白名单而不是黑名单,因为黑客最终总会找到绕过它的方法。Path.GetInvalidFileNameChars()
下面是一个您可以使用的代码示例:
string whitelist = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
foreach (char c in filename)
{
if (!whitelist.Contains(c))
{
filename = filename.Replace(c, '-');
}
}
从我的旧项目中,我找到了这个解决方案,它已经完美地运行了 2 年多。我正在用“!”替换非法字符,然后检查双倍!!'s,使用你自己的字符。
public string GetSafeFilename(string filename)
{
string res = string.Join("!", filename.Split(Path.GetInvalidFileNameChars()));
while (res.IndexOf("!!") >= 0)
res = res.Replace("!!", "!");
return res;
}
我接受了 Jonathan Allen 的答案,并制作了一个可以在任何字符串上调用的扩展方法。
public static class StringExtensions
{
public static string ReplaceInvalidFileNameChars(this string input, char replaceCharacter = '-')
{
foreach (char c in Path.GetInvalidFileNameChars())
{
input = input.Replace(c, replaceCharacter);
}
return input;
}
}
然后可以像这样使用:
string myFileName = "test > file ? name.txt";
string myValidFileName1 = myFileName.ReplaceInvalidFileNameChars();
string myValidFileName2 = myFileName.ReplaceInvalidFileNameChars('');
string myValidFileName3 = myFileName.ReplaceInvalidFileNameChars('_');
评论