简体   繁体   English

动态的预备陈述是否有意义?

[英]Does a dynamic prepared statement makes sense?

I want to create dynamic prepared statements, that every part is dynamic, the values, the table and the WHERE part. 我想创建动态的准备好的语句,每个部分都是动态的,值,表和WHERE部分。

I use nodejs + PostgreSQL and the pg module to talk to the PostgreSQL. 我使用nodejs + PostgreSQL和pg模块与PostgreSQL通讯。 The pg module offers a different syntax to go along with node.js , but I guess the principles are the same. pg模块提供了与node.js一起使用的不同语法,但是我想原理是相同的。 This is based to the official example here 这是基于此处的官方示例

//dynamic that can change 
let select = 'name , email, age';
let table = 'user';
let where = 'id=$1 AND gender=$2';
let values = [1,'female'];

//prepare
const query = {
  // give the query a unique name  
  name: 'fetch-user',
  text: 'SELECT' + select + 'FROM' + table + 'WHERE' + where,
  values: values
}

//execute
client.query(query)
  .then(res => console.log(res.rows[0]))
  .catch(e => console.error(e.stack))

I was wondering if this will make sense , performance-wise . 我想知道从性能角度来看这是否有意义。

I red the documentation and , what I understand is that by having all the parts of a prepared statement dynamic, then the planning may be not so effective , or not effective at all. 我将文档编成红色,并且我了解的是,通过使准备好的语句的所有部分动态化,计划可能不会那么有效,或者根本就无效。

What should I do? 我该怎么办? Should I keep this dynamic syntax? 我应该保留这种动态语法吗? Or it doesn't make any sense, so I have to create multiple prepared statements and use them for different tables? 还是没有任何意义,所以我必须创建多个准备好的语句并将它们用于不同的表?

Thanks 谢谢

There should be no performance issues here. 这里应该没有性能问题。 The "dynamic" part of your SQL is just a string you're passing into the query object, so the only performance to consider is resolving the text property. SQL的“动态”部分只是要传递给查询对象的字符串,因此要考虑的唯一性能是解析text属性。 You're passing your database a fully prepared statement; 您正在向数据库传递充分准备的语句; it's nodejs that is resolving the different variables to come up with the query object's text property. 是nodejs解决了不同的变量,以提供查询对象的text属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM