How long are these text lines? If they are very long, you can improve performance by storing the hash of the strings (along with the original strings).
CREATE TABLE strings_db ( id PRIMARY KEY INT, text TEXT, hash TEXT );
Your hash column can store MD5, CRC32, or any other hash algorithm you choose. And it must be indexed.
Then change your request to something like:
SELECT id FROM strings_db WHERE hash=calculate_hash(?)
If the average size of your text fields is large enough than the size of your hashes, performing a search in a shorter field will help with disk I / O. It also means extra CPU overhead on insertion and selection, hash calculation, and additional disk space for storing the hash. Therefore, all these factors must be taken into account.
PS Always use prepared statements to avoid SQL injection attacks!
source share