[英]SQL Query concatenation performance boost
I have a class (that reflects a db row) that has more than 200 instances created within the codes bootstrap. 我有一个类(反映数据库行),在代码引导程序中创建了200多个实例。 Each one has a single
SELECT
query with the only condition being WHERE 'tblA'.'AID' = #
, but I was thinking of creating a single query, that would parse 200 WHERE clauses connected by OR
logic, then from the result, 200 objects are created with the data already found, so there is only 1 query. 每个人都有一个
SELECT
查询,唯一条件是WHERE 'tblA'.'AID' = #
,但是我正在考虑创建一个查询,该查询将解析由OR
逻辑连接的200个WHERE子句,然后从结果200使用已找到的数据创建对象,因此只有1个查询。
I am implementing this on a test server at the moment, but I was wondering if this was a bad step for efficiency, and at what time it would be better to do 2 sets of queries, taking care of half the clauses each (or how ever many more need to be made)? 目前,我正在测试服务器上实施此操作,但我想知道这是否对提高效率是不利的一步,什么时候最好执行两组查询,每组都要处理一半的子句(或者如何处理还需要做更多吗?
Additional , I am also writing a performance enhancer into it to replace something like 另外 ,我也正在编写一个性能增强器以替换类似
WHERE `tblA`.`AID` = 2 OR `tblA`.`AID` = 3 OR `tblA`.`AID` = 5 OR `tblA`.`AID` = 6 OR `tblA`.`AID` = 7
with 同
WHERE (`tblA`.`AID` >= 2 AND `tblA`.`AID` <= 3) OR (`tblA`.`AID` >= 5 AND `tblA`.`AID` <= 7)
or even 甚至
WHERE `tblA`.`AID` >= 2 AND `tblA`.`AID` <= 7 AND `tblA`.`AID` <> 4
If you have a discrete list, then just use in
. 如果您有离散列表,则只需
in
使用。 . 。 .
。
where AID in (2, 3, 5, 6, 7, . . .)
And let the SQL engine worry about the optimization. 并让SQL引擎担心优化。
The biggest hit is likely to be the time to parse the query and sending a large query to the engine. 最大的损失可能是解析查询并将大型查询发送到引擎的时间。 If your list gets really long, then consider putting the list in a temporary table, building an index on the table, and doing a join.
如果列表真的很长,请考虑将列表放在临时表中,在表上建立索引,然后进行联接。
You don't specify what database you are using, but this advice is pretty database-agnostic. 您没有指定要使用的数据库,但是此建议与数据库无关。
You still haven't specified DBMS. 您仍未指定DBMS。
For SQL Server this might be modestly worthwhile (though you may well want to consider joining on a table valued parameter or similar rather than having a lengthy IN
list anyway). 对于SQL Server,这可能不值一提(尽管您可能很想考虑连接一个表值参数或类似参数,而不要使用冗长的
IN
列表)。
SQL Server will do separate individual seeks rather than collapse them into contiguous ranges. SQL Server将执行单独的查找,而不是将它们分解为连续的范围。 This is covered thoroughly in the article When is a Seek not a Seek?
文章何时寻求而不是寻求彻底覆盖了这一点。 but some examples below.
但下面有一些例子。
CREATE TABLE T (X int PRIMARY KEY)
INSERT INTO T
SELECT TOP 1000000 ROW_NUMBER() OVER (ORDER BY @@SPID)
FROM master..spt_values v1, master..spt_values v2
SET STATISTICS IO ON;
SELECT *
FROM T
WHERE X IN ( 1, 2, 3, 4, 5, 6, 7, 8, 9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,57,58,59,60,
61,62,63,64)
Table 'T'.
表“ T”。 Scan count 64, logical reads 192
扫描计数64,逻辑读取192
SELECT *
FROM T
WHERE X BETWEEN 1 AND 64
Table 'T'.
表“ T”。 Scan count 1, logical reads 3
扫描计数1,逻辑读取3
As mentioned in the comments on the article for greater than 64 values you will get a slightly different plan that adds a table of constants and a nested loops join into the mix. 如文章评论中提到的,对于64个以上的值,您将获得一个略有不同的计划,该计划将添加一个常量表,并且将嵌套循环加入到混合中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.