今天测试了一下 sqlalchemy 性能
self.db.query(Users).filter(Users.Id==1).first() < self.db.execute('SELECT * FROM `users` WHERE Id=%s' % user_id).first() < self.db.queryRow('SELECT * FROM `users` WHERE `Id`=%s' % user_id)
sqlalchemy的echo=False的
ab测试如下:
ab -n 2000 -c 50 http://127.0.0.1:8888/
self.db.query(Users).filter(Users.Id==1).first() 的
Requests per second: 460.00左右 [#/sec] (mean)
self.db.execute('SELECT * FROM `users` WHERE Id=%s' % user_id).first()
Requests per second: 860.00左右 [#/sec] (mean)
下面这个为 MySQLdb
self.db.queryRow('SELECT * FROM `users` WHERE `Id`=%s' % user_id)
Requests per second: 1110.00左右 [#/sec] (mean)
在想是否是用MySQLdb好呢还是用sqlalchemy好。
这MySQLdb我也改了下代码的:基本的CRUD如下:
db = db(host='localhost', user='root', passwd='password', port=3306, db='test', charset='utf8'); #row = db.queryRow('SELECT @@version') row = db.queryRow('SELECT `id`, `name` FROM `test` WHERE id=2') if(row): print row['name'] else: print '123' data = {} data['UserName'] = "mytest??" data['Email'] = "my2@test.com" data["Age"] = 26 data["money"] = 6254.258 #data['bc']="def" #data['flt']=38.5 #添加数据 #print db.execute_insert('mytb',data) #修改数据 print db.execute_update('mytb', data, {'id':1, "Age":24}) row = db.queryRow('select * from mytb where id = %s', (2)) print row['UserName'] for item in db.queryAll('select * from mytb'): print item['UserName']
据说 amysql 的性能比MySQLDb 快2.5倍。