» 首页 » 电脑_数码 » 编程 » 请高手帮忙优化一下这条SQL语句

请高手帮忙优化一下这条SQL语句

有两张表,都是一连串的数字,现在想查出这两张表,A表中数字前面有包含B表中的数字的,就挑出来到一张新的表.

A表样式:
10629625
8826512201248
10325335752
10629625123
103253353752
……
B表样式
10629
103253
88265122
……

我的语句是这样的select a.col001 into C from A a,B where left(a.col002,len(B.col001))=B.col001
,不过数据比较大,执行起来很慢,有没有高手帮一下要怎样优化这条语句,谢谢了


rand()不大好用,用这个:
0-1之间类似于

program test
implicit none
real :: x
call random_seed()! 系统根据日期和时间随机地提供种子"
call random_number(x)
print *,x
end program


另外,在一个程序中, "call random_seed () 这一句是不能重复的,只能用一次。
所以它不能放到循环里面去,另外,如果一个子程序会被调用多次的话,也不能放到子程序里面。

==============
如果你非得用rand()的话,看看这个帖子吧:
http://bbs.pfan.cn/post-276179.html

用存储过程。。。。
写一个函数.
把A表,循环用函数对比就比你哪个快多了!
use tempdb
create table a(a bigint)
create table b(b bigint)

insert into a
select 10629625 union all
select 8826512201248 union all
select 10325335752 union all
select 10629625123 union all
select 103253353752

insert into b
select 10629 union all
select 103253 union all
select 88265122

select * from a
select * from b

select distinct cast(b.b as varchar) into c from a,b where cast(a.a as varchar) like (cast(b.b as varchar) + '%')
select * from c
要用到储存过程和游标(有小小难度)
if exists(select * from sysobjects where name='aa' and type='p')
drop proc aa
go
create proc aa
as
declare @s varchar(20)
declare t cursor for
select n from b
open t
fetch next from t
into @s
--set @s=rtrim(@s)+'%'

select * from a where N like rtrim(@s)+'%'
while @@fetch_status=0
begin
fetch next from t
into @s
select * from a where N like rtrim(@s)+'%'
end
close t
deallocate t
go

 相关问题
·请高手帮忙优化一下这条SQL语句
·在fortran中rand()的用法是怎样的?麻烦举个例子
·一、以下程序包含三个函数:sort,print,printfil,按照要...
·求一个批处理~!!
·求简单的php通用后台程序?
·在Textl中输入的任何字符,立即显示在Text2中。
·编写一类似QQ群聊的多人聊天程序
·请教这段是什么意思.
·关于学习asp.net
·pascal题目:机器人
·什么是动漫知识与动漫语言?
·输入2个字符串,输出较大的一个
·急!!!根据用户的ID调数据库中的内容显示
·判断字符个数及读取字段程序
·PHP中的问题

 《请高手帮忙优化一下这条SQL语句》答案收集时间:2008-06-14 14:28:19



©2007 电脑技术问答录