具有多个索引的 SQL 查询 - SQL Server 2000
我使用类似的查询,这样
select.....from.. with... (INDEX=IX_TABLE_1, INDEX=IX_TABLE_2)...
我收到以下错误
每个表只有一个索引提示列表 是允许的
这似乎与 SQL Server 2005 配合良好。这是 SQL Server 的问题吗?
I use a similar query like this
select.....from.. with... (INDEX=IX_TABLE_1, INDEX=IX_TABLE_2)...
I get the following error
Only one list of index hints per table
is allowed
This seems to be working well with SQL Server 2005. Is it an issue with SQL server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这可能是由于您使用的语法所致。
而不是 (INDEX=IX_TABLE_1, INDEX=IX_TABLE_2),请尝试:
我认为事实上您有 2 个“INDEX=”部分。
另外,我建议只使用索引提示作为最后的手段,因为查询优化器通常应该选择要使用的最佳计划/索引。这就是为什么通常您应该信任优化器。如果您确实使用索引提示,那么最好经常查看它们,因为它们可能会随着时间的推移而变得更糟(例如,随着数据量的增长,最初使用提示表现更好的内容可能会开始表现更差)。
I think it may be because of the syntax you're using.
Instead of (INDEX=IX_TABLE_1, INDEX=IX_TABLE_2), try:
I think it is the fact you have 2 "INDEX=" parts.
Also, I would recommend only using index hints as a last resort as the query optimiser should generally choose the best plan/indexes to use. This is why generally, you should trust the optimizer. If you do use index hints, it's a good idea to review them fairly frequently as they may become worse over time (e.g. as data volumes grow, what did originally perform better with the hint, may start performing worse).
实际上,您不应该首先给出索引提示。
解释
旁注
如果优化器似乎在使用哪些索引方面做出了愚蠢的选择;这通常需要进一步调查。
其次,请确保优化器不会拒绝特定索引,因为事实上该索引会降低性能。例如,您可能会想要执行以下操作之一:
索引提示看起来完全合乎逻辑;但是:
最后
我不确定是否是这样;但你的问题并不表明它是一个复杂的多表查询。那么它实际上可能像下面这样微不足道?
无论什么情况,为单个表提示多个索引肯定没有任何意义(除非通过自联接多次使用)。你说:
我不得不问:你确定吗?
我尝试了一下;尽管它没有导致错误消息,但它严重迷惑了优化器。它迫使优化器遍历同一个表两次(不必要)并将结果集相互连接 - 产生巨大的开销!
Actually, you shouldn't be giving index hints in the first place.
Explanation
Side Note
If the optimser appears to be making a silly choice as to which indexes to use; this generally warrants further investigation.
Secondly, be sure that the optimiser isn't rejecting a particular index because in actual fact said index reduces performance. For example, you might be tempted to do one of the following:
The index hints seem perfectly logical; however:
Finally
I'm not sure if it is the case; but your question doesn't indicate it being a complex multi table query. So it may in fact be as trivial as follows?
Whatever the situation, it certainly doesn't make any sense to hint multiple indexes for a single table (unless it is used multiple times with self-joins). You said:
And I have to ask: Are you sure?
I tried it out; and as much as it didn't cause an error message - it seriously confused the optimiser. It forced the optimiser to traverse the same table twice (unnecessarily) and join the result sets back to each other - incurring a tremendous overhead!!