表a
  a1 a2 a3 a4 a5 sort 
  1  2  3  4  5  1*2*3*4*5
  5  2  3  4  1  1*2*3*4*5
  5  3  4  2  1  1*2*3*4*5
  1  5  6  7  8  1*5*6*7*8
  1  5  6  8  7  1*5*6*7*8
  8  7  6  5  1  1*5*6*7*8就是这样的表,原来sort字段是空的
我现在想update sort成上表所示
a1,a2,a3,a4,a5  为1-20的任意数字,并在同一条记录中不能重复。
怎样更新才能快呢?
我现在是用CLR的函数做的,好慢
180000条记录,要4s
如何才能加快速度呢?
sql 语句如下:
   UPDATE a SET sort = dbo.fn_Sort(a1,a2,a3,a4,a5,'*')

解决方案 »

  1.   

    Sort的顺序要求从小到大排列?
      

  2.   

    if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb
    (
    a1 int,
    a2 int,
    a3 int,
    a4 int,
    a5 int,
    sort varchar(100)
    )insert into tb(a1,a2,a3,a4,a5,sort) values(1,  2,  3,  4,  5,null)
    insert into tb(a1,a2,a3,a4,a5,sort) values(5,  2,  3,  4,  1,null)
    insert into tb(a1,a2,a3,a4,a5,sort) values(5,  3,  4,  2,  1,null)
    insert into tb(a1,a2,a3,a4,a5,sort) values(1,  5,  6,  7,  8,null)
    insert into tb(a1,a2,a3,a4,a5,sort) values(1,  5,  6,  8,  7,null)
    insert into tb(a1,a2,a3,a4,a5,sort) values(8,  7,  6,  5,  1,null)
    goselect id=identity(int,1,1) , * into test from tbselect * into test2 from (
    select id,a1 as a from test
    union all
    select id,a2 as a from test
    union all
    select id,a3 as a from test
    union all
    select id,a4 as a from test
    union all
    select id,a5 as a from test) t
    order by id, a
    go
    --创建一个合并的函数
    if object_id('pubs..f_hb') is not null
       drop function f_hb
    go
    create function f_hb(@id int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + '*' + cast(a as varchar) from test2 where id = @id
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select a.a1,a.a2,a.a3,a.a4,a.a5 , b.sort from test a,(
    select distinct id ,dbo.f_hb(id) as sort from test2) b
    where a.id = b.iddrop table tb
    drop table test
    drop table test2a1          a2          a3          a4          a5          sort                                                                                                                                                                                                                                                             
    ----------- ----------- ----------- ----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    1           2           3           4           5           1*2*3*4*5
    5           2           3           4           1           1*2*3*4*5
    5           3           4           2           1           1*2*3*4*5
    1           5           6           7           8           1*5*6*7*8
    1           5           6           8           7           1*5*6*7*8
    8           7           6           5           1           1*5*6*7*8(所影响的行数为 6 行)
      

  3.   

    对,从小到大排序。
    sql server2005,不一定用函数做
    只要速度快就行。
    还有一个id的主键
      

  4.   

    a1          a2          a3          a4          a5          sort      
    ----------- ----------- ----------- ----------- ----------- ----------
    1           2           3           4           5           1*2*3*4*5
    5           2           3           4           1           1*2*3*4*5
    5           3           4           2           1           1*2*3*4*5
    1           5           6           7           8           1*5*6*7*8
    1           5           6           8           7           1*5*6*7*8
    8           7           6           5           1           1*5*6*7*8(所影响的行数为 6 行)
      

  5.   

    但是,我要做几次啊,差不多7次吧,这样就要30s了
    这样肯定不行的。
    老龟你的这个table test2 ,应该速度很慢的.
    你想想180000条记录,180000*5,就120万条了。
    如果再用函数查询.
      

  6.   

    create table ta( a1 int, a2 int , a3 int, a4 int,a5 int)
    insert ta
    select   1,  2,  3,  4,  5  union all
    select   5,  2,  3,  4,  1  union all
    select   5,  3,  4,  2,  1  union all
    select   1,  5,  6,  7,  8   union all
    select   1,  5,  6,  8,  7   union all
    select   8,  7,  6,  5,  1  alter table ta add 新列 varchar(20) null--新增列
    declare @i int
    set @i=0
    update ta
    set 新列=@i,@i=@i+1
    --生成递增值--以下生成新表select * into tb
    from (select 新列,a1 from ta
    union all
    select 新列,a2 from ta
    union all
    select 新列,a3 from ta
    union all 
    select 新列,a4 from ta
    union 
    select 新列,a5 from ta)a
    order by 新列,a1(select *,记录=(select count(*)  from tb  where 新列=b.新列 and a1!>b.a1) into test_ta from tb b)--新表组合
    --创建函数
    create function test_fun(@新列 int)
    returns varchar(1000)
    as
    begin
    declare @sql varchar(1000)
    set @sql=''
    select @sql=@sql+cast(a1 as varchar)+'*' from test_ta where 新列=@新列
    select @sql=left(@sql,len(@sql)-1)
    return @sql
    end
    select 新列,合成=dbo.test_fun(新列) from test_ta group by 新列--查看效果
    --以下为更新
    update ta 
    set 新列=dbo.test_fun(test_ta.新列)
    from test_ta where test_ta.新列=ta.新列select * from ta--查看结果
    a1          a2          a3          a4          a5          新列                   
    ----------- ----------- ----------- ----------- ----------- -------------------- 
    1           2           3           4           5           1*2*3*4*5
    5           2           3           4           1           1*2*3*4*5
    5           3           4           2           1           1*2*3*4*5
    1           5           6           7           8           1*5*6*7*8
    1           5           6           8           7           1*5*6*7*8
    8           7           6           5           1           1*5*6*7*8(所影响的行数为 6 行)--drop table test_ta,tb  删除新表的
    --drop function test_fun 删更新函数--drop table ta 删测试原表
      

  7.   

    最好楼主测试下所有算法的速度我的算法没用函数和临时表(为了测试,原始表用了表变量@b,希望楼主自己能换回去)declare @b table (
    id int IDENTITY(1,1),
    a1 int,
    a2 int,
    a3 int,
    a4 int,
    a5 int,
    sort varchar(30)
    )
    insert @b(a1,a2,a3,a4,a5) select
      1,  2,  3,  4,  5
    union all select
      5,  2,  3,  4,  1
    union all select
      5,  3,  4,  2,  1
    union all select
      1,  5,  6,  7,  8
    union all select
      1,  5,  6,  8,  7
    union all select
      8,  7,  6,  5,  1declare @t table (
    id int,
    a int
    )insert @t
    select id,a1 from @b
    union all
    select id,a2 from @b
    union all
    select id,a3 from @b
    union all
    select id,a4 from @b
    union all
    select id,a5 from @bwhile exists (select 1 from @t)
    begin
    update b
    set sort=isnull(sort+'*','')+cast(t.a as varchar)
    from @b b,@t t
    where b.id=t.id
    and not exists (
    select 1 from @t where id=t.id and a<t.a
    )delete t
    from @t t
    where not exists (
    select 1 from @t where id=t.id and a<t.a
    )end
    select * from @b
      

  8.   

    to:pengda1i(冒牌大力 V0.3) 
    你的算法要4分多钟
    18万条记录
      

  9.   

    不会重复的,一行,记录的a1,a2,a3,a4,a5 肯定不一样的
      

  10.   

    18W条记录的磁盘I/O开销也不小吧,一次性更新这么多数据,4s已经不慢了;如果希望提高这个操作的速度,恐怕还得从磁盘的I/O效率上做些文章。
      

  11.   

    http://blog.csdn.net/superdullwolf/archive/2007/01/16/1484119.aspx