Normal day to day maintenance of large data volumes can be difficult. In a previous post I talked about how to handle DBCC CheckDB on a multi-terabyte database. Today I will tackle another issue I came across while dealing with multi-terabyte databases: index maintenance.
Like most of you, my absolute favorite solution when it comes to index maintenance is the Ola Hallengren solution. This is a perfectly written and solid maintenance solution, but I noticed it fails to pass the fragmentation checking stage of an index in VLDBs. In order to tame the VLDB beast, I wrote my own script for handling index maintenance. This script can be found here.
Maintenance solutions usually restart daily, even if not all indexes were reached during the maintenance window. To cope with this, we wrote a VLDB maintenance script which divides the full index maintenance work load over a few days by creating a few tables. These tables keep track of your index maintenance progress (Checks & Rebuild/Reorganizes), allowing you to check and rebuild all indexes. Only when all indexes have been checked, the maintenance cycle will start over.
Another issue is when the TLOG is larger in size than the index, the TLOG builds up and causes your disk to fill up with all the problems that come with it. For this reason, we built in a check into the script that skips the index if there’s insufficient available storage space at that moment or in total. Instead, the index will be reported in the reporting table, allowing you to check which indexes are not getting rebuild and try to get them rebuild in another way.
As with a normal script, I defined some parameters which you have to enter to create the index maintenance.
SET @ManagementDatabase = 'master' --> Database in which objects have to be created
This is be the database you will use to create the objects needed for the script to work.
SET @Database = 'WideWorldImporters' --> Database which will be maintained
This is the database on which the maintenance has to be done.
SET @AmountOfMemory = 30 --> Percentage of memory to be used for Online Rebuild Operations(0-100) (EnterpriseOnly)
This is the amount of memory available for the online rebuild to ensure you don’t use all your memory for one rebuild.
SET @ColumnStatisticsMaintenance = 'Yes' -->Do you want to maintain Column Statistics? (Yes/No)
If you also want to maintain the column statistics you will have to say yes here.
SET @MaxRunTime = 300 --> Amount of minutes the Maintenance can run each night (300 = 5hours)
The amount of minutes your script can run every night, after this amount of time no new statements will be launched.
SET @JobStartHour = 1 --> F.E. 1 job will start at 01:00 (possible options 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 24)
The start hour of your scheduled jobs, created by the script.
SET @LockTimeout = 360 --> Set the locktimeout in seconds
The amount of time in seconds a lock can be kept by de index rebuild/reorganize before cancelling the operation.
SET @WithOnline = 'YES' -->Rebuild indexes with the ONLINE rebuild option (YES OR NO)
This gives you the choice to use online rebuilds or not (in case of enterprise: I would suggest YES,but I had one case where it was NO )
The ColumnStatistics table contains the workload for all Column Statistics. The script uses this table to define whether or not it should resample the statistics. The IndexFragTable contains all information the script needs to rebuild or reorganize your indexes and where it should continue since its last rebuild. The IndexRebuildColumnStatLoggingTable is the most important table for you because it contains all information on failed rebuilds, error messages and unchecked indexes.
The dbo.IndexStatsOptimize is the main stored procedure which will be executed to handle you index maintenance. The dbo.Update IndexStatsTable updates your table, adds new indexes and resets after the cycle has been run.
This job will execute the stored procedures. If you need to make an alteration to your schedule, this is the place to go. Also, you can change your parameters here by altering the dbo.IndexStatsOptimizer stored procedure.
The script has been made AG-Aware, the only thing you have to do is execute it on both nodes.
If you made a mistake with your parameters, just reexecute the script. It will recreate all objects. If you made an error with the name of your database, then you have to manually remove all objects listed above.
The script will check daily for new tables and indexes. Add them to the list of indexes to be checked and rebuild. The script will also look for deleted tables and delete them from the list.
If you have any questions, feel free to ask. Thanks for reading and enjoy the script!
Stay tuned!
© 2023 Kohera
Crafted by
© 2022 Kohera
Crafted by
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |