Advertisement
Guest User

Untitled

a guest
Jan 28th, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import time,psycopg2,random
  2.  
  3. NROWS = 1000000
  4. NATTRS = 32             # number of attributes per row
  5.  
  6. attr_columns = ["a%02d" % n for n in range(NATTRS) ]
  7.  
  8. def query(s,*args,**kwargs):
  9.     print(s)
  10.     return c.execute( s,*args,**kwargs )
  11.  
  12. def bench( f, *args, **kwargs ):
  13.     t = time.time()
  14.     try:
  15.         return f( *args, **kwargs )
  16.     finally:
  17.         dt = time.time()-t
  18.         c = db.cursor()
  19.         query( "SELECT pg_relation_size( 'testins' )")
  20.         size, = c.fetchone()
  21.         query( "SELECT count(*) FROM testins")
  22.         rows, = c.fetchone()
  23.         print("%s: %.03fs %d rows %10.03f kB" % (f.__name__, dt, rows, size/1024))
  24.  
  25. def drop():
  26.     query( "DROP TABLE profiles_btree" );
  27.     query( "DROP TABLE profiles_gist" );
  28.     query( "DROP TABLE profiles_bloom" );
  29.     query( "DROP TABLE profiles_gin" );
  30.  
  31. def build():
  32.     query( "CREATE UNLOGGED TABLE profiles_btree ( id INTEGER NOT NULL,"
  33.         + ",".join( "%s INTEGER NULL" % c for c in attr_columns )
  34.         + " );" )
  35.  
  36.     query( "CREATE UNLOGGED TABLE profiles_bloom ( id INTEGER NOT NULL,"
  37.         + ",".join( "%s INTEGER NULL" % c for c in attr_columns )
  38.         + " );" )
  39.  
  40.     query( "CREATE UNLOGGED TABLE profiles_gist ( id INTEGER NOT NULL, attrs INTEGER[] NOT NULL )" );
  41.     query( "CREATE UNLOGGED TABLE profiles_gin  ( id INTEGER NOT NULL, attrs INTEGER[] NOT NULL )" );
  42.  
  43. def fill():
  44.     print("Generate...")
  45.     insert_attrs = []
  46.     insert_arrays = []
  47.     for nrow in range( NROWS ):
  48.         attrs = [ None ] * NATTRS
  49.         array = []
  50.  
  51.         # Make 5 columns with 50% True, 50% NULL
  52.         for pos in range( 5 ):
  53.             if random.randint(0,1):
  54.                 attrs[pos] = 1
  55.                 array.append( pos )
  56.  
  57.         # Put 5 True values at random in the other columns
  58.         for n in range( 5 ):
  59.             pos = random.randint( 5, NATTRS-1 )
  60.             attrs[ pos ] = 1
  61.             array.append( pos )
  62.  
  63.         insert_attrs.append( [nrow] + attrs )
  64.         insert_arrays.append( [nrow, array] )
  65.  
  66.     print("INSERT")
  67.     c.executemany( "INSERT INTO profiles_btree VALUES ("+",".join( "%s" for n in range(NATTRS+1))+")", insert_attrs )
  68.     query( "INSERT INTO profiles_bloom SELECT * FROM profiles_btree")
  69.  
  70.     print("INSERT")
  71.     c.executemany( "INSERT INTO profiles_gist VALUES (%s,%s)", insert_arrays )
  72.     query( "INSERT INTO profiles_gin   SELECT * FROM profiles_gist")
  73.  
  74.     for table in "btree","gist","bloom","gin":
  75.         query( "ALTER TABLE profiles_%s ADD PRIMARY KEY(id)" % table )
  76.  
  77.  
  78. db = psycopg2.connect("dbname=test")
  79. c = db.cursor()
  80. if 1:
  81.     try:
  82.         drop()
  83.         query("COMMIT")
  84.     except Exception as e:
  85.         print(e)
  86.         query("ROLLBACK")
  87.  
  88.     try:
  89.         build()
  90.         query("COMMIT")
  91.     except Exception as e:
  92.         print(e)
  93.         query("ROLLBACK")
  94.  
  95.     fill()
  96.     query("COMMIT")
  97.  
  98.     query("CREATE INDEX ON profiles_bloom USING bloom(%s)" % ",".join(attr_columns) )
  99.     query("CREATE INDEX ON profiles_gist  USING GIST (attrs gist__int_ops)" )
  100.     query("CREATE INDEX ON profiles_gist  USING GIN  (attrs gin__int_ops)" )
  101.     for col in attr_columns:
  102.         query("CREATE INDEX ON profiles_btree(%s) WHERE %s IS NOT NULL"%(col,col))
  103.     query("COMMIT")
  104.     query("VACUUM ANALYZE")
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement