LoginSignup
0
0

More than 3 years have passed since last update.

クローリングした内容の高速重複判断方法

Last updated at Posted at 2020-03-04

目的

クローリングしたデータがデータベースにすでに存在しているかどうか知りたい際に、
対照(select id from database where content like '%クローリングしたデータ%')で重複判断するのは効率が悪いため、
もっと効率が良い判断方法です。

流れ

1.クローリングされたデータをハッシュ(hash)化します。
 そのハッシュは数字と英文字で組み合わせた文字列です。SHA-512バリエーションを使い、64ビットに拡張されています

2.ハッシュ文字列の列をユニークインディクスします。

3.クローリングした内容をハッシュする
 mysql関数を使います
 SHA2('My secret passphrase',512)

4.insert ignore into を使ってデータベースに追加します
 insert ignore into (content,content_hash) values ('クローリングしたデータ',SHA2('クローリングしたデータ',512))

5.content_hashはユニークインディクスなので、重複があれば、エラーと判断し、重複したデータは無視(insert ignore into)で処理します。

スピードが速い、誤差が少ないことはメリットです。

参考

https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html#function_sha2
https://dev.mysql.com/doc/refman/8.0/en/insert.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0