Non-Clustered Index scan issue - sql

I have a table on which I have created Non-Clustered Index, but in a query I am not able to make Table Seek on ipReq. As you can see in first image I have created index and also tried with different combinations. But didn't get success to make this table in seek. Due to this query my overall performance has been degraded. Let me know if anyone wants more information which I haven't added.

Related

SQL Server non-clustered index and the query optimizer

In one of the projects I am working on, there is a table which has about one million records. For better performance I created a non-clustered index and defined sid field as index key column. When I execute this query
SELECT [id]
,[sid]
,[idm]
,[origin]
,[status]
,[pid]
FROM [EpollText_Db].[dbo].[PhoneNumbers] where sid = 9
The execution plan is like the above picture. My question is, why does SQL server ignore the sid index and scan the whole one million records instead, to find the query result. Your help is greatly appreciated
I believe that the problem is in the size of your result. You are selecting ten thousand records from your database which is quite a lot if you consider the necessary query plan that would include index seek operation. The plan includes index seek would be something like this
Therefore, ten thousand key lookups would be included and a significant number of random logical accesses. Due to this, if your table row is small, he could decide to use clustered index scan. If you are really concerned about the performance of this query create a covering index:
CREATE INDEX idx_PhoneNumbers_sid
ON [EpollText_Db].[dbo].[PhoneNumbers](sid)
INCLUDE ([id],[idm],[origin],[status],[pid])
However, this may slow down inserts, deletes, and updates, and it may also double the size of your table.

Why does selecting a field change the Index Scan Type?

As you can see from the picture:
Query1 and Query2 are equal in tables and where clauses. But when I add a field from the Address table indexing goes from a Index Scan to a Table Scan. My question is why?
Note: I see recommended index, but I do not think I have the authority to change the database.
This is more than likely happening because StateProvCode is not a column in the PK of the Address table, nor is it an INCLUDE column. So SQL Server must be determining that it would be cheaper to simply scan the table, instead of scanning the PK and then doing additional lookups in the Address table to get the value of StateProvCode for each row. It's possible that your performance won't suffer all that much because scanning an index might only be a little faster than scanning the table (unless you have a filtered index in place). Of course, as you can see, you probably need to create an index to really improve performance.

How to get list of values stored in index?

I'm having this issue in Oracle 11g R2. Table containing not null column which is indexed with non unique index. The index is not containing other columns.
Then I assumed that if I query distinct values of the column from the table, it would use index to get different values of the column (sounds logical to me). However at least explain plan is telling me it's doing full table scan. Also it took some time so probably the plan was not changed during run time. Optimizer index hint didn't helped.
I tried to search answer for this but no luck. Is there way to get values stored in index or somehow query the table without "touching" the table at all (like multi column index joins can)?
Thanks!
EDIT: This was about Oracle EBS gl_balances table and gl_balances_n2 index. I got answer and this changed the explain plan:
select /*+ index_ffs(gl gl_balances_n2) */
distinct gl.period_name
from gl_balances gl;
It may not be more efficient to scan the index than to scan the table -- don't forget that the index segment also contains branch nodes, and each index entry has to contain a ROWID of about 16 bytes (if memory serves).
So a "fast full index scan", which is the plan you're looking to get, may not be as fast as a full table scan. (You'd use an index_ffs() hint for that, by the way.)
edit: It be possible to use a more exotic method
Maintaining your own list by periodically querying the table using DBMS_Scheduler.
A materialized view. Complete refresh on demand might be adequate, though barely better than just periodically querying the data and maintaining your own unique list.
Making the index compressed, though that would only be of value for longish index keys.
A bitmap index -- not for a concurrently modified table though.

Creating a clustered index on this heap?

So I am curious to know if it is worth creating a clustered index on a heap table that has about 30M rows of data. Before now, it wasn't going to be used in any application that we have but now we are creating an app to query that table.
The reason why I ask if it is worth it is because the application we are creating is basically doing this type of query.
SELECT *
FROM [table];
I am leaving the * in to represent that we are basically pulling all fields.
So my question is, is it worth creating a clustered index on a table that does not have one even though we are going to be selecting all fields and rows for our application?
Thanks for any info/advice.
No it is not worth it. If you are going to run a select without a where clause, a clustered index will just add more data to the Page files, depending on what you choose for your index(It all really depends on your data). Creating a larger scan of the table. A Heap table is the actual better performance wise in many situations(if you are just getting all rows from a table and not using joins/wheres/filter clauses of some sort), because it is stored in less page files.
Having a clustered index, when it isnt used will also bear some overhead in updating/creating stats on a table and doing inserts (page splits)
So if you arent going to use the index, and aren't going to filter on your table you are better off without the index

Missing Index Details SQL

I am tuning my SQL server and when I show my execution plan for one of my queries at the top it reads:
"Missing Index (Impact 99.7782): CREATE NONCLUSTERED INDEX..."
So I looked at the missing index details and it is showing this:
/*
Missing Index Details from ExecutionPlan1.sqlplan
The Query Processor estimates that implementing the following index could improve the query cost by 99.7782%.
*/
/*
USE [phsprod]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[address] ([userid])
GO
*/
I have only been working with SQL for about a month now and I have never done anything with this as all my tables have been built for me already. Can anyone help explain/give me any ideas on what to do with this? Thanks.
That means SQL Server is suggesting that your query could run faster with an index. Indexes add overhead and disk storage, so you should ignore this hint unless the query is giving performance problems in production.
To create the index, uncomment the statement after use, replace [<Name of Missing Index, sysname,>] with a real name, and run it:
USE [phsprod]
GO
CREATE NONCLUSTERED INDEX IX_Address_UserId
ON [dbo].[address] ([userid])
That means SQL Server is suggesting that your query could run faster with this index.
It can mean that your current indexes are not the greatest for the query you are running. Maybe your query could be optimised. Or maybe you COULD add the index. But if you decide to do this, you have to analyse carefully.
Indeed, indexes add overhead and disk storage. But, it can also improve performance.
For instance, if you always search in your table based on a "userid", then maybe it can payoff to add an index on that column, since SQL will be able to search usign this index.
Think a little bit of this like if you search for a word in a dictionnary.
If your looking for the word "dog", your going to search for "d" and then words that begin with "do" to finally find the word "dog".
If the words were not in alphabetical order in the dictionnary, you would have to search the whole dictionnary to find the word "dog"!
A clustered index (or a primary key) is the order of your columns.
Right now, it seems that you don't have an index on the column "userid". So SQL Server has (probably) to scan the entire table until he finds the userid.
If you add a nonclustered index, it will not re-order your table, but it will tell SQL Server between what range he should search to find the userid you want to. (Like "in the dictionnary, between page 20 and 30") So it will not have to search the whole table to find it.
But it also means that when you add new data to the table, or remove, or modify, he needs to keep his index up-to-date. Generally, a few indexes don't hurt, but you need to be sure they are needed. You don't want to add too much indexes as they can hurt performances if you add too much.
And if your table contains only a few hundreds of rows, maybe it won't show you a big improvement of performances. But over time, when your table grows, it may make a difference.
Hope that helps!