Querydsl正在为列生成一个nvarchar
参数的存储过程,但表的列类型为varchar
。这样MS-SQLServer就无法使用索引,这会显着增加查询执行时间。
Querydsl使用的元数据是使用Querydsl的MetaDataSerializer
(蚂蚁目标)生成的。
如何强制Querydsl为列使用varchar
存储过程参数?
数据库表:
TableX有三列:id(int),ColA(varchar)和ColB(nvarchar)
生成的元数据:
public class QTableX extends com.mysema.query.sql.RelationalPathBase<QTableX> {
private static final long serialVersionUID = 1142355320;
public static final QTableX TableX= new QTableX("TableX");
public final StringPath id = createNumber("id", Integer.class);
public final StringPath colA= createString("ColA");
public final StringPath colB= createString("ColB");
public QTableX(String variable) {
super(QTableX.class, forVariable(variable), "dbo", "TableX");
}
public QTableX(Path<? extends QTableX> path) {
super((Class)path.getType(), path.getMetadata(), "dbo", "TableX");
}
public QTableX(PathMetadata<?> metadata) {
super(QTableX.class, metadata, "dbo", "TableX");
}
}
Querydsl :
SQLQuery query = new SQLQueryImpl(new SQLServerTemplates());
query.from(QTableX.TableX).where(QTableX.TableX.colA.eq("FieldName")
.and(QTableX.TableX.colB.like(".1.")));
生成了sql :
declare @p1 int
set @p1=NULL
-- @P0 should be varchar (not nvarchar)
exec dbo.sp_prepare @p1 output,N'@P0 nvarchar(4000),@P1 nvarchar(4000)',N'select TableX.id
from TableX TableX
where TableX.ColA = @P0 and TableX.ColB like @P1 escape ''\''',1
exec dbo.sp_execute @p1, N'FieldName', N'.1.'