Solved

Rebuilding Index and updating statistics using SQL SMSS

Posted on 2016-10-19
4
73 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 48

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

828 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