Wednesday, April 25, 2018

SQL Operations Studio - Query Editor and Source Control

https://www.mssqltips.com/sqlservertip/5375/sql-operations-studio--query-editor-and-source-control/

Problem
In my previous tip, I gave an overview of SQL Operations Studio which was introduced by Microsoft to work on cross-platforms and it contains many exciting features. In this tip, we will explore additional features of SQL Operations Studio. 
Solution
Pre-requisites: Please read the previous tip to get information about installation, an overview of the menu items, connection dialog box etc.

SQL Operations Studio Overview

In this tip, I will be using the February Public Preview release of SQL Operations Studio.
windows
You can find the detailed installation steps in the previous tip.
Once we launch SQL Operations Studio February Public Preview, we get the below welcome message.
sql operations
Now let's see what it has to offer.

Connection dialog box - SQL Operations Studio

In the February release of SQL Operations Studio, if we connect to an instance, we do not need to enter the database name manually, instead it provides the available databases as a drop-down list.  The previous releases did not provide the database drop-down list.
recent history
We can also find the recent connections in the recent history tab. This enables us to quickly connect to the previous connection.
Once we connect to the instance, we can either right-click on the connection followed by new query or click on a new query from the Tasks.
new query
A New Query window opens so we can write queries similar to SQL Server Management Studio.
sql query
We can see these options in the query editor window.
  • Run: To execute a query
  • Cancel: To cancel running query, if required.
  • Disconnect: We can change the existing connection.
  • Change Connection: Change database context from the drop-down list
  • Explain: To show estimated execution plan.

Query Editor Overview in SQL Operations Studio

Snippets

SQL Operations Studio has an extensive library of snippets built in. A snippet is a code template that has the basics for a command. We can speed up writing queries with these T-SQL Code snippets.
As soon as you start typing in the query editor it starts giving a suggestion as shown below.
sql query
It shows details about the selected operation to right. For example, if we select sqlCreateDatabase, it shows what this does and the T-SQL code to create a new database.
sql create database
Just hit the Tab key on the keyboard and it puts the code into a query editor.
database name
As we can see, 'DatabaseName' is highlighted, so we just need to give the 'DatabaseName' and run it. This will create the database for us with the default settings quickly.

Go to Definition and Peek Definition

Developers and database administrators need to view object definitions such as column names, primary key, foreign keys, etc. while working with objects. One way is to expand the table from the drop-down in the object explorer or open a separate query window and get information about the object.
SQL Operations Studio makes it even easier to view the definition of an object. Select the object for which we want to view the definition and right click to get options.
go to definition
Click on Go to definition or press F12 and a new query window will open with the definition of the object.
human resources
Here is the definition of the object:
select database
If we do not want a separate window to open and need to view definition inside the same query editor, click on Peek Definition or Alt+F12.
peek definition
create table
We can then review and copy columns from the window. 
To close the peek window, just click on the X in the upper right side of the peek window.
It also saves a copy of the definition in the temporary directory.
human resources
We can open the directory and see the object definition in a .sql file.
windows

Running Queries and Viewing Results

Now let's execute a query and view the output.
explain
Running a query in SQL Operation Studio looks similar to SQL Server Management Studio. It gives results below the query in the results pane.
There are few highlighted options to look at:
  • Explain: Once we click on Explain, it opens up the query execution plan. The format for the execution plans looks good with nice graphics.
select top
If we move mouse over any of the operators, it gives details about the operator.
full name
  • Export Results: Along with the query results, we can see it provides options to export data in different formats like CSV, JSON and Excel format.
Save a JSON, export data in JSON format as shown below:
results
The final option is View as Chart. It represents the data in different chart formats.
select top
We can choose from the below chart types and check out the following examples:
  • Count
  • Image
  • Pie
  • Bar
  • Horizontal Bar
  • Line
  • Scatter
  • Time series
chart type
Bar Chart Example
chart type
Pie Chart
pie chart
If we move mouse over the chart, we can get the details as well.
bar chart
We have the option to select the data direction (Vertical/horizontal) along with legend position.
bar chart
We can also copy and save the chart image if required.
Create Insight opens up JSON window as shown below. We will talk more about it future tips.
sql query

Source Control in SQL Operations Studio

SQL Operations Studio comes with a Git source control manager. We need to install Git (version 2.0.0 or later) to use these features.
If Git is not installed on the system, we get the below message to download Git.
download git
Also, in the Output Screen we can see that it looks for git in the location shown below.
terminal
Click on 'Download Git', it opens a web page to download the software.
git
Download the software and start the installation process.
general punlic license
Provide the installation folder.
select destination location
Select the components, we will use the default setup options here.
select components
Select the program shortcut for the start menu folder.
select start menu folder
Choose the default editor used by Git.
choosing the default editor
path environment
Choose HTTPS transport for the backend.
transport backend
Select the line ending conversions.
configuring conversions
In the next screen, we can configure extra options (i.e. enable file system caching, enable Git credentials manager, etc.).
enable file system caching
Start the installation process by clicking on Install.
installing
Once setup is completed, we get the below notification.
completing the git setup wizard
Now launch SQL Operations Studio and click on Source Control.
sql query
Now select the folder to use for the Git repository and click on Initialize Repository.
select folder
Git integration is now complete and we can see Source Control as GIT.
source control
We get multiple options by clicking on the ...  icon.
sql operations studio
Next Steps

New SQL Operations Studio Installation and Overview

https://www.mssqltips.com/sqlservertip/5339/new-sql-operations-studio-installation-and-overview/

Problem
SQL Server Management Studio is used as the default tool to connect to different SQL Server versions to manage SQL Server. Prior to SQL Server 2017, SQL Server Management Studio was only on the Windows platform. Microsoft recently launched a preview version of Microsoft SQL Server Operations Studio that runs on Windows, macOS, and Linux for SQL Server, Azure SQL Database, and Azure SQL Data Warehouse. In this tip, we will get an overview of SQL Operations Studio.
Solution
SQL Operations Studio is a free, light-weight tool for developers and administrators for SQL Server on Windows, Linux and Docker, Azure SQL Database and Azure SQL Data Warehouse on Windows, Mac or Linux machines. SQL Operations Studio is built on top of Visual Studio Code with the objective to make it highly extensible. It’s built on an extensible microservices architecture and includes the SQL tools service built on .NET Core.
SQL Operations Studio allows users to run command line tools such as PowerShell, BCP, SSH, Bash, etc. in the integrated terminal window inside the interface. It is also quite easy to view, generate and modify scripts with smart T-SQL code snippets and a rich graphical interface for database objects. DBAs can create customizable dashboards for monitoring which improves efficiency and quick turnaround for performance issues.
Before we move further, let's see how to install SQL Operations Studio.

SQL Operations Studio installation

SQL Operations Studio is currently in the January Public Preview. As mentioned earlier, SQL Operations Studio can be installed on Windows, Linux, MacOS as well. In this demo, we will have a look at the Windows version. You can find details for other OS installations in the Next Steps section.
Download the Windows installer from this link SQL Operations Studio (preview) installer for Windows.
Microsoft SQL Operations Studio Installable software
I have downloaded the Windows version for SQL Operations Studio and clicked on it to start the installation.
Microsoft SQL Operations Studio Installation
Click on the installation file to launch the setup process.
 SQL Operations Studio Installation
In next screen, accept the license agreement and click next.
Microsoft SQL Operations Studio Installation agreement accept
Select the SQL Operations Studio installation path, by default it will go to C:\program files\SQL Operations Studio.
Microsoft SQL Operations Studio Installation location
Select the Start Menu folder for SQL Operations Studio. If you don't want to create a start menu folder, click on the checkbox 'Don't create a Start Menu folder'.
Microsoft SQL Operations Studio Installation start menu folder
In the next screen, it will add the SQL Operations Studio folder path to the environment variable PATH. Please note that this will be available after system restart.
Microsoft SQL Operations Studio Installation PATH
To see the PATH setting, go to Edit the system environment variables.
Microsoft SQL Operations Studio Installation PATH view
It opens the system properties windows as shown below. Click on Environment Variables and edit the PATH variable.
Microsoft SQL Operations Studio Installation PATH view
Microsoft SQL Operations Studio Installation PATH view
You can see the SQL Operations Studio folder path in the PATH variable and then click OK.
Microsoft SQL Operations Studio Installation PATH view
Now click on Next and you can see the settings, go back if you want to change any setting. Click on Install to start the installation process.
Microsoft SQL Operations Studio Installation review
Set up is now starting the install.
Microsoft SQL Operations Studio Installation PATH view
Once set up is finished, by default, it will launch SQL Operations Studio.
Microsoft SQL Operations Studio launch

Overview of SQL Operations Studio

Once the installation is complete, we are now able to connect to SQL Server. The initial screen of SQL Operations Studio looks like the below image.
Microsoft SQL Operations Studio launch screen
Fill out the connection information and click Connect. Unlike other tools, this version doesn't provide a dropdown list of databases, so if we want to connect to a specific database, we can type the database name in the database name field.
We can also click on Advanced to configure more connection options such as timeout, encrypt connection, port number, connection pooling, failover partner, etc.
Microsoft SQL Operations Studio launch screen
I have filled out the basic details like server name, authentication method as Windows authentication. In my case, I don't want to connect to a specific database, so I have kept it blank.
Microsoft SQL Operations Studio launch screen
Once connected to the server, we can see the layout of SQL Operations Studio as below. I have numbered different areas of SQL Operations Studio to explain further below.
Microsoft SQL Operations Studio layout
Microsoft SQL Operations Studio layout section

(1) Object Explorer

This area shows the servers pane where all the server connections will be listed. We can expand the server similar to SSMS and can view databases (tables, schemas, views, functions, stored procedures, etc.), Security (logins, credentials, audits, linked servers logins, server roles, etc.) and Server Objects (endpoints, triggers, linked servers, etc.).
Microsoft SQL Operations Studio object explorer
We can also create groups for the servers.  One example is to group servers according to their roles production, QA, UAT, Dev, etc.
Click on the new server group.
Microsoft SQL Operations Studio new server group
It opens a window to define the server groups and also we can choose the group color as well. This makes it easy to identify the server group based on the color code as well
Microsoft SQL Operations Studio group color and details
We can see below that the group is highlighted with the color we choose while creating it.
Microsoft SQL Operations Studio group details
Now we can add the servers in their respective group by right click on group name followed by the new connection.
Microsoft SQL Operations Studio add servers in group
Fill out the connection details and server will be listed under the group.
Microsoft SQL Operations Studio server group connections

(2) Server Dashboard

Server Dashboard gives information about connected servers such as SQL Server Version, Edition, Computer Name and Operating System Version.
Microsoft SQL Operations Studio server dashboard

(3) Common Tasks

This area shows the common tasks to be performed. These common tasks are:
  • Restore: Shortcut to launch a database restore
  • Configure: Shortcut to configure your server. We will explore this with my future tips.
  • New Query: To run a query, perform query analysis including execution plan overview, etc.

(4) Search Pane

This section provides a easy way to find database objects from the list of databases. We can simply start typing and it narrows down the database objects to match what was typed.
Microsoft SQL Operations Studio search
If we are connected to a specific database, we can search objects such as tables, stored procedures, etc.
Microsoft SQL Operations Studio search objects

(5) Backup Status

This section provides backup status for the databases on the connected server. This is also very helpful information where we can quickly view the backup stats as:
  • How many backups completed in last 24 hrs
  • How many database backups are more than 24 hrs old
  • How many databases for which no backups are present
Microsoft SQL Operations Studio layout

(6) Database Size Graph

