语句优化

jiao3630 2011-04-02 02:58:26
insert into [CIDWSD].[dbo].[OrganizationalContact]
SELECT [PhoneNumberID]=cast(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(substring([电话1],2,LEN([电话1])),'0','0')
,'1','1')
,'2','2')
,'3','3')
,'4','4')
,'5','5')
,'6','6')
,'7','7')
,'8','8')
,'9','9')
,'-','-')
as varchar(12))
,[OrganizationID]=[ORG_Code]
,[ExtensionNumber]=''
,[Type]=''
,[Department]=''
,[LinkMan]=''
,[Position]=''
,[Memo]=''
,[CreateTime]=GETDATE()
,[Creator]='dbo'
,[EditTime]=GETDATE()
,[Editor]='dbo'
FROM [CIDWSD].[dbo].[OrganizationsContact2011-01-20]
where [ORG_Code] not in(select [OrganizationID] from [CIDWSD].[dbo].[OrganizationalContact])
and [电话1] is not null and [电话1] <>''
and cast(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(substring([电话1],2,LEN([电话1])),'0','0')
,'1','1')
,'2','2')
,'3','3')
,'4','4')
,'5','5')
,'6','6')
,'7','7')
,'8','8')
,'9','9')
,'-','-')
as varchar(12))
not in (select [PhoneNumberID] from [CIDWSD].[dbo].[OrganizationalContact])
and [ORG_Code] in (select [OrganizationID] from [CIDWSD].[dbo].[Organization])


各位老大帮忙看下怎么优化
纠结的很 1个小时才执行12W
总数有100W+
...全文
119 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiao3630 2011-04-02
  • 打赏
  • 举报
回复
改完了 结贴
--小F-- 2011-04-02
  • 打赏
  • 举报
回复
这么多 REPLACE用不了函数的
kingtiy 2011-04-02
  • 打赏
  • 举报
回复
这个数据写入表之前就应该进行清理的.
根本问题在前端.不在数据.优化下前端.
AcHerat 2011-04-02
  • 打赏
  • 举报
回复
where 条件后的字段加索引,where条件筛选时先不要cast那段,其他条件放到where后筛选放入临时表,然后在用临时表插入表中。
jiao3630 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
SQL code

[PhoneNumberID]=cast(
replace(
replace(
replace(
replace(
……
[/Quote]
这样能快不少 语句本身还有么有可优化的
jiao3630 2011-04-02
  • 打赏
  • 举报
回复
快溜 2011-04-02
  • 打赏
  • 举报
回复
这么多replace
AcHerat 2011-04-02
  • 打赏
  • 举报
回复

[PhoneNumberID]=cast(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(substring([电话1],2,LEN([电话1])),'0','0')
,'1','1')
,'2','2')
,'3','3')
,'4','4')
,'5','5')
,'6','6')
,'7','7')
,'8','8')
,'9','9')
,'-','-')
as varchar(12))


把表整个replace后放到一个临时表里,然后用临时表来插入到表中,后边写where条件。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