SQL中的强制执行顺序
我在某些条款中有一组条件,例如
WHERE id > 123456
AND type LIKE 'type%'
问题,我希望查询首先运行整数比较,因为它显着减少了字符串比较需要运行的数据量。
但是,优化器选择首先运行字符串比较,从而迫使其运行非常缓慢。
有没有办法迫使我的Where子句的执行顺序?
I have a set of conditions in my where clause like
WHERE id > 123456
AND type LIKE 'type%'
The issue is that I would like the query to run the integer comparison first, as it signficantly decreases the amount of data that the string comparison needs to run.
However, the optimizer chooses to run the string comparison first forcing it to run extremely slowly.
Is there a way to force the execution order of my where clause?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道强迫它以一定顺序运行,但是如果您使用CTE怎么办?
很刺耳,但也许可行?
I don't know about forcing it to run in a certain order but what if you used a cte?
Pretty hacky but maybe workable?
SQL是声明性语言,因此通常不允许您指定条件执行顺序。但是,您有几种可能性:
ID
上定义索引,而不是type
ID> ID
和类型
和确保ID
列比类型
列更具选择性(缩小结果)。您尚未描述您的数据。/*+ index(...)*/
提示执行适当的索引,请指定您的DB供应商,并解释查询计划以获取更精确的答案。
SQL is declarative language hence it, in general, does not allow you to specify order of execution of conditions. You have several possibilities though:
id
and not ontype
id
andtype
and ensureid
column is more selective (narrowing result) thantype
column. You haven't described your data./*+ INDEX(...)*/
hint to enforce proper indexPlease specify your db vendor and explain plan of your query for more precise answer.