This section provides a glance of sizes of various databases individually for the database and transaction logs file size.
Microsoft SQL Operations Studio >DB Size graph
This shows a graphical representation of the data file and log file space. If we hover the mouse over the graph on a particular database, it shows the file sizes for that database.
Microsoft SQL Operations Studio >DB Size

(7) Status Bar

This status bar is an interactive status bar to display a handful of information along with some relative information.
Microsoft SQL Operations Studio Status bar
Below we can see the information, options in the status bar are:
  • Managed linked account: If we are connecting with Azure, it shows the account information and also we can add the account here as well.
  • Problems: If any problems occur such as a syntax error, execution error, etc. it will be noted here.
  • Connection information: Connection information such as server name and database name.
  • Cursor position: Shows the cursor position. Clicking on the current cursor position takes us to the specified line in the opened file.
  • Indentation: Indentation can be changed to spaces or tabs. Clicking on Indentation opens up the below window and we can change Indentation from the list of options provided.
Microsoft SQL Operations Studio Indentation
  • Encoding: We can also view and change the encoding from here. Clicking on encoding provides an option to reopen or save with encoding.
Microsoft SQL Operations Studio Encoding
If we click on Reopen with Encoding, it provides a list of encoding options.
Microsoft SQL Operations Studio Reopen with encoding
  • End of Line Sequence: We can change the End of Line sequence to either LF or CRLF. You can read this tip for more about End of Line sequence.
Microsoft SQL Operations Studio End of Line sequence
  • Language Mode: We can select from a list of different language options. By default, it is set to SQL.
Microsoft SQL Operations Studio Lanuage mode
  • Tweet feedback: Smiley face at the end provides you a possibility to quickly tweet feedback.
Microsoft SQL Operations Studio Tweet feedback

(8) Activity bar

The activity bar contains multiple tabs as shown below.
Microsoft SQL Operations Studio Activity bar
  • Server Panel: Displays the servers group and servers listed.
  • Task History: If we performed any activities such as backups, restores or other similar tasks, it shows the history of those tasks.
  • Explorer pane: The Explorer Pane contains a list of all open files in the editor. If we have any unsaved files as well, it highlights them so that we can save them, if required.
If we right click on the file name, it provides a list of options:
  • Reveal in Explorer opens up an Explorer window.
  • Open in Terminal opens up a terminal window in the lower half of the SQL Operations Studio, by default it is PowerShell, but we can also change to a Command Prompt or a BASH terminal.
Microsoft SQL Operations Studio options explorer pane
  • Open to the Side opens the file in split screen, so we can compare two files side by side.
Microsoft SQL Operations Studio compare files
  • Search: The Search pane opens up a pane that can be used to search, or search and replace, text in the current editor window.
  • Source Control Pane: This allows us to manage various files within your source code control system.
  • Settings: In the bottom lower side, we can get a setting menu. If we click on that, we get various interesting options.
Microsoft SQL Operations Studio Settings
  • Command Palette: We get multiple options out of command palette to ease our tasks.
Microsoft SQL Operations Studio Command Palette
  • It has inline search functionality, as soon as we type, it gives suggestions to choose from.
Microsoft SQL Operations Studio Command Palette search
  • Settings: Once we click on the Settings option, it provides a JSON editor with two screens: one for the default settings and another to override the default settings.
Microsoft SQL Operations Studio settings
  • Color theme: SQL Operations Studio has many more available color themes to choose from. Once we click on a color theme, it gives color theme options as:
Microsoft SQL Operations Studio color theme options
We can simply select the color theme and it immediately changes to that particular color theme.
Microsoft SQL Operations color theme layout
  • Keyboard shortcuts: We can get list of all keyboard shortcuts after click on this option.
Microsoft SQL Operations Studio Keyboard shortcuts
  • Checking for updates: If an update is available for SQL Operations Studio, it will show that update so that we can apply it to use the latest version.
Microsoft SQL Operations Studio updates

Summary

Currently, SQL Operations Studio is released as a Preview version. The tool is very useful and contains many interesting features. We will explore further on the SQL Operations Studio options such as executing a query, exploring execution plans, monitoring dashboards, etc. in future tips.

