Join Simon Allardice for an in-depth discussion in this video Monitoring database size and integrity, part of SQL Server 2008 Essential Training.
Looking at index fragmentation, can give us a very specific idea of a tiny little piece that we want to keep an eye on and fix and maintain in our database, but what about the larger idea of the database itself and the consistency and integrity of the database? Well we can use some of those reports to get some very useful information. If I right-click any of my databases I can drop into my reports, my standard reports, and start off with something very simple and straightforward like the disk usage idea. How much space is this taking up? Getting this little pie chart that will tell me how much of it is data the purple stuff, how much of it is index, how much of it is unallocated and just being held back for new rows and new information.
And that's useful, but won't really guide you in any particular direction to doing anything. However, some of the other reports can be a bit more useful that way. We can drop into disk usage by top tables, allowing you to quickly scan which of your tables are taking up, both of them have a space and have the most rows in them, not necessarily the same thing. We can drop into our reports, and one of the ones that I like is the Disk Usage by Partition. Now we don't cover partitioning in this course. Partitioning is the ability to take large tables and actually split them across multiple physical file structures.
It's not something we can do with SQL Server Standard edition. It's only actually available in the Enterprise editions. But the report nonetheless gives us some interesting stuff to look at. Because it will show us all our tables and allow us to scan things like our indexes and how much space those take up. So here at the DimCustomer table 18,000 rows, 9,000 K, and I can scan the indexes as well, but seems to show some consistency. I've got 18,000 rows in them, but it shows me how space the indexes are taking up.
Dropping back in to the Reports we have things like view all our transactions. I haven't executed anything against this database for quite a while so there is nothing that's been run. But we would have that if we wanted. We've got user statistics, schema changes, the index usage and physical statistics we saw in the previous movie. I also have this one, Database Consistency History. Now the first time you run it, you're likely to see no messages at all. Because what it's telling you is this. It's a history of executions of DBCC CHECKDB.
Well what the devil does that mean? DBCC CHECKDB is a command that you can run if you're in the SysAdmin role and it does internal consistency checks on the database. Let me show you how you do it. Open up a new query window, making sure that AdventureWorksDW is selected or whatever database that I want to run this on, and I simply type DBCC CHECKDB. Execute. Depending on the site of your database it may take a while to do it. But when it comes back, it's going to run a whole bunch of internal checks on your tables and some of the background objects that support your tables.
And hopefully you will end up with CHECKDB found 0 allocation errors and 0 consistency errors in the database. Even if you're doing backups regularly, even if you're taking a look at your indexes and defragmenting those, you should be running this DBCC CHECKDB on a fairly regular basis, at least weekly. Possibly more often if you've got a very volatile database. Again like with your indexes this should be part of your enterprise level maintenance plan idea. But the nice thing about it is every time it's run, it is then stored in the log, and if I run my report again on the Database Consistency History, I should expect to see 2 instance of this and how long they took.
Now obviously we're starting to build very complex structures here. And a real deep discussion on what we should be doing for database monitoring and optimization is beyond the scope of this course, but there's an immense amount of material, both in Books Online and on the web, that you'll probably want to take a look at as you get deeper into your databases.
- Using T-SQL (Transact-SQL)
- Managing databases with SQL Server Management Studio
- Understanding database normalization
- Using SELECT statements
- Building indexes
- Monitoring database size and integrity
- Backing up and restoring databases
- Creating functions and stored procedures
- Managing database permissions
- Creating and formatting reports
- Adding charts to reports
- Creating and executing a simple SSIS package
Skill Level Beginner
Q: I'm having problems installing the free Express R2 version of SQL Server on Windows XP. I tried 64-bit and 32-bit versions. In the videos, the author installs from a DVD. Do I need to do the same?
Q: The link to the installer for the AdventureWorks sample database, as shown in the Chapter 2 movie "Installing sample databases," no longer works. Where can I find the installer?
A: Microsoft has reorganized its site. The sample files are still there, but they're a bit harder to find. To install them:
1) Visit http://msftdbprodsamples.codeplex.com/.
2) Click the link to "SQL Server 2008 R2 OLTP."
3) Click the AdventureWOkrs2008R2 Data File link and agree to the conditions to download the MDF file.
4) Move the MDF file to your SQL Server Directory, usually located at C:\Program Files\Microsfot SQL Server\MSSQL 10_50.MSSQLSERVER\MSSQL\DATA.
5) Open the SQL Sever Management Studio and connect to your instance using an account with administrative privileges.
6) Attach the sample database by right-clicking the Databases folder in the Object Explorer and choosing Attach from the pop-up menu.
7) Click the Add button in the next menu and navigate to the MDF file in the Locate Database Files window that appears. Select it and click OK.
8) Remove the reference to the log file in the "AdventureWorks2008R2" database details: pane by selecting the Log entry and clicking removing.*
9) Click OK to return to SQL Server Management Studio and complete the attachment process.
*MDF files are the "data" files for SQL Server databases. They often come along with LOG files (ldf files). This one didn't so we need to REMOVE the reference to the non-existent log file. Select the second row in the lower section (it should say File Type: Log and Message: Not Found) and click the REMOVE button.
For an illustrated version of these instructions (with screenshots), click here for a PDF version.