?
Solved

Rebuilding Index and updating statistics using SQL SMSS

Posted on 2016-10-19
4
Medium Priority
?
91 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 1200 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 51

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 800 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.
Suggested Courses

762 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