简体   繁体   English

在MySQL数据库中查询64字节(字符)哈希值需要花费几秒钟的时间。 怎么提高?

[英]Query 64 byte (char) hash in MySQL database takes several seconds. How to improve?

I have a database table (MySQL 5) which stores file hashes and filenames. 我有一个数据库表(MySQL 5),用于存储文件哈希和文件名。 I am indexing thousands of files. 我正在索引数千个文件。 I don't have a primary key set because I want to index all files even if they are duplicates but in different locations. 我没有主键集,因为我想为所有文件建立索引,即使它们是重复的文件也位于不同的位置。 So if I have 2 files X.bin and Y.bin, even if the file hash is the same I want to insert them both into my table because they have different file names. 因此,如果我有2个文件X.bin和Y.bin,即使文件哈希相同,我也想将它们都插入表中,因为它们具有不同的文件名。

The only time I don't want to insert a duplicate is when the filename and file hash already exist in the system. 我唯一不想插入重复项的时间是系统中已经存在文件名和文件哈希。 For that I need to do a query on the file hash. 为此,我需要对文件哈希进行查询。 This is where it takes a long time. 这是需要很长时间的地方。 I am using SHA256 hash which is 64 characters in length. 我正在使用长度为64个字符的SHA256哈希。 I have thousands of records in the database and when I do a query on a single hash it takes 5 seconds. 我在数据库中有成千上万的记录,当我对单个哈希进行查询时,需要5秒钟。

My query is: 我的查询是:

SELECT FileName FROM fileinfo WHERE FileHash='qazwsxedcrfvtgbyhnujm'

Other than using a different file hash like MD5 which is 32 chars in length, is there anything else I can do to speed up the query? 除了使用长度为32个字符的不同文件哈希(例如MD5)之外,还有什么我可以做的以加快查询速度吗?

Thanks 谢谢

Try to add key index on FileHash column.Not unique, simple key index: 尝试在FileHash列上添加键索引。不是唯一的简单键索引:

ALTER TABLE fileinfo
ADD INDEX FileHash (FileHash)

Make EXPLAIN SELECT ... before this alter and just after it. 在更改之前和之后进行EXPLAIN SELECT...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM