page_type | languages | products | description | ||
---|---|---|---|---|---|
sample |
|
|
Azure SQL - Getting Started Samples |
In this tutorial you will learn how to create a database, use Go to connect to it, and improve performance with columnstore indexes.
Regardless of platform or operating system, you can use Azure SQL. As you can see the tutorial is available to be used with:
Once you've gotten familiar with Azure SQL and Go, you can take advantage of several samples. The samples will help you to create Full-Stack solutions or Back-End APIs that can be used in projects of any size and scale.
All of these examples require an Azure SQL database. You can follow these instructions to create one.
For instructions on how to set up an Azure SQL database, see Azure SQL Database.
Set up your machine using the instructions for your OS by clicking on the links below, then return here to complete the tutorial.
-
Using the Azure portal, go to your database and look in the panel on the left. Under the settings subcategory, find "connection strings".
-
Click Connection Strings, and then take note of the information:
Server=tcp:your_server.database.windows.net,1433;Initial Catalog=your_database;Persist Security Info=False;User ID=your_user ;Password=<THIS IS ACTUALLY NOT RETURNED>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
-
Make a note of the following somewhere for reference in subsequent steps:
Server=your_server.database.windows.net Database=your_database
In order to connect to your Azure SQL database, you will need to create a filrewall rule on the target server. This allows your application to talk to your Azure SQL Database.
- From your database, in the Overview, navigate to Server name.
- From the server, in the search bar at the top, type firewall, and select Firewalls and virtual networks.
- Select + Add Client IP.
- Select Save.
Create a new folder for your project called AzureSqlSample and switch to that folder from the command line.
Next we will create a Go app that connects to Azure SQL database.
Create a copy of the file connect.go in the AzureSqlSample folder. This sample uses the GoLang Context methods to ensure that there's an active connection to the database server. Make sure to update the connection information.
Prepare and run your Go app from the AzureSqlSample folder in the terminal.
go mod init azure-demo
go mod tidy
az login
go run connect.go
Create a file called CreateTestData.sql in the AzureSqlSample folder. Copy and paste the following the T-SQL code into it. This will create a schema, table, and insert some sample rows.
CREATE SCHEMA TestSchema;
GO
CREATE TABLE TestSchema.Employees (
Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Name NVARCHAR(50),
Location NVARCHAR(50)
);
GO
INSERT INTO TestSchema.Employees (Name, Location) VALUES
(N'Jared', N'Australia'),
(N'Nikita', N'India'),
(N'Tom', N'Germany');
GO
SELECT * FROM TestSchema.Employees;
GO
From the AzureSqlSample folder, connect to the database using sqlcmd and run the SQL script to create the schema, table, and insert some rows.
az login
sqlcmd -S your_server.database.windows.net -G -d your_database -i ./CreateTestData.sql
Create a copy of crud.go in the AzureSqlSample folder. Update your connection information. This will insert, update, delete, and read a few rows.
Prepare and run the 'crud.go' app from the AzureSqlSample folder in the terminal.
go mod init AzureSqlSample
go mod tidy
az login
go run crud.go
Create a new folder for your project called AzureSqlGormSample and switch to that folder from the command line.
Paste the contents of this file: orm.go into a new file in your folder. Name the file orm.go. Make sure to replace the connection information.
Prepare and run the 'orm.go' app from the AzureSQLGormSample in the terminal.
go mod init AzureSqlGormSample
go mod tidy
az login
go run orm.go
In this section we will show you an example of Columnstore Indexes and how they can improve data processing speeds. Columnstore indexes can dramatically improve performance on analytical workloads, and offer better data compression than traditional rowstore indexes.
This example creates a table with 3 million rows and then uses a Go application to query that data, capturing the run time before and after adding a columnstore index.
Create a new folder for your project called AzureSqlColumnstoreSample and switch to that folder from the command line.
Create a new file called CreateSampleTable.sql in the folder AzureSqlColumnstoreSample. Paste the T-SQL code below into your new SQL file. Save and close the file.
WITH
a
AS
(
SELECT *
FROM (VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) AS a(a)
)
SELECT TOP(3000000)
ROW_NUMBER() OVER (ORDER BY a.a) AS OrderItemId,
a.a + b.a + c.a + d.a + e.a + f.a + g.a + h.a AS OrderId,
a.a * 10 AS Price,
CONCAT(a.a, N' ', b.a, N' ', c.a, N' ', d.a, N' ', e.a, N' ', f.a, N' ', g.a, N' ', h.a) AS ProductName
INTO Table_with_3M_rows
FROM a, a AS b, a AS c, a AS d, a AS e, a AS f, a AS g, a AS h;
From the AzureSqlColumnstoreSample folder, connect to the database using sqlcmd and run the SQL script to create the table with 3 million rows. This may take a few minutes to run.
az login
sqlcmd -S your_server.database.windows.net -G -d your_database -i ./CreateSampleTable.sql
Leave the terminal window open for the duration of this exercise to avoid having to run 'Az Login' multiple times.
In the AzureSqlColumnstoreSample folder, create a copy of this file: columnstore.go. Make sure to replace the connection information.
Prepare and run your Go app from the AzureSqlColumnstoreSample folder in the terminal.
go mod init AzureSqlColumnstoreSample
go mod tidy
go run columnstore.go
In the AzureSqlColumnstoreSample folder, run this command to create a columnstore index on your table:
sqlcmd -S your_server.database.windows.net -G -d your_database -Q "CREATE CLUSTERED COLUMNSTORE INDEX Columnstoreindex ON Table_with_3M_rows;"
Re-run your Go app from the AzureSqlColumnstoreSample folder in the same terminal window.
go run columnstore.go
Once you are finished with your code, remember to clean up your folders.