mysql handler与select 性能比对


mysql> create table test(id int AUTO_INCREMENT PRIMARY KEY NOT NULL,value varchar(100) NOT NULL);

Description: 
'''
import random
from time import time
import oursql
class handler:
     
    def __init__(self):
        pass

    def build_conn(self):
        
        self.conn = oursql.connect(host = 'localhost',
            user = 'root',
            passwd = '',
            db = 'test'
            )
    def reconnect(self):
        try:
            self.conn.ping()
        except:
            try:
                self.conn.close()
            except:
                pass
            finally:
                self.build_conn()
    
    def open(self):
        self.build_conn()

    def close(self):
        self.conn.close()
   
    def handler(self):
        """docstring for handler"""
        self.reconnect()
        cursor = self.conn.cursor()
        query = 'handler test read first'
        cursor.execute(query)
        cursor.close()
    def table_open(self):
        """docstring for table_open"""
        self.reconnect()
        cursor = self.conn.cursor()
        query = 'handler test open'
        cursor.execute(query)
        cursor.close()
    def table_close(self):
        """docstring for table_close"""
        self.reconnect()
        cursor = self.conn.cursor()
        query = 'handler test close'
        cursor.execute(query)
        cursor.close()
    def table_handler(self):
        """docstring for table_select"""
        self.reconnect()
        self.table_open()
        start=time()
        for i in range(0,10000):
            cursor = self.conn.cursor()
            query = 'handler test read id = (%s) '%(i,)
            cursor.execute(query)
            res = cursor.fetchone()
            cursor.close()
        end=time()
        print end-start
        self.table_close()
        return res
    def table_select(self):
         """docstring for table_select"""
         self.reconnect()
         start=time()
         for i in range(10000,20000):
            cursor = self.conn.cursor()
            query = 'select * from test where id = (%s) '%(i,)
            cursor.execute(query)
            res = cursor.fetchone()
            cursor.close()
         end=time()
         print end-start
         return res
    
    def table_insert(self):
        """docstring for table_insert"""
        self.reconnect()
        for i in range(20000):
            cursor = self.conn.cursor()
            value = random.randint(0,10000)
            query = 'insert into test(value) values(%s)'%(value,)
            print query
            cursor.execute(query)
            cursor.close()
            self.conn.commit()

if __name__ == '__main__':
    test = handler()
    test.open()
    test.table_handler()
    test.table_select()
    test.close()


└─(17:46:%)── python handler.py                                                                                                             ──(Thu,Oct27)─┘
1.2317969799    handler的结果
1.61981511116  select的结果

你可能感兴趣的:(handler)