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的结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值