Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is SQLite faster than Redis in this simple benchmark?

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?

like image 577
torayeff Avatar asked Jun 26 '12 22:06

torayeff


People also ask

Is SQLite faster than Redis?

In terms of the efficiency of updating databases, Redis is superior to MySQL while SQLite is slowest. However, in terms of the efficiency of querying from databases, SQLite seems to be about ten times faster than Redis and MySQL.

Why is SQLite faster?

SQLite uses a very simplistic access algorithm, its fast but does not handle concurrency. As the database starts to grow, and the amount of simultaneous access it will start to suffer. The way servers handle multiple requests is completely different and way more complex and optimized for high concurrency.

How fast is Redis compared to database?

Speed: Redis is extremely fast in comparison to other datastores. Redis claims to be faster as it stores huge volumes of data in the primary memory within a fraction of seconds. It loads up to 110000 SETs/second. It also retrieves 81000 GETs/second in an entry-level Linux box.

How fast is SQLite in memory?

sqlite or memory-sqlite is faster for the following tasks: select two columns from data (<. 1 millisecond for any data size for sqlite . pandas scales with the data, up to just under 0.5 seconds for 10 million records)


1 Answers

from the redis documentation

Redis is a server: all commands involve network or IPC roundtrips. It is meaningless to compare it to embedded data stores such as SQLite, Berkeley DB, Tokyo/Kyoto Cabinet, etc ... because the cost of most operations is precisely dominated by network/protocol management.

Which does make sense though it's an acknowledgement of speed issues in certain cases. Redis might perform a lot better than sqlite under multiples of parallel access for instance.

The right tool for the right job, sometimes it'll be redis other times sqlite other times something totally different. If this speed test is a proper showing of what your app will realistically do then sqlite will serve you better and it's good that you did this benchmark.

like image 180
Harald Brinkhof Avatar answered Sep 17 '22 18:09

Harald Brinkhof