Solved

Rebuilding Index and updating statistics using SQL SMSS

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

Accepted Solution

by:
Pawan Kumar Khowal 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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now