Solved

Rebuilding Index and updating statistics using SQL SMSS

Posted on 2016-10-19
4
79 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 49

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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…

726 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