postgresql - SQL - SELECT by index which is a long hash -
let's assume have long unique hashes (like 250-character long). store them primary key
. lookup row slower if primary key based on int
?
i've made simple benchmark preparing tables:
create table test_int(id integer primary key); create table test_text(id text primary key); insert test_int select * generate_series(1,1000000); insert test_text select md5(random()::text)||md5(random()::text)||md5(random()::text)||md5(random()::text)||md5(random()::text)||md5(random()::text)||md5(random()::text)||md5(random()::text) generate_series(1,1000000); analyze; explain analyze select * test_int id=<somevalue>; index scan using test_int_pkey on test_int (cost=0.42..8.44 rows=1 width=4) (actual time=0.023..0.024 rows=1 loops=1) index cond: (id = 58921) heap fetches: 1 planning time: 0.129 ms execution time: 0.058 ms explain analyze select * test_text id=<somevalue>; index scan using test_text_pkey on test_text (cost=0.68..8.69 rows=1 width=260) (actual time=0.105..0.106 rows=1 loops=1) index cond: (id = '3e74346a6a060a208b3916c9b289d1a5020fa9ff3175fdb78022001a2e5f0857a70bddfbd0f148b11ec8928edd9ce008835c9b8624f6afb7b05e2d05ad9cc049418b046ea5e0d4814ffc34fc7fad476996c634353189140702148ba6c1f8fb055ca8ff18747bb3e62f3ca7189fb5165dff850c699e6 (...) heap fetches: 1 planning time: 0.122 ms execution time: 0.140 ms
on machine lookup numeric id approx twice fast in comparison text id;
what's more interesting pk index size:
select indexname, pg_size_pretty(pg_relation_size(indexname::text)) pg_indexes tablename in ('test_int', 'test_text'); "test_int_pkey" "21 mb" "test_text_pkey" "420 mb"
so 250 chars long texts slower integer ids.
Comments
Post a Comment