我刚刚看到这个函数定义:
create function dbo.f (@a int, @b int)
returns integer
as
begin
return case when
not exists (Select * from t1 where t1.col1 = @a)
AND @b > 0
then 1 else 0 end
end
GO
Run Code Online (Sandbox Code Playgroud)
看到一个不存在我想注意全表扫描并尝试改进它
create function dbo.f (@a int, @b int)
returns integer
as
begin
return case when
exists (Select * from t1 where t1.col1 = @a)
OR @b > 0
then 0 else 1 end
end
GO
Run Code Online (Sandbox Code Playgroud)
我的感觉是,这种转换可以由优化器来完成。看起来很简单,但我如何确定他是否这样做呢?
对伊戈尔的回答发表评论:( 由于马茨的评论而修复了比较)
这给我带来以下启发:
create …Run Code Online (Sandbox Code Playgroud) 我有以下 SQL Server 查询
select
(select top 1 b2 from BB b where b.b1 = a.a1 order by b2) calc,
a1,
a2
from AA a
where a2 = 2;
Run Code Online (Sandbox Code Playgroud)
我可以使用解析函数重写
select
(select b2 from
(select
row_number() over (order by b2) lfd,
b2 from BB b where b.b1 = a.a1
) as t where lfd = 1
) calc,
a1,
a2
from AA a
where a2 = 2;
Run Code Online (Sandbox Code Playgroud)
但是当我将其转换为 oracle 时
create table AA ( a1 NUMBER(10), a2 NUMBER(10) );
insert …Run Code Online (Sandbox Code Playgroud) 当我创建这样的表时
create table char_test(
item varchar(10) collate SQL_Latin1_General_CP1_CI_AS
)
go
Run Code Online (Sandbox Code Playgroud)
我可以存储包含?
insert into char_test values ('?');
Run Code Online (Sandbox Code Playgroud)
当我做一个
select item from char_test;
Run Code Online (Sandbox Code Playgroud)
我得到
item
----------
=
Run Code Online (Sandbox Code Playgroud)
但是使用
select replace(item, '?', '>=') from char_test;
Run Code Online (Sandbox Code Playgroud)
给
-----------------
>=
Run Code Online (Sandbox Code Playgroud)
所以数据库知道这个字符。如何将字符串转换为显示此字符的 nvarchar?
编辑:
这里的重要事实是,这里的 SQL Server 将一些不同的字符映射到同一个字符,而不会抛出错误或警告。
我敢说这是非常糟糕的设计。
当它停止执行长时间运行的脚本时。脚本终止需要的时间比脚本运行的时间还要长。即我必须杀死蟾蜍。
第二次编辑:
刚刚发现这是Toad的一个特性而不是Oracle的
在 SQL 开发人员中,您可以停止执行长时间运行的查询,并且类似于 SQL Server Management Studio,您可以立即修改您的查询。
架构中的表旨在以只读方式使用,并且每三个月仅更新一次。
我的问题是指性能维护(备份/恢复、导出/导入)
临时表空间怎么样,在这种情况下使用不同的表空间更好吗?
给定
create table rating (
ID int identity(1,1) primary key,
PersonID int,
Ratingdate date,
rating varchar(2)
);
insert into rating values ( 1, '2010-08-04' , 'A3');
insert into rating values ( 1, '2010-08-14' , 'A1');
insert into rating values ( 2, '2010-08-04' , 'G2');
insert into rating values ( 2, '2010-08-14' , 'G1');
insert into rating values ( 3, '2010-08-04' , 'G1');
insert into rating values ( 3, '2010-08-10' , 'G1');
insert into rating values ( 3, '2010-08-14' , 'G3'); …Run Code Online (Sandbox Code Playgroud) 我的任务是从生产数据库导出一个大表的内容,并将数据导入远程位置的数据库中。
该表有大约 45,000,000 行。在数据库中使用大约 4 GB 空间。
它的 10 列的类型为 int、datetime 和 varchar(n) <= 255,但一些 varchar 字段包含换行符和批量复制使用的常用字段分隔符。
由于行数,我想导出到 Excel 是没有选择的。使用 SSIS 导出到 Flatfile 是可能的(大约需要 30 分钟),但重新导入不是自动的,因为有些字段会被吐出。
我现在的想法是
我假设插入到具有简单恢复模型的数据库中只会在新数据库上生成最少的日志记录,这种方式是导出数据最快的方式之一,对生产数据库的影响也是最小的。
我知道表格
Select * into new_table from old_table
Run Code Online (Sandbox Code Playgroud)
陈述。Bur 最近我在这里和这里找到了 2 个帖子,它们使用它
SELECT * FROM NhlPlayer
INSERT INTO PlayerBackups
Run Code Online (Sandbox Code Playgroud)
我在msdn 中什么也没找到,这是一些新语法还是只是一个错误?我猜后者是因为
INSERT INTO PlayerBackups
SELECT * FROM NhlPlayer
Run Code Online (Sandbox Code Playgroud)
会工作得很好。
我想将它应用于过程定义。
我的基本想法是将 nvarchar(max) 类型的字符串拆分为 nvachar(4000) 块并连接 HashBytes('MD5',chunk) 的结果。
我无法想象这还没有完成。
sql-server ×5
oracle ×4
collation ×1
dynamic-sql ×1
export ×1
performance ×1
select ×1
subquery ×1
tablespaces ×1