Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an sql index

How to select an sql index

I have an table TabEmp with fields c_ID (primary key), and c_Emp_ID.

I created an index on it idx_TabEmp (non-clustered) with the fields c_ID (primary key) and c_Emp_ID

I am using select statement

select * from index = idx_TabEmp 

It throws an error

Incorrect syntax near 'index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax

I am using

select * from TabEmp (index = idx_TabEmp)

it works, but I am not sure if that is the correct way to select an index

Can you please tell me the correct way to query an index?

like image 569
Fransis Avatar asked Dec 03 '10 15:12

Fransis


People also ask

How do I select an index in SQL?

Syntax: CREATE INDEX index ON TABLE column; where the index is the name given to that index and TABLE is the name of the table on which that index is created and column is the name of that column for which it is applied.

How do I select an index for a table in SQL Server?

You should build an index based on the predicates in the Where clause. For example, you can consider columns used in the Where clause, SQL joins, like, order by, group by predicates, and so on. You should join tables in a way that reduces the number of rows for the rest of the query.

How do I select an index column?

Columns with one or more of the following characteristics are good candidates for indexing: Values are unique in the column, or there are few duplicates. There is a wide range of values (good for regular indexes). There is a small range of values (good for bitmap indexes).


1 Answers

The index is something which the optimizer picks up "automagically - ideally you don't need to force select an index.

If you really want to force select the index, use index hint

SELECT *
FROM TabEmp
WITH (INDEX(idx_TabEmp))

Also, note that with no filter conditions (ie, no WHERE clauses), the Index does not come into the picture since you are not searching for a specific data - you're selecting everything.

To provide the book analogy - when you're reading a full book cover to cover - you don't need to look at the Index. It's only when you're searching for a specific page that you look at the index and find what you want.

like image 85
Sathyajith Bhat Avatar answered Sep 23 '22 07:09

Sathyajith Bhat