I have done simple performance test on my local machine, this is python script:
import redis
import sqlite3
import time
data = {}
N = 100000
for i in xrange(N):
key = "key-"+str(i)
value = "value-"+str(i)
data[key] = value
r = redis.Redis("localhost", db=1)
s = sqlite3.connect("testDB")
cs = s.cursor()
try:
cs.execute("CREATE TABLE testTable(key VARCHAR(256), value TEXT)")
except Exception as excp:
print str(excp)
cs.execute("DROP TABLE testTable")
cs.execute("CREATE TABLE testTable(key VARCHAR(256), value TEXT)")
print "[---Testing SQLITE---]"
sts = time.time()
for key in data:
cs.execute("INSERT INTO testTable VALUES(?,?)", (key, data[key]))
#s.commit()
s.commit()
ste = time.time()
print "[Total time of sql: %s]"%str(ste-sts)
print "[---Testing REDIS---]"
rts = time.time()
r.flushdb()# for empty db
for key in data:
r.set(key, data[key])
rte = time.time()
print "[Total time of redis: %s]"%str(rte-rts)
I expected redis to perform faster, but the result shows that it much more slower:
[---Testing SQLITE---]
[Total time of sql: 0.615846157074]
[---Testing REDIS---]
[Total time of redis: 10.9668009281]
So, the redis is memory based, what about sqlite? Why redis is so slow? When I need to use redis and when I need to use sqlite?
flushdb
?CREATE TABLE IF NOT EXISTS
and you can take out the roundtrips and try/catch block ;)