Analyze table usage for present data

Standard

Reading Time: 3 minutes

dataThe time I’ve spend analyzing tables for effective database modeling is just too much. I don’t like to do things twice and I’d rather spend a little more time to develop something smart than doing things over and over again. This was also the case where I had to analyze table usage for a set of tables in a databases.

The question asked was to see the minimum and the maximum length for all the columns in a table. Doing this is not that hard but making it effective to execute it against more than one column is a bit harder. Still not rocket science but needs a little more actions are needed to get things done.

The script below will take a few arguments

  • table_name
  • sample

The table_name argument is just the table name you want to analyze. You could change the query so it will execute against all the tables but that’s a little too far for the purpose of this script.

The sample argument allows you to take a subset of data instead of the entire table. The reason I did that was because I’m working with tables with more than a 100 million rows and that could take a long time before everything would’ve been read.

The script will take the amount of rows and put it in a temporary table. Than get all the columns from the specified table. After this it will create the queries needed to get all the lengths needed and make up a result.

At the end the result is combined with the information from the columns.

This will create the following result:

data_usage_result

In the example above you can see that several columns have a CHAR data type of length 40 but are unused and that costs data. By changing the column data type from CHAR to VARCHAR it was possible to save a lot of data especially if you’re dealing with millions of rows.

Hope this helps you out to analyze your tables a little faster.

First version PSSQLLib released

Standard

Reading Time: 1 minute

libraryUPDATE:

The scripts for the PSSQLLib are now on GIT!

It’s been long in the making and I’ve finally made a Powershell module (PSSQLLib) which makes my life a little easier when it comes to getting information from my instances.

The module in question makes it possible to get information like the databases, databse users, the privileges of the logins and the database users, the configution settings and a lot more from any instance.

The module is just a first release and new features can certainly be added. If you’re missing a feature, leave a comment and I’ll get it in there.

For more information about the Powershell module can be found here.

Hope you enjoy it!

Restore a database using Powershell

Standard

Reading Time: 3 minutes

/home/sstad/domains/sqlstad.nl/public html/wp content/uploads/2015/05/150504 backup restore

Recently I had a situation where I had to restore several full backups of multiple databases on a SQL Server instance daily. The databases would already be there and a simple restore with replace option would suffice. As I’m a fan of Powershell I wanted to restore a database using Powershell.

As lazy as I am I don’t want to make a script for each of the databases, I just want to supply a few parameters to a script and that the script does the rest for me.

I wanted the script to do the following:

  • Get the latest backup file from a directory
  • Restore the database with replace option
  • Be able to use the script in a SQL Server Agent job

The script would have to have the following parameters to do everything:

  • Directory: Directory to search through to get the latest file.
  • SQL Server instance: Which instance the server needed to restore to
  • Database: The database to restore to
  • Filter: Which extension to search for like “BAK”

Eventually the script looks like this

The catches are commented due the fact that the SQL Server job can’t cope with these kind of error messages. The script assumes that the database is already present. In a later version I will make it possible to adjust this accordingly but for now the functionality is just to execute a replace.

To make the job go to SQL Server and create a new job and job step and fill it in like below:

job_restore_step

Make sure the field Type is set on “Operating system (CmdExec)” and that the command is set up like this:

The parameters:

  • -d: Directory to search
  • -s: SQL Server instance
  • -db: Database to replace
  • -f: Filter

I used the “*.bak” filter to get all the bak-files but you could use a different extension if needed as long as it is for a full backup.

Replace the values between the brackets and give the job a spin.

 

 

Generate truncate script

Standard

Reading Time: 1 minute

truncate all the tablesLet me first say that I never truncate my production databases! It’s a bad practice and in most cases you don’t win anything. A good article about this: Stop Shrinking Your Database Files. Seriously. Now.

Besides the downsides of truncating your files I’ve had situations where I had to restore several databases for read-only purposes to a server.

When you deal with a lot of databases and want to shrink all the files you have to query the values or look at the properties of each database. This is not a efficient way to do this and therefore I’ve created the a little script to generate a truncate script to do everything for you.

For SQL Server 2000

For SQL Server 2005 and up

I left out the system databases because I didn’t want to mess with them.

Find columns with special characters

Standard

Reading Time: 3 minutes

Problem

im specialImagine a database where you want to know how many times a certain column contains a special character. The reason I had to do this was because I needed to know the impact of an export interface which wasn’t working properly with special characters.

But as lazy as I am as it comes to do repetitive tasks, I had to find a way I could easily get all columns with special characters in a data set without a lot of hassle.

The solution

Fortunately for me I can use the internal INFORMATION views of a database in SQL Server to get all the columns with their table, schema and type of data. Because this database only used varchar columns for saving text data I only had to search for this data type. You can easily extend the selecting by adding the neccesary types like TEXT, NVARCHAR etc.

To create the script I had to accomplish the following things:

  1. Retrieve all the colums that could contain text
  2. Set up a pattern to check the columns with
  3. Loop through the columns
  4. In every loop save the results
  5. Make up a data set of the data retrieved

I eventually got to this script:

This might take a while when you need to search through a lot of columns and records.  When the script you will get a result like below:

results specialcharacters

results specialcharacters

 

 

 

 

 

 

 

The pattern can contain any character that you want. I only searched for some of the most common special characters but any character will do.

