Advertisement
Guest User

Untitled

a guest
Feb 4th, 2024
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. import time,psycopg2,random
  2.  
  3. NROWS = 10000000
  4.  
  5. def query(s,*args,**kwargs):
  6.     print(s)
  7.     c.execute( s,*args,**kwargs )
  8.     print( "%d rows", c.rowcount )
  9.  
  10. def bench( n, sql, args, name=None ):
  11.     t = time.time()
  12.     try:
  13.         for i in range(n):
  14.             c.execute( sql, args )
  15.     finally:
  16.         dt = time.time()-t
  17.         print("%8.03fms %6d rows %10s %s" % (1e3*dt/n, c.rowcount, name, sql[:40]))
  18.  
  19.     # c.execute( "EXPLAIN "+sql, args )
  20.     # for row in c:
  21.     #     print( row[0][:60] )
  22. def drop():
  23.     try:
  24.         query( "DROP TABLE test_array" );
  25.         query("COMMIT")
  26.     except Exception as e:
  27.         print(e)
  28.         query("ROLLBACK")
  29.  
  30. def build():
  31.     try:
  32.         query( "CREATE UNLOGGED TABLE test_array( id INTEGER NOT NULL, s TEXT )" )
  33.         query("COMMIT")
  34.     except Exception as e:
  35.         print(e)
  36.         query("ROLLBACK")
  37.  
  38. def fill():
  39.     try:
  40.         query( "INSERT INTO test_array SELECT n,n FROM generate_series(1,%s) n", (NROWS,) )
  41.         query("COMMIT")
  42.     except Exception as e:
  43.         print(e)
  44.         query("ROLLBACK")
  45.  
  46. db = psycopg2.connect("dbname=test")
  47. c = db.cursor()
  48. if 0:
  49.     drop()
  50.     build()
  51.     fill()
  52.  
  53.     query("ALTER TABLE test_array ADD PRIMARY KEY(id)" )
  54.     query("COMMIT")
  55.     query("VACUUM ANALYZE test_array" )
  56.  
  57. for length in 1, 10, 100, 1000, 10000, 100000:
  58.     if length <= 1000:
  59.         ntest = 100
  60.     else:
  61.         ntest = 1
  62.     ids = ",".join( str(n) for n in range(1, 1+length))
  63.     bench( ntest, "SELECT * FROM test_array WHERE id =ANY(ARRAY[%s])"%ids, (), "Correlated" )
  64.     bench( ntest, "SELECT * FROM test_array WHERE id IN (%s)"%ids, (), "Correlated" )
  65.     bench( ntest, "SELECT * FROM unnest(ARRAY[%s]) id JOIN test_array USING (id)"%ids, (), "Correlated" )
  66.  
  67.     ids = ",".join( str(random.randint(1,NROWS)) for n in range(1, 1+length))
  68.     bench( ntest, "SELECT * FROM test_array WHERE id =ANY(ARRAY[%s])"%ids, (), "Random" )
  69.     bench( ntest, "SELECT * FROM test_array WHERE id IN (%s)"%ids, (), "Random" )
  70.     bench( ntest, "SELECT * FROM unnest(ARRAY[%s]) id JOIN test_array USING (id)"%ids, (), "Random" )
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement