为什么在这个简单的基准测试中 SQLite 比 Redis 快? [英] Why is SQLite faster than Redis in this simple benchmark?

查看:75
本文介绍了为什么在这个简单的基准测试中 SQLite 比 Redis 快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地机器上做了简单的性能测试,这是python脚本:

导入redis导入 sqlite3导入时间数据 = {}N = 100000对于 xrange(N) 中的 i:key = "key-"+str(i)value = "value-"+str(i)数据[键] = 值r = redis.Redis("本地主机", db=1)s = sqlite3.connect("testDB")cs = s.cursor()尝试:cs.execute("CREATE TABLE testTable(key VARCHAR(256), value TEXT)")除了作为 excp 的异常:打印 str(excp)cs.execute("DROP TABLE testTable")cs.execute("CREATE TABLE testTable(key VARCHAR(256), value TEXT)")打印[---测试SQLITE---]"sts = time.time()对于关键数据:cs.execute("INSERT INTO testTable VALUES(?,?)", (key, data[key]))#s.commit()s.commit()ste = time.time()打印[sql的总时间:%s]"%str(ste-sts)打印[---测试 REDIS---]"rts = time.time()r.flushdb()# 用于空数据库对于关键数据:r.set(key, data[key])rte = time.time()打印[redis 的总时间:%s]"%str(rte-rts)

我希望 redis 执行得更快,但结果显示它要慢得多:

[---测试SQLITE---]【sql总时间:0.615846157074】[---测试Redis---]【redis总时间:10.9668009281】

那么,redis 是基于内存的,那么 sqlite 呢?为什么redis这么慢?什么时候需要用redis,什么时候需要用sqlite?

解决方案

来自 redis 文档<块引用>

Redis 是一个服务器:所有命令都涉及网络或 IPC 往返.将其与 SQLite、Berkeley DB、Tokyo/Kyoto Cabinet 等嵌入式数据存储进行比较是没有意义的……因为大多数操作的成本恰恰是由网络/协议管理主导的.

虽然在某些情况下承认速度问题,但确实有意义.例如,在多次并行访问的情况下,Redis 的性能可能比 sqlite 好得多.

适合工作的正确工具,有时是 redis 有时是 sqlite 有时是完全不同的东西.如果此速度测试正确显示了您的应用实际将执行的操作,那么 sqlite 将为您提供更好的服务,并且您执行此基准测试是件好事.

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?

解决方案

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.

这篇关于为什么在这个简单的基准测试中 SQLite 比 Redis 快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