knex 在阅读巨大的表格时超时

knex times out when reading tables that are huge

我在 Azure SQL 数据库中有一个巨大的 table,大约有 1000 多行。我正在使用 NodeJS 和 Knex 构建 SQL 后端。每次我尝试获取整个 table 的内容时,连接都会超时。但是,当我尝试使用过滤器查询内容时,一切正常。我的代码如下所示。我是否必须以不同方式处理 return 大量数据的查询?

activity: {
    get: async (queryObject) => {
      
      return queryObject != null
        ? knex('Activities').where(queryObject)
        : knex('Activities');
    },

我将解决方案总结如下。

如果你想在一个table和knex中查询大行,我建议你使用knex.Streams.stream()方法来读取数据。详情请参考here.

例如

const options = {
    client: '',
    connection: {
        host: '127.0.0.1',
        user: 'user12',
        password: 's$cret',
        database: 'mydb'
    }
}
const knex = require('knex')(options);
const stream=  knex.select("*").from("CSVTest").stream()
 stream.on('error', function (err) {
                        // Handle error, an 'end' event will be emitted after this as well
                    })
                        .on('fields', function (fields) {
                            // the field packets for the rows to follow
                        })
                        .on('result', function (row) {
                            // Pausing the connnection is useful if your processing involves I/O

                            processRow(row);
                        })
                        .on('end', function () {
                            // all rows have been received
                        });