如何在SQL查询中使用索引 [英] How to use index in SQL query

查看:2401
本文介绍了如何在SQL查询中使用索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我对这些东西不熟悉..我在开始时在我的SP中创建了一个索引,如下所示

 创建索引index_fab 
ON TblFab(Fab_name)

现在我有查询

 从TblFab中选择fab_name,其中artc ='x'和atelr ='y'。 

now是否有必要在select子句中使用此索引名称,否则它将自动用于加速查询



我是否必须使用类似

 从TblFab中选择fab_name WITH(INDEX(index_fab))其中artc ='x'和atelr ='y'。 

或在查询中使用此索引的任何其他方法



以及如果我们在这张桌子上使用连接时如何使用索引?

解决方案

首先,你是说你的意思吗?在存储过程中创建索引?这是一个坏主意 - 如果您运行存储过程两次,它将失败,因为索引已经存在。



其次,您的查询不使用索引中提到的列,因此它不会产生任何影响。



<第三,正如JodyT所写,查询分析器(SQL Server本身)将决定使用哪个索引;它几乎肯定比你更好。



最后,为了加快你提到的查询速度,在artc和atelr列上创建一个索引。


Well i am new to this stuff ..I have created an index in my SP at start like follows

Create Index index_fab
ON TblFab (Fab_name)

Now i have query under this

select fab_name from TblFab where artc = 'x' and atelr = 'y'.

now Is it necessary to use this index name in select clause or it will automatically used to speed up queries

Do i have to use something like

select fab_name from TblFab WITH(INDEX(index_fab)) where artc = 'x' and atelr = 'y'.

or any other method to use this index in query

and also how to use index if we are using join on this table?

解决方案

Firstly, do you mean you're creating the index in a stored procedure? That's a bad idea - if you run the stored procedure twice, it will fail because the index already exists.

Secondly, your query doesn't use the column mentioned in the index, so it will have no impact.

Thirdly, as JodyT writes, the query analyzer (SQL Server itself) will decide which index to use; it's almost certainly better at it than you are.

Finally, to speed up the query you mention, create an index on columns artc and atelr.

这篇关于如何在SQL查询中使用索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