我有一些 Django 代码,它以编程方式构建相对复杂的查询,通过一系列过滤器和排除调用将各种过滤器应用于初始数据集:
for filter in filters:
if filter['name'] == 'revenue':
accounts = accounts.filter(account_revenue__in: filter['values'])
if filter['name'] == 'some_other_type':
if filter['type'] == 'inclusion':
accounts = accounts.filter(account__some_relation__in: filter['values'])
if filter['type'] == 'exclusion':
accounts = accounts.exclude(account__some_relation__in: filter['values'])
...etc
return accounts
Run Code Online (Sandbox Code Playgroud)
对于大多数这些条件,过滤器的可能值相对较小且包含在内,因此 Django 的 ORM 生成的 IN 子句具有足够的性能。然而,在某些情况下,IN 子句可能会更大(10K - 100K 项)。
在普通的 postgres 中,我可以通过使用表值构造函数使这个查询更加优化,例如:
SELECT domain
FROM accounts
INNER JOIN (
VALUES ('somedomain.com'), ('anotherdomain.com'), ...etc 10K more times
) VALS(v) ON accounts.domain=v
Run Code Online (Sandbox Code Playgroud)
如果原始查询中有超过 30K 的 IN 子句,则运行时间可能超过 60 秒,而表值版本的查询则需要 1 秒,这是一个巨大的差异。
但我无法弄清楚如何让 …
我有一个CQL表(cql 3,cassandra 2.0.*),它看起来像:
CREATE TABLE IF NOT EXISTS user_things (
user_id bigint,
thing_id bigint,
created_at timeuuid,
PRIMARY KEY (user_id, thing_id)
);
Run Code Online (Sandbox Code Playgroud)
我想做插件
INSERT INTO user_things (user_id, thing_id, created_at) VALUES (?, ?, now())
Run Code Online (Sandbox Code Playgroud)
但仅当行不存在时.
我可以在两个同步语句中执行此操作(首先是SELECT,如果SELECT没有返回行,则返回INSERT)或者我可以使用INSERT ... IF NOT EXISTS.
该CQL文档状态"但是,请注意,使用IF NOT EXISTS将产生不可忽略的性能成本(内部,Paxos的将被使用),所以这应该谨慎使用."
我想知道是否有人做过基准测试,看看如果我们有很多这样的操作发生了什么更有效率?(说几百秒)