Solved

Rebuilding Index and updating statistics using SQL SMSS

Posted on 2016-10-19
4
70 Views
Last Modified: 2016-10-20
I'm not an expert on SQL, but have to rebuild index and update statistics on a database.

When I search the steps to do, nearly all of them are running manual codes to do it and they are all different.
So, I created a SMSS maintenance plan to do the rebuild index and update statistics. And I will run it on the scheduled maintenance time this Friday. It will probably take a lot of time since I haven't done it for a few years. The reason why it is, because when I run it, it fails. I think it's because users access database. I have to block all connections to SQL server.

Questions:
1.How to block all SQL connections      so that I can do rebuild index and update statistics alone? I have to do this at midnight from home, and can't disconnect network connection, have to block only SQL connections and stay offline.


2.I see an option in the SQL maintenance job I created, but not sure this is the only option I can do.

3.Can I take the database offline by 'SSMS / Tasks/ Take Offline' and rebuild index?


sql
The server is Windows 2012 STD and SQL 2008 STD
0
Comment
Question by:crcsupport
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 300 total points
ID: 41851365
This is the best option you have got. Automate it and schedule.

I see an option in the SQL maintenance job I created, but not sure this is the only option I can do.

Get you maintenance solution from

https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html

Note - It is FREE
0
 
LVL 1

Author Comment

by:crcsupport
ID: 41851368
Hi, Pawan,
I successfully rebuilt index, but the fragmentation level doesn't drop in running this script. Do you know why? I checked one of the table's properties window, it shows 0% fragmented, though. And the Page fullness shows 99%.

Code to check fragmentation;
-- Check fragmentation on db(dbname)

Declare @dbid INT
Set @dbid =DB_ID('dbname')

select dbschemas.[name] as 'Schema', 
                dbtables.[name] as 'Table', 
                dbindexes.[name] as 'Index',
                indexstats.avg_fragmentation_in_percent,
                indexstats.page_count
from sys.dm_db_index_physical_stats (@dbid, NULL, NULL, NULL, NULL) as indexstats
                INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
                INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
                INNER JOIN sys.indexes as dbindexes on dbindexes.[object_id] = indexstats.[object_id]
                                AND indexstats.index_id = dbindexes.index_id
where indexstats.database_id = @dbid
order by indexstats.avg_fragmentation_in_percent desc

Open in new window


After rebuildindex finished, two different statistics and page fullness 99%
2016-10-19-22_05_10-SQL-How-to-check.png2016-10-19-22_04_26-DC2-on-WIN2012-S.png
0
 
LVL 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 200 total points
ID: 41851565
What's wrong with the Page Fullness? That value you want it to be the nearest from 100% possible.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 41852472
I'm going to close this thread, I opened duplicates.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question