在数据库中存储哈希ID并获取它,还是在代码本身中创建哈希ID并使用它? [英] Storing hashed ID in DB and fetching it or creating hashed ID in code itself and use it?

查看:201
本文介绍了在数据库中存储哈希ID并获取它,还是在代码本身中创建哈希ID并使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种处理散列ID的方法,但需要您就哪种方法优于另一种方法以及为什么提出意见?我对逻辑感兴趣,而不是对哈希或表数据类型等使用的技术感兴趣!

I have two way of doing handling hashed IDs but need your opinions on which one would better over another and why? I'm only interested in the logic rather than what technology I used for hashing or table data type etc!

我将生成一个URL以处理删除产品的过程,并且该URL将像这样(最后一段是对(SHA1)product.id进行哈希处理):

I'm going to generate an URL to handle deleting a product and the URL will be like this (last segment is hashed (SHA1) product.id):

...../delete/product/123c45c789ds654dedkjfghr87frd

场景1: 将哈希ID存储在表中,并像这样使用它:

SCENARIO 1: Store hashed ID in table and use it like this:

>查询:

DELETE ...... WHERE hash_id = '$url_hash_id'

>表:

products
id - INT
hash_id - VARCHAR(40)

场景2: 不要将散列ID存储在表中,而是在每次创建URL并每次使用时都将其生成:

SCENARIO 2: Do not store hashed ID in table but generate it everytime when you create the URL and use it like this:

>查询:

DELETE ...... WHERE SHA1(id) = '$url_hash_id'

>表:

products
id - INT

我能想到的是:

  1. 场景1-表变大(可能是整个数据库,因为我可能最终会为所有表实现同一列),但是散列过程只运行一次.
  2. 方案2-使用较少的表空间,但使用mySql SHA1()函数和 每次需要时都会调用php SHA1()函数.
  1. Scenario 1 - Table gets bigger (potentially whole DB because I might end up implement same column for all the tables) but hashing process is run once.
  2. Scenario 2 - Less table space used but mySql SHA1() function and php SHA1() functions are called everytime we need it.

推荐答案

我选择方案1是因为WHERE SHA1(id) = '$url_hash_id'将不使用mysql索引.使用大量记录,表扫描将使您丧命,而不仅仅是存储hash_id和该列的索引的额外开销.

I'd go with scenario 1 simply because WHERE SHA1(id) = '$url_hash_id' will not use the mysql index. With a large number of records the table scanning will kill you more than the extra overhead of storing the hash_id and an index for that column.

这篇关于在数据库中存储哈希ID并获取它,还是在代码本身中创建哈希ID并使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