Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/nhibernate/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OR和and优化Sqlite查询_Sqlite_Nhibernate - Fatal编程技术网

使用OR和and优化Sqlite查询

使用OR和and优化Sqlite查询,sqlite,nhibernate,Sqlite,Nhibernate,执行以下查询需要5秒钟: SELECT DISTINCT(Product.Name) FROM Product WHERE (0=1 OR Product.Number="prod11"); 而以下操作只需15毫秒: SELECT DISTINCT(Product.Name) FROM Product WHERE (Product.Number="prod11"); SELECT DISTINCT(Product.Name) FROM Product WHERE (1=1 AND Produ

执行以下查询需要5秒钟:

SELECT DISTINCT(Product.Name) FROM Product WHERE (0=1 OR Product.Number="prod11");
而以下操作只需15毫秒:

SELECT DISTINCT(Product.Name) FROM Product WHERE (Product.Number="prod11");
SELECT DISTINCT(Product.Name) FROM Product WHERE (1=1 AND Product.Number="prod11");
有趣的是,以下过程也只需要15毫秒:

SELECT DISTINCT(Product.Name) FROM Product WHERE (Product.Number="prod11");
SELECT DISTINCT(Product.Name) FROM Product WHERE (1=1 AND Product.Number="prod11");
查询计划显示第一个查询使用完整表扫描(出于未知原因),而第二个和第三个查询使用索引(如预期的那样)

出于某种原因,它看起来像是Sqlite优化了“1=1和…”,但它没有优化“0=1或…”

如何使Sqlite在第一个查询中也使用索引

查询是由NHibernate构建的,所以很难更改它们


Sqlite版本是针对Windows的最新版本。

Sqlite的查询优化器非常简单和实用

出于某种原因,如果可以使用覆盖索引,则可以优化此查询,因此请尝试以下操作:

CREATE INDEX TakeThatNHibernate ON Product(Number, Name)

1=1
1=0
是在
NHibernate
框架的某些部分中使用的SQL表达式,用于表示不会改变SQL查询逻辑的空语句。无子标准的
连接
生成
1=1
表达式,无子标准的
分离
生成
1=0
表达式。如果未提供值,In()将生成
1=0
表达式


为了避免这种优化,您可以更改创建这些空表达式的代码,并且只使用至少有一个子标准的标准。

0\u为什么在
where
条件中有多余的语句(即
0=1
1=1
)?这只是一个更大查询的简化部分。根据搜索条件,0=1和1=1检查用于启用或禁用WHERE子句的部分内容。在NHibernate端,查询包含类似(!criteria.FilterByProductNumber | | | criteria.ProductNumber==product.Number)&(!criteria.FilterByOtherProperty | | criteria.OtherProperty=product.OtherProperty)。。“!criteria.FilterBy…”导致插入常量0=1和1=1表达式。Postgres实际上还优化了0=1的大小写以使用索引。我看不出为什么Sqlite可以优化1=1而不是0=1…?您是否尝试删除不必要的语句?我无法删除它们。。NHibernate创造了它们。