Skip to content

Instantly share code, notes, and snippets.

@keitheis
Last active December 13, 2015 19:08
Show Gist options
  • Save keitheis/4959972 to your computer and use it in GitHub Desktop.
Save keitheis/4959972 to your computer and use it in GitHub Desktop.
It tests the speed of building SQL by different ways, really.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sql
from sqlalchemy import MetaData, Table, Column, String, DateTime
metadata = MetaData()
users = Table('users', metadata,
Column('id', String, primary_key=True),
Column('name', String),
Column('email', String),
Column('created', DateTime))
def use_sqlalchemy():
return str(users.select().where(users.c.id == '20130215'))
def use_sqlalchemy_mosql():
return str(sql.select('users', {'id': '20130215'}))
def use_sqlalchemy_sql():
return str("SELECT * FROM users WHERE id = '20130215';")
if __name__ == '__main__':
from timeit import timeit
print use_sqlalchemy()
print
print use_sqlalchemy_mosql()
print
print use_sqlalchemy_sql()
print
n = 30000
print '* Using SQLAlchemy'
print timeit(use_sqlalchemy, number=n)
print
print '* Using SQLAlchemy with MoSQL:'
print timeit(use_sqlalchemy_mosql, number=n)
print
print '* Using SQLAlchemy with pure SQL:'
print timeit(use_sqlalchemy_sql, number=n)
print
"""
Output:
SELECT users.id, users.name, users.email, users.created
FROM users
WHERE users.id = :id_1
SELECT * FROM users WHERE id = '20130215';
SELECT * FROM users WHERE id = '20130215';
* Using SQLAlchemy
7.11727309227
* Using SQLAlchemy with MoSQL:
1.16956305504
* Using SQLAlchemy with pure SQL:
0.00899291038513
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment