How do I calculate a good hash code for a list of strings?

后端 未结 11 1591
我寻月下人不归
我寻月下人不归 2020-12-01 02:34

Background:

  • I have a short list of strings.
  • The number of strings is not always the same, but are nearly always of the order of a “handful”
  • I
11条回答
  •  臣服心动
    2020-12-01 03:30

    A SQL-based solution could be based on the checksum and checksum_agg functions. If I'm following it right, you have something like:

    MyTable
      MyTableId
      HashCode
    
    MyChildTable
      MyTableId  (foreign key into MyTable)
      String
    

    with the various strings for a given item (MyTableId) stored in MyChildTable. To calculate and store a checksum reflecting these (never-to-be-changed) strings, something like this should work:

    UPDATE MyTable
     set HashCode = checksum_agg(checksum(string))
     from MyTable mt
      inner join MyChildTable ct
       on ct.MyTableId = mt.MyTableId
     where mt.MyTableId = @OnlyForThisOne
    

    I believe this is order-independant, so strings "The quick brown" would produce the same checksum as "brown The quick".

提交回复
热议问题