12

SQL Server 2012で、行と列のセットのハッシュを生成する方法はありますか?

ハッシュを生成し、それを親レコードに保存したいと思います。更新が行われると、着信ハッシュを親レコードハッシュと比較し、データが変更されたかどうかを確認します。

したがって、このようなものがいいでしょう:

SELECT GENERATEHASH(CONCATENATE(Name, Description, AnotherColumn))
FROM MyChildTable WHERE ParentId = 2 -- subset of data belong to parent record 2

「CONCATENATE」は、列だけでなく、結果セット内の行も連結する集計関数になります。MAXと同様ですが、すべてを文字列連結として返します。

うまくいけば、これはとにかく私が何を意味するのかを理解するのに役立ちます!

私が解決しようとしている根本的な問題は、クライアントのシステムが大量の階層データのインポートを実行することです。ハッシュを使った処理を避けることができれば、かなりの時間を節約できると思います。現時点では、重複データを処理する必要がある場合、SPの実行速度は300%遅くなります。

どうもありがとう

4

4 に答える 4

10
select HashBytes('md5',convert(varbinary(max),(SELECT * FROM MyChildTable WHERE ParentId = 2 FOR XML AUTO)))

ただし、HashBytesは8000バイトのみに制限されています...8000バイトごとにdeMd5を取得する関数を作成できます...

于 2014-03-07T13:34:22.013 に答える
9

CHECKSUM_AGG集計を使用できます。それはその目的のために作られています。

于 2012-08-08T11:16:22.967 に答える
2

単一行ハッシュの場合:

select HASHBYTES('md5', Name + Description + AnotherColumn)
FROM MyChildTable WHERE ParentId = 2

テーブルチェックサムの場合:

select sum(checksum(Name + Description + AnotherColumn)*1.0)
FROM MyChildTable WHERE ParentId = 2
于 2012-08-08T10:55:15.957 に答える
1

別のアプローチ:

-- compute a single hash value for all rows of a table
begin

    set nocount on;

    -- init hash variable
    declare @tblhash varchar(40);
    set @tblhash = 'start';

    -- compute a single hash value
    select @tblhash = sys.fn_varbintohexsubstring(0, hashbytes('sha1',(convert(varbinary(max),@tblhash+
    (select sys.fn_varbintohexsubstring(0,hashbytes('sha1',(convert(varbinary(max),
    -- replace 'select *' if you want only specific columns to be included in the hash calculation
    -- [target table] is the name of the table to calc the hash from
    -- [row_id] is the primary key column within the target table
    -- modify those in the next lines to suit your needs:
    (select * from [target_table] obj2 where obj2.[row_id]=obj1.[row_id] for xml raw)
    ))),1,0))
    ))),1,0)
    from [target_table] obj1;

    set nocount off;

    -- return result
    select @tblhash as hashvalue;

end;
于 2013-10-10T12:40:50.043 に答える