简体   繁体   中英

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. 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.

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. I have thousands of records in the database and when I do a query on a single hash it takes 5 seconds.

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?

Thanks

Try to add key index on FileHash column.Not unique, simple key index:

ALTER TABLE fileinfo
ADD INDEX FileHash (FileHash)

Make EXPLAIN SELECT ... before this alter and just after it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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