OrmLite 中参数化查询的参数长度
Parameter lengths for parameterized queries in OrmLite
OrmLite 中的 POCO 更新执行 SQL 就像这个例子:
(@P1 varchar(1043),@P2 varchar(6))
UPDATE table
SET FILEDATA=@P1
WHERE FILEID=@P2
但它会导致基于具有不同参数长度的不同 @P1
和 @P2
值的多个查询计划。
那么,在 Ormlite 中为参数化查询指定数据 types/lengths 的最佳方式是什么,以便正确缓存查询计划,并避免由于参数长度可变而导致多个查询计划?
这是具有可变长度字符串的类似情况:https://dba.stackexchange.com/questions/216330/parameterized-query-creating-many-plans
更新
这是一个例子:
数据库Table
dbo.Users
Id (PK, int, not null)
Email (nvarchar(150), not null)
POCO
[Alias("Users")]
public class User
{
[PrimaryKey]
[AutoIncrement]
public int Id { get; set; }
public string Email { get; set; }
}
代码
int userId = 1;
User user;
// get User
using (var db = DbConn.OpenDbConnection())
{
user = db.SingleById<User>(userId);
}
// print User email (hi@example.com)
Console.WriteLine(user.Email);
// update User email
using (var db = DbConn.OpenDbConnection())
{
user.Email = "tester@example.org";
db.Update(User);
}
更新操作将导致 SQL 查询类似于我在顶部发布的查询,参数长度可变。由于参数长度可变,它会导致 SQL 服务器创建多个查询计划。理想情况下,查询应该具有固定长度的参数,以便可以为具有不同参数值(即不同电子邮件)的相同操作(例如用户更新)创建、缓存和重用查询计划。
现在从 this commit where it takes the default string size of the configured StringConverter
. This change is available from v5.5.1 that's now available on MyGet 指定字符串参数的大小。
如果需要,它的行为可以被 replacing the String Converter 覆盖并覆盖 InitDbParam()
.
OrmLite 中的 POCO 更新执行 SQL 就像这个例子:
(@P1 varchar(1043),@P2 varchar(6))
UPDATE table
SET FILEDATA=@P1
WHERE FILEID=@P2
但它会导致基于具有不同参数长度的不同 @P1
和 @P2
值的多个查询计划。
那么,在 Ormlite 中为参数化查询指定数据 types/lengths 的最佳方式是什么,以便正确缓存查询计划,并避免由于参数长度可变而导致多个查询计划?
这是具有可变长度字符串的类似情况:https://dba.stackexchange.com/questions/216330/parameterized-query-creating-many-plans
更新
这是一个例子:
数据库Table
dbo.Users
Id (PK, int, not null)
Email (nvarchar(150), not null)
POCO
[Alias("Users")]
public class User
{
[PrimaryKey]
[AutoIncrement]
public int Id { get; set; }
public string Email { get; set; }
}
代码
int userId = 1;
User user;
// get User
using (var db = DbConn.OpenDbConnection())
{
user = db.SingleById<User>(userId);
}
// print User email (hi@example.com)
Console.WriteLine(user.Email);
// update User email
using (var db = DbConn.OpenDbConnection())
{
user.Email = "tester@example.org";
db.Update(User);
}
更新操作将导致 SQL 查询类似于我在顶部发布的查询,参数长度可变。由于参数长度可变,它会导致 SQL 服务器创建多个查询计划。理想情况下,查询应该具有固定长度的参数,以便可以为具有不同参数值(即不同电子邮件)的相同操作(例如用户更新)创建、缓存和重用查询计划。
现在从 this commit where it takes the default string size of the configured StringConverter
. This change is available from v5.5.1 that's now available on MyGet 指定字符串参数的大小。
如果需要,它的行为可以被 replacing the String Converter 覆盖并覆盖 InitDbParam()
.