0

我在 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 循环中一次读取每一行。这需要很多时间来处理?

4

1 回答 1

1

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

下面是我尝试过的示例代码。在 CrateDb 中包含 2 个表。一个是客户,另一个是待办事项。这些表具有customer.id=todo.customer_id的关系。

示例中的第一个和第二个查询只是分别从两个表中选择每条记录。

第三个查询是使用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");
    }

我希望它对你有用。

于 2021-02-05T20:01:58.587 回答