Friday, April 13, 2018

space used by tables

create table #TableSize (
    Name varchar(255),
    [rows] int,
    reserved varchar(255),
    data varchar(255),
    index_size varchar(255),
    unused varchar(255))
create table #ConvertedSizes (
    Name varchar(255),
    [rows] int,
    reservedKb int,
    dataKb int,
    reservedIndexSize int,
    reservedUnused int)

EXEC sp_MSforeachtable @command1="insert into #TableSize
EXEC sp_spaceused '?'"
insert into #ConvertedSizes (Name, [rows], reservedKb, dataKb, reservedIndexSize, reservedUnused)
select name, [rows],
SUBSTRING(reserved, 0, LEN(reserved)-2),
SUBSTRING(data, 0, LEN(data)-2),
SUBSTRING(index_size, 0, LEN(index_size)-2),
SUBSTRING(unused, 0, LEN(unused)-2)
from #TableSize

select * from #ConvertedSizes
order by reservedKb desc

drop table #TableSize
drop table #ConvertedSizes

or simply:

DECLARE @SpaceUsed TABLE( TableName VARCHAR(100)
      ,No_Of_Rows BIGINT
      ,ReservedSpace VARCHAR(15)
      ,DataSpace VARCHAR(15)
      ,Index_Size VARCHAR(15)
      ,UnUsed_Space VARCHAR(15)
      )
insert into @spaceused exec sp_MSForEachTable 'exec sp_spaceused [?]'
select * from @spaceused order by No_Of_Rows desc

Friday, February 23, 2018

Missing Index Script, Unused Index Scripts, Find Statistics of Whole Database, SQL Wait Statistics

Dear Friend,

In this email we will check out a few of the critical scripts related to performance tuning. You can DO IT YOURSELF.

If you face any issue or have further questions AFTER you have followed Steps listed in this email, you can always reply to this email. I read every single email and reply every single email in 24 hours. 

In this very first email we will see a few of the very important scripts related to SQL Server Indexes.

Note: Please try this on your development environment first before you execute them on your production database. Always take database backup before you change anything.
 

Step 1: Index and Statistics

1) Missing Index Scripts

Performance is often associated with Indexes. If your database is missing many indexes, you may want to execute a script from the following blog and create necessary missing indexes for your database tables. 
Missing Index Script


2) Unused Index Scripts

If you have unused indexes in your database, they will do more harm than helping your database. It is a good idea to remove them from your database. You may want to execute a script from the following blog and remove unnecessary indexes from your database tables. 
Unused Index Script


3) Find Statistics of Whole Database

Database statistics help SQL Server Engine to make appropriate decisions to use indexes. It is critical to understand the health of your statistics and script mentioned in the blog post displays all the details about your database statistics. 
Statistics Detail Script


Step 2: SQL Wait Statistics

If you want to improve performance of your SQL Server, it is critical to understand where is the performance bottleneck. I have written an entire month long series on the SQL Wait Statistics. With the help of SQL Wait Statistics, we can identify the performance bottleneck of the SQL Server. 
Identify SQL Wait Statistics

Once you identify the performance bottleneck with the above script, you can quickly search in the series of blog post and fix the issue by removing the bottleneck.


Step 3: Reply to This Email

Well,  if you have followed all the instructions so far, you should be able to fix your performance issues. If due to any reason, you need more help or want to talk to me, just hit reply to this email with your questions. Remember, I read every single email and reply within in 24 hours. Do not forget to include the result of above two steps in the excel along with your email. 
 

Professional Consultation

If you need further help you can take my professional help as well.

Comprehensive Database Performance Health Check
One stop solution for your SQL Server Performance Problems. 

SQL Server Performance Tuning Practical Workshop
No PPT, No Boring Theory – Just 100% Real World Scenario based DEMONSTRATIONS.
 
Thanks for reading it! 

Have a great day! 
~ Pinal from SQLAuthority