SQL indexing on varchar

前端 未结 5 875
伪装坚强ぢ
伪装坚强ぢ 2020-12-25 15:32

I have a table whose columns are varchar(50) and a float. I need to (very quickly) look get the float associated with a given string. Even with ind

5条回答
  •  旧时难觅i
    2020-12-25 15:44

    Keys on VARCHAR columns can be very long which results in less records per page and more depth (more levels in the B-Tree). Longer indexes also increase the cache miss ratio.

    How many strings in average map to each integer?

    If there are relatively few, you can create an index only on integer column and PostgreSQL will do the fine filtering on records:

    CREATE INDEX ix_mytable_assoc ON mytable (assoc);
    
    SELECT  floatval
    FROM    mytable
    WHERE   assoc = givenint
            AND phrase = givenstring
    

    You can also consider creating the index on the string hashes:

    CREATE INDEX ix_mytable_md5 ON mytable (DECODE(MD5(phrase), 'HEX'));
    
    SELECT  floatval
    FROM    mytable
    WHERE   DECODE(MD5(phrase), 'HEX') = DECODE(MD5('givenstring'), 'HEX')
            AND phrase = givenstring -- who knows when do we get a collision?
    

    Each hash is only 16 bytes long, so the index keys will be much shorter while still preserving the selectiveness almost perfectly.

提交回复
热议问题