python - 使用 python 读取优化 cassandra

标签 python python-3.x cassandra datastax

我有一个具有以下模型的表:

CREATE TABLE IF NOT EXISTS {} (
                user_id bigint ,
                pseudo text,
                importance float,
                is_friend_following bigint,
                is_friend boolean,
                is_following boolean,
                PRIMARY KEY ((user_id), is_friend_following)
            );

我还有一张 table ,里面有我的种子。这 (20) 个用户是我的图表的起点。因此,我选择他们的 ID 并在上表中搜索以获取他们的关注者和 friend ,然后从那里构建我的图表 (networkX)。

def build_seed_graph(cls, name):
    obj = cls()
    obj.name = name
    query = "SELECT twitter_id FROM {0};"
    seeds = obj.session.execute(query.format(obj.seed_data_table))
    obj.graph.add_nodes_from(obj.seeds)
    for seed in seeds:
        query = "SELECT friend_follower_id, is_friend, is_follower FROM {0} WHERE user_id={1}"
        statement = SimpleStatement(query.format(obj.network_table, seed), fetch_size=1000)
        friend_ids = []
        follower_ids = []
        for row in obj.session.execute(statement):
            if row.friend_follower_id in obj.seeds:
                if row.is_friend:
                    friend_ids.append(row.friend_follower_id)
                if row.is_follower:
                    follower_ids.append(row.friend_follower_id)
        if friend_ids:
            for friend_id in friend_ids:
                obj.graph.add_edge(seed, friend_id)
        if follower_ids:
            for follower_id in follower_ids:
                obj.graph.add_edge(follower_id, seed)
    return obj

问题是构建图表所需的时间太长,我想对其进行优化。 我的表 'network_table' 中有大约 500 万行。

我想知道如果不使用 where 子句进行查询而只对整个表执行单个查询,这对我来说是否会更快?它会适合内存吗?这是一个好主意吗?还有更好的办法吗?

最佳答案

我怀疑真正的问题可能不是查询,而是处理时间。

I'm wondering if it would be faster for me if instead of doing a query with a where clauses to just do a single query on whole table? Will it fit in memory? Is that a good Idea? Are there better way?

如果启用分页(https://datastax.github.io/python-driver/query_paging.html - 使用 fetch_size),对整个表执行单个查询应该不会有任何问题。 Cassandra 将返回 fetch_size,并在您从 result_set 中读取其他结果时获取这些结果。

请注意,如果表中有许多与种子无关的行,则完整扫描可能会较慢,因为您将收到不包含“种子”的行

免责声明 - 我是构建 ScyllaDB 团队的一员 - 一个与 Cassandra 兼容的数据库。

ScyllaDB 最近发布了一篇关于如何高效地并行进行全面扫描的博客 http://www.scylladb.com/2017/02/13/efficient-full-table-scans-with-scylla-1-6/这也适用于 Cassandra - 如果完整扫描相关并且您可以并行构建图表,那么这可能会对您有所帮助。

关于python - 使用 python 读取优化 cassandra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813635/

相关文章:

python - 通过environment.yml使用conda安装npm包

python - 内存泄漏 - gunicorn + django + mysqldb

python - 我可以使用 python 套接字发送更多数据进行 DDOS 攻击吗?

python - 有没有办法在移动网站上向下滚动或触发无限滚动加载?

android - Cassandra DB For Android,它的 API 以及如何使用它?

python - 过滤用户电子邮件时在 cassandra 中不区分大小写

Python3无法安装bcrypt

Python3 在 tar 文件中处理 csv 文件

mysql - 检查 csv 文件中是否存在数据库记录

cassandra - cassandra 中的故障检测和恢复机制如何工作?