gpt4 book ai didi

java - 准备好的语句如何提高性能

转载 作者:行者123 更新时间:2023-11-29 07:41:51 30 4
gpt4 key购买 nike

<分区>

我看到下面的语句,它讲述了我们通过 JDBC PreparedStatement 类获得的性能改进。

If you submit a new, full SQL statement for every query or update to the database, the database has to parse the SQL and for queries create a query plan. By reusing an existing PreparedStatement you can reuse both the SQL parsing and query plan for subsequent queries. This speeds up query execution, by decreasing the parsing and query planning overhead of each execution.

假设我正在创建语句并在运行这样的查询时提供不同的值:

String sql = "update people set firstname=? , lastname=? where id=?";

PreparedStatement preparedStatement =
connection.prepareStatement(sql);

preparedStatement.setString(1, "Gary");
preparedStatement.setString(2, "Larson");
preparedStatement.setLong (3, 123);

int rowsAffected = preparedStatement.executeUpdate();

preparedStatement.setString(1, "Stan");
preparedStatement.setString(2, "Lee");
preparedStatement.setLong (3, 456);

int rowsAffected = preparedStatement.executeUpdate();

那么我仍然会获得性能优势,因为我正在尝试设置不同的值,以便生成的最终查询根据值发生变化。

能否请您准确解释我们何时获得性能优势?这些值也应该相同吗?

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com