I hope you find this post helpfull and I appreciate any comment.

SSRS permissions overview

Standard

Reading Time: 1 minute

Problem: SSRS Permissions Overview

I had to create a SSRS permissions overview with all the privileges set for each report, folder and other objects in SQL Server Reporting Services.

When you try to google anything like export permissions SSRS  you get tons of results where people have issues with the permissions.

I don’t have issues with the permissions I only want to get an SSRS permissions overview.

Solution

Just a simple T-SQL script which combins some of the tables which hold the values I needed.

The script below will show all the different objects in SSRS with their path and name with username and the privilege it has.

More info about the ReportServer database can be found here.

The image below shows how the query returns the results

results ssrs permissions

Results from SSRS permissions query

This is a fairly simple T-SQL script but it can help you out.

I always appreciate comments so if you have one feel free to share it.

Get ASCII characters from string

Standard

Reading Time: 2 minutes

I had a situation where I got an error converting a value to a decimal which had some white text and a valid number.

During the conversion I used the LTRIM and RTRIM function to remove any white spaces which were present at the beginnend and the end of the value. Unfortunately the characters in the string didn’t get removed with these functions. This let me to creating a little script to get the different character strings.

This gave me the following result:

result_asciivalues

I knew the ASCII value 32 is a space and these characters were removed during the process. But as you can see the values at the end are not spaces, tabs or any other kind of white space character.

This helped me find out that something went wrong with the export of the files which I would never have found without knowing the reason why the value didn’t convert to decimal.

For more information about the ASCII function you can follow this link

I hope this script can help you too.

Split large data file into smaller data files

Standard

Reading Time: 4 minutes

Problem

split_large_databasefileThe business uses a database which is 350 GB database in size and has only one data file and one transaction logfile. The previous DBA didn’t increase the amount of data files which lead to a large data file. Due to the size of the data file I got into trouble with the performance and restoring the database wasn’t a walk in the park either. So how can split a large data file into smaller data files.

What are the options?

  1. Do nothing and let the file grow when needed
  2. Split the file up into multiple data files

Solution number 1 is not a solution and will bite you in the long run because the database will grow so large it will not be easy to maintain anymore.

Solution number 2 will make sure the database has multiple files what could deliver an increase in performance and will be easier to restore. This solution can have be done in two ways:

  1. Have one file group with all the data files
  2. Work with multiple file groups and multiple files

In option number one you’ll have a single file group (the PRIMARY file group) which holds all the files. SQL Server will spread out the data over all the files in that file group equally.

In option number 2 you can make one or more file groups containing at least one data file where you can specifically move tables to.

Because I’m not 100% familiar with the performance per table and I didn’t have enough time to investigate this, I chose to use the PRIMARY file group and create several data files. I wanted to spread out the data over multiple data files because I wanted the original file to be emptied and the new files to be filled.

This was not a one-stop go action because I had to face several issues:

  1. When the data from the original file is moved to other files, the original data file maintains the original size.
  2. Because of issue 1 I had to create more space. I didn’t have the space on the original server and had to place the data elsewhere temporarily.
  3. Placing the files on another server or share isn’t possible because SQL Server doesn’t allow you to place database files on file shares
  4. Attaching an extra disk wasn’t possible in a timely fashion

Solution

To solve the issue that SQL Server doesn’t allow database files to be placed on file shares I enabled trace 1807. This trace bypasses several check of SQL Server from where it’s possible to place the files on file shares. See the code below:

The information about the trace flag was found at Varun Dhawan’s blog post which can be found here.

I created a file share on another server which pointed and gave the SQL Server service account modify rights on that share on NTFS and full control on the share.

I than created 10 data files to make sure the data files get a size around 35 GB each which is easy to handle.
TIP: Make sure the growth of the new data files is set to at least 1 GB because the continuous growth will have a negative impact on the performance of the data move.

To empty the original data file I used the following script:

This code will run for hours! Make sure there is enough space for the new data files to grow otherwise the process will exit. If the process in some way quits just run the shrinkfile command again and SQL Server will continue the move of data.

I used the script below to see how the files got filled:

This return the following result:

split_large_file_databasefiles

 

As you can see the new data files are filled proportionally with the data from the original data file. The transaction log-file is begin used because the data goes through the transaction log-file and than gets written to the new data files. In the same time the free space in the original data file increases.

When the process finished I was still having a large data file with a lot space available.  Because this was the primary file which contains all the system objects for the database I first shrunk the primary data file with the following command:

Because the file didn’t bother me I adjusted the initial size of the data file to be very small like 100MB and set it to have a maximum size of 100MB:

This will make sure the primary data file will never grow again and all the data that gets written will be written to the new data files.

Although the database works I wouldn’t want the files to be like they were spread across multiple locations. I detached the database to be able to move the data files around

… then moved the files from the fileshare to the databaseserver data directory and attched the database again where I needed to lookup the new datafiles because originally they were placed in an alternate location.

At last I had to clean up the changes I made by removing the file share and undo the trace. Undoing the trace is done with the following command

Conclusion

The process took a very long time. In this situation it took me more than 17 hours to get the database configured and to get the data moved.

Besides the fact that this took very long I’m glad with the result. The next step could be identity possible performance killer tables that can be placed in separate files on other file groups but that is a whole different article all together.

I hope you can use this information for your own actions and comments are always welcome!