#!/usr/bin/python import threading import MySQLdb import sys # insert many new records unix_socket = "/tmp/mysql.sock" iterations = 1000000 rd = 3 # hello world version of insert def insert_hw(): return 0 def insert(): for i in range(iterations): a = i; b = a % rd conn = MySQLdb.connect( host="localhost", user="testuser", passwd="testpass", db="testdb", unix_socket=unix_socket) cursor = conn.cursor() param = (a,b) sql="insert into testdb.t values (%s, %s)" #print sql cursor.execute (sql, param); conn.commit() conn.close() #cursor = self.conn.cursor() #cursor.execute("exit") #print "insert" def update(): unit = iterations / rd; idx = 0 for i in range(rd): begin = i * unit ; end = (i + 1) * unit ; for j in range(begin, end): conn = MySQLdb.connect( host="localhost", user="testuser", passwd="testpass", db="testdb", unix_socket=unix_socket) cursor = conn.cursor() a1 = idx b1 = idx % rd a2 = idx + iterations cursor.execute (""" UPDATE t SET a = %s WHERE a = %s and b = %s """, (a2, a1, b1)); conn.commit() conn.close() idx = idx + 1 # remove all the data from table def clear(): conn = MySQLdb.connect( host="localhost", user="testuser", passwd="testpass", db="testdb", unix_socket=unix_socket) cursor = conn.cursor() cursor.execute("delete from testdb.t") conn.commit() conn.close() def select(): conn = MySQLdb.connect( host="localhost", user="testuser", passwd="testpass", db="testdb", unix_socket=unix_socket) cursor = conn.cursor() cursor.execute("select * from testdb.t order by a") cds=cursor.fetchall() #print cds conn.close() def main(): clear() insert() print 'insert done' #select() update() print 'update done' #select() if __name__ == '__main__': main();