gpt4 book ai didi

sql - 如何避免大in子句?

转载 作者:行者123 更新时间:2023-12-03 16:28:22 25 4
gpt4 key购买 nike

我有 3 张 table :

table_product (30 000 row)
---------
ID
label

_
table_period (225 000 row)
---------
ID
date_start
date_end
default_price
FK_ID_product


table_special_offer (10 000 row)
-----
ID
label
date_start,
date_end,
special_offer_price
FK_ID_period

所以我需要从所有这些表中加载数据,所以这就是我要做的:
1/像这样从“table_product”加载数据
select *
from table_product
where label like 'gun%'

2/像这样从“table_period”加载数据
select *
from table_period
where FK_ID_product IN(list of all the ids selected in the 1)

3/像这样从“table_special_offer”加载数据
select *
from table_special_offer
where FK_ID_period IN(list of all the ids selected in the 2)

正如您可能认为第 3 点中的 IN 子句可能非常非常大(例如 75 000 大),因此我有很多机会获得超时或诸如“已达到表达式服务限制”之类的情况。

你有没有遇到过这样的事情,你是如何避免的?

附注:
上下文:SQL Server 2005、.net 2.0
(请不要告诉我我的设计不好,或者我不应该做“select *”,我只是简化了我的问题,所以它比描述我的业务的 500 页要简单一些)。

谢谢。

最佳答案

切换到使用连接:

SELECT <FieldList>
FROM Table_Product prod
JOIN Table_Period per ON prod.Id = per.FK_ID_Product
JOIN Table_Special_Offer spec ON per.ID = spec.FK_ID_Period
WHERE prod.label LIKE 'gun%'

您应该注意的是 IN 与 JOIN 与 EXISTS 的区别 - great article here.

关于sql - 如何避免大in子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4254872/

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