SQL Server中内联表值的功能(Cost of Inline Table Valued function in SQL Server)
如果SQL直接内联,在SQL Server 2008中使用内联表值函数是否存在固有成本? 我们的应用程序非常大量地使用内联表值函数来重用常见查询,但最近,我们发现如果我们不使用它们,查询运行得更快。
考虑一下:
CREATE FUNCTION dbo.fn_InnerQuery (@asOfDate DATETIME) RETURNS TABLE AS RETURN ( SELECT ... -- common, complicated query here )
现在,当我这样做时:
SELECT TOP 10 Amount FROM dbo.fn_InnerQuery(dbo.Date(2009,1,1)) ORDER BY Amount DESC
查询返回结果大约15秒。
但是,当我这样做时:
SELECT TOP 10 Amount FROM ( SELECT ... -- inline the common, complicated query here ) inline ORDER BY Amount DESC
查询返回时间不到1秒。
在这种情况下,我对使用表值函数的开销感到有些困惑。 我原本没想到。 我们的应用程序中有很多表值函数,所以我想知道这里是否有我遗漏的东西。
Is there an inherent cost to using inline-table-valued functions in SQL Server 2008 that is not incurred if the SQL is inlined directly? Our application makes very heavy use of inline-table-valued functions to reuse common queries, but recently, we've found that queries run much faster if we don't use them.
Consider this:
CREATE FUNCTION dbo.fn_InnerQuery (@asOfDate DATETIME) RETURNS TABLE AS RETURN ( SELECT ... -- common, complicated query here )
Now, when I do this:
SELECT TOP 10 Amount FROM dbo.fn_InnerQuery(dbo.Date(2009,1,1)) ORDER BY Amount DESC
The query returns with results in about 15 seconds.
However, when I do this:
SELECT TOP 10 Amount FROM ( SELECT ... -- inline the common, complicated query here ) inline ORDER BY Amount DESC
The query returns in less than 1 second.
I'm a little baffled by the overhead of using the table valued function in this case. I did not expect that. We have a ton of table valued functions in our application, so I'm wondering if there is something I'm missing here.
原文:https://stackoverflow.com/questions/1301177
最满意答案
但事实证明
Sub
没有在原始指针上实现。您可以将指针转换为
usize
以对其进行数学运算:fn main() { let source = "hello, world"; let a = &source[1..]; let b = &source[5..]; let diff = b.as_ptr() as usize - a.as_ptr() as usize; println!("{}", diff); }
but it turns out that
Sub
is not implemented on raw pointers.You can convert the pointer to a
usize
to do math on it:fn main() { let source = "hello, world"; let a = &source[1..]; let b = &source[5..]; let diff = b.as_ptr() as usize - a.as_ptr() as usize; println!("{}", diff); }
There's also the unstable method
offset_from
:#![feature(ptr_offset_from)] fn main() { let source = "hello, world"; let a = &source[1..]; let b = &source[5..]; // I copied this unsafe code from Stack Overflow without // reading the text that told me how to know if this was safe let diff = unsafe { b.as_ptr().offset_from(a.as_ptr()) }; println!("{}", diff); }
Please be sure to read the documentation for this method as it describes under what circumstances it will not cause undefined behavior.
相关问答
更多-
这是一个可能工作的解决方案(或者可能不是:))。 问题在于偏移和行号之间的映射包含在pdb中(也就是调试符号) 采用与用于将二进制文件传输到客户端时完全相同(重要)的构建配置。 然后转到项目属性,选项卡“Build”,单击页面底部的“Advanced”按钮。 然后找到“调试信息”并选择“仅限pdb” 构建项目并转到输出文件夹。 选择ildasm ,将其添加到PATH或将其复制到某个位置。 在您的生成文件夹中执行ildasm /LINENUM /SOURCE YourFile.exe 。 它将输出具有偏移和源 ...
-
只需将文件读取为原始字节,UTF-8中的换行符将始终为13和13或10 ...但如果文件将要以字符串形式读取文件,则会出现同样的问题不同的EOL惯例。 BufferedReader的原始字节相当于BufferedInputStream 您也可以在不进行编码的情况下统计字符串的UTF-8字节: public static int byteCountUTF8(String input) { int ret = 0; for (int i = 0; i < input.length(); ++i) ...
-
ARM有两种指令模式,地址的最低有效位用于指示给定函数使用的模式。 Thumb模式的奇数地址,甚至是ARM模式的地址。 即使您使用-marm开关重新编译它,地址也是-marm 。 ARM has two instruction modes, and the least-significant bit of the address is used to indicate which mode a given function uses. Odd addresses for Thumb mode, and ev ...
-
// make it global double previousval=-1000; double currentVal=-1000; then in your code Joint right = skel.Joints[JointType.HandRight]; currentVal=right.Position.X; double offset = 0; if(prev==-1000) offset = curr ...
-
绝对不需要使用高级别的BinaryWriter来为流写入一个简单的字节 - 这样做更高效和整洁: Stream outStream = File.Open(filename, FileMode.Open); outStream.Seek(0x6354C, SeekOrigin.Begin); outStream.WriteByte(0xb0); (一般来说,在将BinaryWriter附加到流中之后,BinaryWriter应该控制流,并且在背后改变内容有点脏) There is absolutely n ...
-
但事实证明Sub没有在原始指针上实现。 您可以将指针转换为usize以对其进行数学运算: fn main() { let source = "hello, world"; let a = &source[1..]; let b = &source[5..]; let diff = b.as_ptr() as usize - a.as_ptr() as usize; println!("{}", diff); } but it turns out that Sub ...
-
with open(filename, 'r') as f: for offset in offsets: f.seek(offset) print(f.readline()) 参考文献: 与声明 打开 寻求 的ReadLine with open(filename, 'r') as f: for offset in offsets: f.seek(offset) print(f.readline()) Re ...
-
喜欢这个? file.tell() 返回文件的当前位置,如stdio的ftell()。 http://docs.python.org/library/stdtypes.html#file-objects 不幸的是,由于OP使用stdin而不是文件,tell()不起作用。 但为了给你所需要的东西打造一个包装并不难。 class file_with_pos(object): def __init__(self, fp): self.fp = fp self.pos = ...
-
这样做。 $exp = '/\(sq cm\): \d+/'; echo preg_replace($exp, '', $array); This will do it. $exp = '/\(sq cm\): \d+/'; echo preg_replace($exp, '', $array);
-
另一种方法是计算每条线的字节数 BufferedReader br = null; try { String line; // in my test each character was one byte ArrayList
byteoffset = new ArrayList (); br = new BufferedReader(new FileReader("number ...