Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

MS SQL Server Create Index

I have a proc that generates / suggests indexes that are needed.

I have an individuals table...
Went from 25000 to 600k+ records

And selects on where FirstName = '%%' simply bogged down to a timeout.

So...
IIT was suggested I add this
Create NonClustered Index IX_Individuals_missing_1852 On [EverywareV3].[dbo].[Individuals] ([FirstName]);

I executed and it has been running 2 3/4 hours.

Is there anything more efficient?

This is the code that generated the "missing" index.

SELECT   t.name AS 'affected_table' ,
            'Create NonClustered Index IX_' + t.name + '_missing_' + CAST(ddmid.index_handle AS VARCHAR(10)) + ' On ' + ddmid.statement + ' ('
            + ISNULL(ddmid.equality_columns, '') + CASE WHEN ddmid.equality_columns IS NOT NULL
                                                            AND ddmid.inequality_columns IS NOT NULL THEN ','
                                                        ELSE ''
                                                END + ISNULL(ddmid.inequality_columns, '') + ')'
            + ISNULL(' Include (' + ddmid.included_columns + ');', ';') AS sql_statement ,
            ddmigs.user_seeks ,
            ddmigs.user_scans ,
            CAST(( ddmigs.user_seeks + ddmigs.user_scans ) * ddmigs.avg_user_impact AS INT) AS 'est_impact' ,
            ddmigs.last_user_seek
FROM     sys.dm_db_missing_index_groups AS ddmig
            INNER JOIN sys.dm_db_missing_index_group_stats AS ddmigs ON ddmigs.group_handle = ddmig.index_group_handle
            INNER JOIN sys.dm_db_missing_index_details AS ddmid ON ddmig.index_handle = ddmid.index_handle
            INNER JOIN sys.tables AS t ON ddmid.object_id = t.object_id
WHERE    ddmid.database_id = DB_ID() AND t.NAME LIKE 'Individuals%'
--AND t.name = 'myTableName' 
ORDER BY t.name,CAST(( ddmigs.user_seeks + ddmigs.user_scans ) * ddmigs.avg_user_impact AS INT) DESC;

Open in new window

Avatar of HainKurt
HainKurt
Flag of Canada image

if you run queries like

where firstname like 'StartWith%'

then index will help... but in such queries

where firstname like '%contains%'

index will not help...
Avatar of Larry Brister

ASKER

But the index taking so long to apply is normal?
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is not prod...

It is an RDS Server on Amazon...

So
How do I open in single mode?
what sql you used to create the index...
what is your current table structure (columns, indexes etc)
maybe your memory and cpu is soooo weak :)
what is type of server
RDS Server Scaleable

Is it possible there is a lock on the table?

And how can I release it if so?
I am talking about server type

https://aws.amazon.com/rds/pricing/
RDS
Standard 64 bit
Version 11.0.2100.60
I mean what is Instance Type (vCPU      Memory (GiB)) on the server?
I found the problem...
It was a locked table session

I killed the session and was able to run my code in 3 minutes
Avatar of Vitor Montalvão
Those are system Views. They shouldn't be locked unless some maintenance tasks were running at the same time.
Check the schedule for the reindex task to see if it match the period where the script was also running.
It was a table that was locked... your answer was closest.