mysql - 我应该如何构建一个数据库来存储大量SHA1数据

标签 mysql

我在构建数据库来存储大量 SHA1 数据并有效返回结果时遇到问题。

我承认 SQL 不是我最擅长的技能,但作为练习,我尝试使用 https://haveibeenpwned.com/Passwords 中的数据。很快就会返回结果

这是我的数据:

mysql> describe pwnd;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| pwndpass | binary(20)       | NO   |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

mysql> select id, hex(pwndpass) from pwnd order by id desc limit 10;
+-----------+------------------------------------------+
| id        | hex(pwndpass)                            |
+-----------+------------------------------------------+
| 306259512 | FFFFFFFEE791CBAC0F6305CAF0CEE06BBE131160 |
| 306259511 | FFFFFFF8A0382AA9C8D9536EFBA77F261815334D |
| 306259510 | FFFFFFF1A63ACC70BEA924C5DBABEE4B9B18C82D |
| 306259509 | FFFFFFE3C3C05FCB0B211FD0C23404F75E397E8F |
| 306259508 | FFFFFFD691D669D3364161E05538A6E81E80B7A3 |
| 306259507 | FFFFFFCC6BD39537AB7398B59CEC917C66A496EB |
| 306259506 | FFFFFFBFAD0B653BDAC698485C6D105F3C3682B2 |
| 306259505 | FFFFFFBBFC923A29A3B4931B63684CAAE48EAC4F |
| 306259504 | FFFFFFB58E389A0FB9A27D153798956187B1B786 |
| 306259503 | FFFFFFB54953F45EA030FF13619B930C96A9C0E3 |
+-----------+------------------------------------------+
10 rows in set (0.01 sec)

我的问题涉及快速查找条目,因为目前需要超过 6 分钟

mysql> select hex(pwndpass) from pwnd where hex(pwndpass) = '0000000A1D4B746FAA3FD526FF6D5BC8052FDB38';
+------------------------------------------+
| hex(pwndpass)                            |
+------------------------------------------+
| 0000000A1D4B746FAA3FD526FF6D5BC8052FDB38 |
+------------------------------------------+
1 row in set (6 min 31.82 sec)

我的数据类型正确吗?我搜索存储 sha1 数据,建议使用 Binary(20) 字段,但不确定如何优化它以搜索数据。

我的 MySQL 安装是一个干净的交 key 虚拟机 https://www.turnkeylinux.org/mysql除了为虚拟机提供更多磁盘空间之外,我没有调整任何设置

最佳答案

两个最明显的提示是:

  • 在列上创建索引。
  • 不要在每次搜索时将每一行都转换为十六进制:

    select hex(pwndpass)
    from pwnd
    where hex(pwndpass) = '0000000A1D4B746FAA3FD526FF6D5BC8052FDB38';
    --    ^^^ This is forcing MySQL to convert every hash stored from binary to hexadecimal
    --        so it can determine whether there's a match
    

事实上,您甚至根本不需要十六进制,仅用于显示目的:

select id, hex(pwndpass) -- This is fine, will just convert matching rows
from pwnd
where pwndpass = ?

... 其中 ? 是占位符,在您的客户端语言中,对应于二进制字符串。

如果您需要直接在命令行中运行查询,您还可以使用 hexadecimal literal :

select id, hex(pwndpass) -- This is fine, will just convert matching rows
from pwnd
where pwndpass = 0x0000000A1D4B746FAA3FD526FF6D5BC8052FDB38

关于mysql - 我应该如何构建一个数据库来存储大量SHA1数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47691828/

相关文章:

php - 在 JOIN MySQL 中选择除元素之外的所有元素

php - 我试图用 PHP、MySQL、jQuery 和 css 处理的乱七八糟的事

mysql - 在单个sql语句中更新多个表

php - 是否有一个好的解决方案来使用数据库的 session 集保存处理程序来处理 session 不活动?

php - jQuery 调用 PHP 文件从 mysql 数据库中获取数据?

mysql - 当某物进入另一张 table 时填充一张 table

php - 如何在 PHP 中显示和插入 Angular 数组数据?

php - 使用 Jquery/Bootstrap 删除标签标签/添加输入文本框

sql - 如何确保 mysql 表中没有重复项?

mysql - MySQL的字符串比较规则是什么?