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



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

activity: {
get: async (queryObject) => {

return queryObject != null
? knex('Activities').where(queryObject)
: knex('Activities');
},

我将解决方案总结如下。

如果您想用knex在一个表中查找大行,我建议您使用knex.Streams.stream()方法来读取数据。有关更多详细信息,请参阅此处。

例如

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
});

最新更新