Skip to content

Instantly share code, notes, and snippets.

@mocyuto
Last active August 29, 2015 14:14
Show Gist options
  • Save mocyuto/ece0c8126c1514091f8b to your computer and use it in GitHub Desktop.
Save mocyuto/ece0c8126c1514091f8b to your computer and use it in GitHub Desktop.
MySQLとinfluxDBの比較スクリプト
# coding: utf-8
import doctest
import MySQLdb
from influxdb import InfluxDBClient as ifdb
from datetime import datetime
DATA = "123456789012345678901234567890123456789012345678901234567890"
COUNT = 10000 * 1
def mysql():
"""MySQLのINSERTとSELECT
"""
print "MySQL"
connect = MySQLdb.connect(db = "test", user="root")
cursor = connect.cursor()
# prepare
cursor.execute("drop table if exists person")
cursor.execute("create table person (id integer, name varchar(200)) engine = InnoDB")
# INSERT
start = datetime.now()
for i in range(COUNT):
insertSQL = "insert into person (id, name) values(" + str(i) + ", '" + DATA + "')"
cursor.execute(insertSQL)
end = datetime.now()
print "insert time: ", end-start
# SELECT
start = datetime.now()
for i in range(COUNT):
selectSQL = "select * from person where id =" + str(i)
cursor.execute(selectSQL)
end = datetime.now()
print "select time: ", end-start
cursor.close()
connect.close()
def influxDB():
"""influxDBのINSERT
"""
print "influxDB"
dbName = "example"
# prepare
client = ifdb('localhost', 8086, 'root', 'root', dbName)
if dbName in [x["name"] for x in client.get_list_database()]:
client.delete_database(dbName)
client.create_database(dbName)
# INSERT
start = datetime.now()
for i in range(COUNT):
jsonBody = [{
"points": [[i, DATA]],
"name": "person",
"columns": ["id", "personName"]
}]
client.write_points(jsonBody)
end = datetime.now()
print "insert time: ", end-start
# SELECT
start = datetime.now()
for i in range(COUNT):
selectSQL = "select id from person where id = " + str(i)
client.query(selectSQL)
end = datetime.now()
print "select time: ", end-start
if __name__ == "__main__":
mysql()
influxDB()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment