John McCormack DBA

SQL Server Databases and Cloud

  • Personal
    • About
  • Free Training
    • SQL Server on Amazon RDS (Free Course)
    • Free practice questions to help you pass DP-900
  • Save money in Azure
    • Azure IaaS SQL Backups – Stop burning money
    • Your Azure SQL Database and Managed Instance is too big
    • Turn the cloud off at bedtime to save 70%
    • Your Azure SQL Virtual Machine might be too big
    • Save money with Azure SQL DB serverless
    • Save up to 73% with reserved instances
    • Delete unused instances to save money in Azure
  • Hire me
    • 60 minute cost optimization
    • Let me solve your SQL Server problems
    • Take a look at my Sessionize speaker’s profile

Search Results for: aws

Glasgow Super Meetup – AWS Athena Presentation

26th October 2018 By John McCormack 1 Comment

The Glasgow Super Meetup was a joint event between Glasgow Azure User Group, Glasgow SQL User Group and Scottish PowerShell & DevOps User Group. I did an AWS Athena Presentation to the group.

Speaking about AWS Athena at the Glasgow Super Meetup might seem like an odd choice since most attendees will use Azure heavily or be more interested in SQL Server, however I was pleasantly surprised by the interest that people took in the subject matter. It was only a lightning talk so there wasn’t time to answer questions however I was asked a number of questions during the break by attendees.

I showed how tables can be archived out of the database and into S3, at a fraction of the price yet the data can still be queried if needed using Athena. I stressed that Athena isn’t intended as a replacement for an RDBMS and as such, queries will be slower than SQL Server however it is much cheaper to store large amounts of data in flat files in object storage (such as S3), rather than  expensive block storage which is used with databases. So if the use case fits, such as infrequently accessed archive data, then it is something to consider. I’ve uploaded my slides and also linked to a recording of the event. If you want to try the code, you’ll find it below.

Slides Recording

Demo

Description

As a proof of concept, I want to export the data from the Sales.SalesOrderHeader table in Adventureworks2012 to flat files using BCP. The data would be partitioned into unique days using the OrderDate column. This data is then exported to the local file system and then uploaded to Amazon S3. The next steps include creating a table in Athena, querying it to review the data and validating the correct data has been uploaded.

