Skip to content

Instantly share code, notes, and snippets.

@kbarber
Created November 6, 2013 16:52
Show Gist options
  • Select an option

  • Save kbarber/7339707 to your computer and use it in GitHub Desktop.

Select an option

Save kbarber/7339707 to your computer and use it in GitHub Desktop.
Loading tool:
#!/usr/bin/env ruby
require 'pg'
require 'digest/sha1'
# Output a table of current connections to the DB
conn = PG.connect(dbname: 'indexloadtesting', host: 'localhost')
1000000.times do
string = (0...60).map { (65 + rand(26)).chr }.join
hash = Digest::SHA1.hexdigest(string)
conn.exec("insert into hashtable values ('#{hash}')")
end
On a table with only 1 column and no indexes. Column type was a varchar(40):
# time ./indexes_last.rb
./indexes_last.rb 44.00s user 21.45s system 42% cpu 2:34.20 total
On a table with 1 column and 1 index (again, column type is varchar(40)):
# time ./indexes_last.rb
./indexes_last.rb 43.52s user 21.71s system 31% cpu 3:24.41 total
Which is no real surprise, index first takes longer. But this proves nothing on its own.
Indexing the varchar(40) column on the first unindexed table:
indexloadtesting=# create index foo on hashtable (hash);
CREATE INDEX
Time: 32503.838 ms
So wall clock time ... indexes up front is roughly 3:24, indexes last is roughtly 3:06.
As far as comparing them in size:
indexloadtesting=# \di+
List of relations
Schema | Name | Type | Owner | Table | Size | Description
--------+-----------------------+-------+-------+--------------------+-------+-------------
public | foo | index | ken | hashtable | 65 MB |
public | hashtableindexes_pkey | index | ken | hashtablewithindex | 85 MB |
(2 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment