crateDB 在 Npgsql C# 客户端中无需迭代即可读取 10k 行

crateDB read 10k rows without iteration in Npgsql C# client

我在 crate 数据库中有 10k 行数据。如何在不迭代的情况下读取数据。我正在为我的服务使用 crateDB C# Npgsql 客户端。

var connString = "Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase";

await using var conn = new NpgsqlConnection(connString);
await conn.OpenAsync();
// Retrieve all rows
var cmd = new NpgsqlCommand("select * from sensordata where timestamp >= extract(epoch from (now() - interval '1 week'))", conn);
            var result = new List<SensorDataViewModel>();

           using (var reader = cmd.ExecuteReader())
            {
                    while(reader.HasRows && reader.Read())
                    {
                       SensorDataViewModel item  = new SensorDataViewModel { 
                            sensorid = reader["sensorid"].ToString(),
                            deviceid = reader["deviceid"].ToString(),
                            reading = Convert.ToInt32(reader["reading"]),
                            timestamp = (double)reader["timestamp"]
                        };
                        result.Add(item);
                    }
            }

这里我在 while 循环中一次读取每一行。这需要很多时间来处理?

也许您需要考虑使用 EntityFrameworkCore。更多详情请参考https://www.npgsql.org/efcore/index.html

下面是我试过的示例代码。在 CrateDb 中包含 2 tables。一个是 customers,另一个是 todo。这些table有关系customer.id=todo.customer_id.

示例中的第一个和第二个查询分别是 select 来自 2 table 的每条记录。

第三个查询是使用join根据关系检索相关记录并通过customers.id过滤。

    class Program
    {
        static async Task Main(string[] args)
        {
            MemoContext memoContext = new MemoContext();

            //select * from customers
            List<Customer> customers = memoContext.customers.Select(x => new Customer
            {
                id = x.id,
                name = x.name,
                contactno = x.contactno,
                email = x.email
            }).ToList();

            //select * from todo
            List<Todo> todos = memoContext.todo.Select(x => new Todo
            {
                complete = x.complete,
                customer_id = x.customer_id,
                id = x.id,
                title = x.title
            }).ToList();

            //SELECT c.name, c.email, c.contactno, t.title, t.complete 
            //FROM customers AS c 
            //JOIN todo AS t ON t.customer_id = c.id 
            //WHERE c.id=1
            var memo = memoContext.customers.Join(
                    memoContext.todo,
                    c => c.id,
                    t => t.customer_id,
                    (c, t) => new
                    {
                        id = c.id,
                        name = c.name,
                        email = c.email,
                        contactno = c.contactno,
                        todotitle = t.title,
                        complete = t.complete
                    }).Where(n => n.id == 1).ToList();
        }
    }


    class MemoContext : DbContext
    {
        public DbSet<Customer> customers { get; set; }
        public DbSet<Todo> todo { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Username=crate;SSL Mode=Prefer;Database=doc");
    }

希望对你有用