Code

  1. Run select query with dynamic sql to generate PowerShell and BCP command. (Run query then select/copy full column and paste into PowerShell)
    1. SELECT DISTINCT
      OrderDate,
      'New-Item -ItemType directory -Path C:\Users\jmccorma\Documents\SQL_to_S3_Demo\Output_Files\year='+CONVERT(varchar(4), OrderDate, 102)+'\month='+CONVERT(varchar(2), OrderDate, 101)+'\day='+CONVERT(varchar(2), OrderDate, 103)+' -ErrorAction SilentlyContinue' as PoSH_command,
      'bcp "SELECT SalesOrderID, RevisionNumber, OrderDate, DueDate, ShipDate, Status, OnlineOrderFlag, SalesOrderNumber, PurchaseOrderNumber, AccountNumber, CustomerID, SalesPersonID, TerritoryID, BillToAddressID, ShipToAddressID, ShipMethodID, CreditCardID, CreditCardApprovalCode, CurrencyRateID, SubTotal, TaxAmt, Freight, TotalDue, Comment, rowguid, ModifiedDate FROM [AdventureWorks2012].[Sales].[SalesOrderHeader] WHERE OrderDate = '''+convert(varchar, OrderDate, 23)+'''"
      queryout "c:\users\jmccorma\Documents\SQL_to_S3_Demo\Output_Files\year='+CONVERT(varchar(4), OrderDate, 102)+'\month='+CONVERT(varchar(2), OrderDate, 101)+'\day='+CONVERT(varchar(2), OrderDate, 103)+'\SalesOrderHeader.tsv" -c -t\t -r\n -T -S localhost\SQLEXPRESS' as bcp_command
      FROM [AdventureWorks2012].[Sales].[SalesOrderHeader]
  2. Highlight column PoSH_command, copy and then paste into Powershell window
  3. Highlight column bcp_command, copy and then paste into Powershell or command window
  4. Upload from local file system to AWS S3. You must have an S3 bucket created for this and you must have configured an IAM user in AWS to do this programatically. You can upload manually using the AWS console if you prefer.
    • aws s3 sync C:\SQL_to_S3_Demo\Output_Files s3://athena-demo-usergroup/ Change to your local file location and your s3 bucket
  5. Create database and table in Athena (copy code into AWS console) and load partitions
    • CREATE DATABASE adventureworks2012;
    • -- Athena table created by John McCormack for Glasgow User Group
      CREATE EXTERNAL TABLE `SalesOrderHeader`(
      `SalesOrderID` INT,
      `RevisionNumber` TINYINT,
      `OrderDate` TIMESTAMP,
      `DueDate` TIMESTAMP,
      `ShipDate` TIMESTAMP,
      `Status` TINYINT,
      `OnlineOrderFlag` BOOLEAN,
      `SalesOrderNumber` STRING,
      `PurchaseOrderNumber` STRING,
      `AccountNumber` STRING,
      `CustomerID` INT,
      `SalesPersonID` INT,
      `TerritoryID` INT,
      `BillToAddressID` INT,
      `ShipToAddressID` INT,
      `ShipMethodID` INT,
      `CreditCardID` INT,
      `CreditCardApprovalCode` STRING,
      `CurrencyRateID` INT,
      `SubTotal` DECIMAL(12,4),
      `TaxAmt` DECIMAL(12,4),
      `Freight` DECIMAL(12,4),
      `TotalDue` DECIMAL(12,4),
      `Comment` STRING,
      `rowguid` STRING,
      `ModifiedDate` TIMESTAMP
      )
      PARTITIONED BY (
      `year` string,
      `month` string,
      `day` string)
      ROW FORMAT DELIMITED
      FIELDS TERMINATED BY '\t'
      ESCAPED BY '\\'
      LINES TERMINATED BY '\n'
      LOCATION
      's3://athena-demo-usergroup/'
      TBLPROPERTIES (
      'has_encrypted_data'='false',
      'skip.header.line.count'='0')
    • MSCK REPAIR TABLE salesorderheader;
  6. Run these queries in SSMS and Athena to review the data is the same
    • This performs a row count and checks the sum of one particular column(territoryid). This is fairly rudimentary check and not guaranteed to be unique but it is a simple way of having a degree of confidence in the exported data.
    • -- Validate Athena data is correct
      -- Athena
      SELECT COUNT(*) as row_count,SUM(territoryid) as column_sum FROM "adventureworks2012"."salesorderheader"
      WHERE year='2014'
      AND month = '01'
      AND day = '23';-- SQL Server
      SELECT COUNT(*) as row_count,SUM(territoryid) as column_sum FROM adventureworks2012.sales.salesorderheader
      WHERE OrderDate = '2014-01-23 00:00:00.000'
  7. Now it is uploaded, you can query any way you like in Athena. It is worth noting that partitioning improves the performance of the query and makes the query cheaper because it scans less data. If partitioning data, you should use the partition key in your query otherwise it will scan all of data. Note the difference between the 2 queries below.
    • -- Not using partition (12 seconds - scanned 7.53MB)
      SELECT * FROM "adventureworks2012"."salesorderheader"
      WHERE OrderDate = CAST('2014-01-23 00:00:00.000' as TIMESTAMP);-- Using Partition (1.8 seconds - scanned 15.55KB - 1/6 of the duration and 1/495 of cost)
      SELECT * FROM "adventureworks2012"."salesorderheader"
      WHERE year='2014'
      AND month = '01'
      AND day = '23';

Further resources:

  • https://aws.amazon.com/athena/ 
  • https://aws.amazon.com/s3/

Filed Under: AWS, AWS Athena, front-page

Benefits and limitations of AWS RDS

23rd July 2020 By John McCormack 1 Comment

Benefits and limitations of AWS RDS

This short video details some benefits and limitations of AWS RDS. It’s not full SQL Server as you know, but it does a lot for you. I also go into some key AWS terminology which may help the those who are new to AWS, or the cloud in general. The video is only 5 minutes long. Once you watched it, you can get a more detailed list of unsupported features from the AWS documentation.

UPDATE: You can now use database mail with RDS – See this.

Benefits and limitations of AWS RDS video

Download slides

Course homepage | Next video

Filed Under: AWS RDS, Training

How to change the slow query log threshold on RDS

19th August 2021 By John McCormack 1 Comment

turtles on a log

What is the slow query log

Before we discuss how to change the slow query log threshold on RDS, let’s quickly establish what the slow query log is.

The slow query log will record all queries which are above the threshold level. The default value is 10 (seconds) but you can set it higher or lower depending on your requirements. It is useful for finding slow queries and allows you to pick out candidates for tuning.

If you set the threshold too low, it can increase I/O overhead on your instance and use a lot of valuable disk space. If you set it too high, it might not capture enough useful information.

AWS Web console

If you are doing this change as a one off, it might be simpler to just use the AWS web console.

  1. Log into AWS
  2. Select RDS
  3. Find out which parameter group your instance is in
    1. Select instance then click configuration.
    2. Scroll down to see the parameter group
    3. It will be a hyperlink so just click on it
  4. In the parameter group page, use the search box and search for slow_query_log
    1. Ensure it is set to 1
    2. If not, click edit parameters and change the value to 1.
  5. Change search box to long_query_time
    1. Set it to any value between 1 and 31536000
    2. Save changes
AWS CLI
aws rds modify-db-parameter-group --db-parameter-group-name "primary-mysql-5point7" --parameters "ParameterName='long_query_time',ParameterValue=1,ApplyMethod=immediate" --profile dev
# If you don't have multiple profiles set, leave out --profile dev.
AWS PowerShell
$HashArguments = @{
    DBParameterGroupName = "mysql5point7-monster-param-grp-ci"
    Parameter=@{ParameterName="long_query_time";ParameterValue="10";ApplyMethod="Immediate"}
    Profilename = "Dev"
    }
Edit-RDSDBParameterGroup @HashArguments

No restart needed

Changing either slow_query_log or long_query_time parameters can both be safely done without the need to restart your instance. This is because RDS defines them as Dynamic parameters. Take note when changing parameters that are defined as Static as they will require a restart. This either means short but immediate downtime or waiting until your next maintenance window.

rds parameters slow_query_log
rds parameters long_query_time

Further reading

I used some of these links to help me gather the information I needed to write this blog post.

  1. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithParamGroups.html#USER_WorkingWithParamGroups.Modifying
  2. https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-parameter-group.html
  3. https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-RDSDBParameterGroup.html
Featured image by icsilviu from Pixabay

Filed Under: front-page, MySQL

Backup and restore for AWS RDS SQL Server

30th July 2020 By John McCormack Leave a Comment

Backup and restore for AWS RDS SQL Server

In this video, we talk about backup and restore for AWS RDS SQL Server. One of the great benefits of Platform as a Service (PaaS) is that your cloud hosting company will handle the backups for you. And they’re really good at it. In this case, AWS will allow you to restore to any point in time within the last 35 days, and all with no administration effort on your part.

This video dives a bit deeper into the non default options such as backing up and restoring using native backups and how you can keep those backups way beyond the 35 days.

Download Slides | View T-SQL Script

Course Homepage | Next video

Filed Under: AWS RDS, AWS SQL Server, SQL Server, Training

Connecting to your AWS RDS instance

26th July 2020 By John McCormack Leave a Comment

Connecting to your AWS RDS instance

This short training video shows you what is required for connecting to your AWS RDS instance. Learn how to use SQL Server Management Studio and what is different from connection to your on-premises SQL Server.

Connecting to your AWS RDS instance video

Course Homepage  |  Next video

Filed Under: AWS, AWS RDS, AWS SQL Server, SQL Server, Training

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • …
  • 6
  • Next Page »
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

John McCormack · Copyright © 2025

 

Loading Comments...