Saturday 29 April 2017

SQL Server 2017 Hidden Gems – Part 1

In this blog post series I will be covering the new features and enhancements brought in SQL Server 2017. With SQL Server 2017 CTP 2.0 release, new column modified_extent_page_count is introduced in sys.dm_db_file_space_usage to track differential changes in each database file of the database. This feature was requested by customers and community in this connect item.

With this, now you can configure your differential backup with an extra piece of code to determine the percentage of pages changed since last full backup. With large number of changes in the database, cost and time to complete differential backup is similar to that of full database backup so there is no real benefit of taking differential backup in this case but it can rather increase the restore time of database.

Let’s do a quick demo to see how it works

I am creating a new database and the running sys.dm_db_file_space_usage on the new database.

CREATE DATABASE vNext
GO
USE vNext
GO
SELECT modified_extent_page_count, * FROM SYS.dm_db_file_space_usage













As full backup will be the base for the differential backup, we are not seeing any pages to be tracked under modified extent page column. If you take a full backup and then run the same command, then you should see some value.
Now I am going to load some test data, take a full backup and then check the value in modified_extent_page_count column.

--Creating a test table and inserting 100 records
CREATE TABLE Test (ID INT IDENTITY(1,1), NAME CHAR(8000))
GO
INSERT INTO Test values ('This is a test data')
GO 100

--Perform a full backup of vNext database to create a base for differential backup
BACKUP DATABASE vNext to disk='NUL'

--Take a look at the modified_extent_page_count column
SELECT modified_extent_page_count, * FROM sys.dm_db_file_space_usage

















Again I am inserting 500 more records to show that whatever changes we do on that file is being tracked

--Let's insert 500 more records
INSERT INTO Test values ('This is a test data')
GO 500

--Take a look at the modified_extent_page_count column
SELECT modified_extent_page_count, * FROM sys.dm_db_file_space_usage














Now let’s see what happens when you take a differential backup.

BACKUP DATABASE vNext TO DISK='vNext_Differential_1.bak' WITH DIFFERENTIAL















You can see 584 pages were processed in the data file with differential backup. This was the same number which was shown in modified_extent_page_count column.
Let’s do couple of backup test to see how the modified_extent_page_count column value changes. First, we’ll take a copy_only full backup

--value of modified_extent_page_count column
SELECT modified_extent_page_count AS 'Before full backup' FROM sys.dm_db_file_space_usage

--Copy only full backup
BACKUP DATABASE vNext TO DISK='NUL' WITH COPY_ONLY

--value of modified_extent_page_count column
SELECT modified_extent_page_count AS 'After COPY ONLY full backup' FROM sys.dm_db_file_space_usage



















As expected, copy_only full backup will not make any change to the backup chain. Now let’s take a full backup of the database

--value of modified_extent_page_count column
SELECT modified_extent_page_count AS 'Before full backup' FROM sys.dm_db_file_space_usage

--Full backup
BACKUP DATABASE vNext TO DISK='NUL'

--value of modified_extent_page_count column
SELECT modified_extent_page_count AS 'After full backup' FROM sys.dm_db_file_space_usage






















So once a fresh full backup is taken that will be the base for next differential backup. So modified_extent_page_count will have a new value.

Hope this helps. Please do share your thoughts in the comments below

Cheers,
Naveen

Wednesday 19 April 2017

What's new with SQL Server 2017?

Microsoft announced the preview for next release of SQL Server at Microsoft Data Amp. It’s SQL Server 2017!!!

















Community Technology Preview (CTP) 2.0 is the first production-quality preview of SQL Server 2017, and it is available on both Windows and Linux. Click here to download SQL Server 2017 CTP 2.0.























Here are some of the exciting new features added to database engine

  • Adaptive Query Processing – Faster query processing with improved plan quality.
  • Store and analyse non-hierarchical relationships with Graph data support using node/edge query.
  • Resumable online index rebuild. Now you can pause and resume the online index rebuilds
  • Cluster-less Availability Groups for scaling read operations. But with you scale up for read operation, you will not get high availability.
Syntax to create AG without cluster looks like
CREATE AVAILABILITY GROUP [AG-NAME] WITH (CLUSTER_TYPE=NONE)

What’s available on SQL Server on Linux?

Operational Features
  • Support for RHEL, Ubuntu, Docker
  • SQL Server Linux on Docker
  • Package based installs, Docker images
  • Support for Open shift, Docker Swarm
  • Failover clustering through pacemaker 
  • Availability Groups
  • SQL Agent
  • Replication
  • SCOM management Pack
  • Log shipping
  • DMV's
  • Full-Text Search
  • Transparent Data Encryption (TDE)

Programming Features
  • In memory OLTP
  • Columnstore
  • Python
  • Compression
  • Always Encrypted
  • Row Level Security
  • Dynamic Data Masking
  • Service broker
  • Change Data Capture (CDC)
  • Partitioning
  • Auditing
  • CLR
  • JSON
  • XML
Security
  • Active Directory authentication coming soon!
What is not available on Linux?
  • DB Mail
  • Alerts
  • FileTables
  • Stretch DB
  • PolyBase
  • XPs
  • Mirroring
  • VSS Snapshots
Other services which are not available on Linux
  • SSAS
  • SSRS
  • SSIS
  • R service
  • MDS
  • DQS

Windows based SQL Server tools like SSMS and Profiler works when connected to SQL Server on Linux. Native command line tools like SQLcmd, bcp and sqlpackage are also supported on Linux.
Microsoft is also planning to release a new cross platform database admin GUI tool for doing administrative tasks. WOO HOO! This tool will work on Windows, Linux and Mac OS. And this is actually an open source project which will be available on GitHub.

Go-ahead and get your copy of SQL Server 2017 CTP and start exploring these new features

Cheers,
Naveen

Sunday 9 April 2017

Always good to know your GUEST in SQL Server

Few days back came across an interesting issue, which was new for me. Application owner buzzed me and raised concerns about the SQL Server security setup.

App.Owner: We have some serious issue with our database security. I fear wrong people having elevated access to our data.
Me: Ok, Let me have quick look and get back to you.
App.Owner: Yes please. We need to fix this now.
Me: Did a quick check and everything seems to be fine. You’re ID with db_owner and other users with read access to the database.
App.Owner: I was also under the same impression. But today, I came to know there are other users who were also are also able to modify the data. That’s not at all good.
Me: hmm that’s strange. I will do a complete check to figure out the loophole.
App.Owner: Thanks. I will wait to hear from you.
Me: Okay. Ahh Found the suspect. All those privileges were getting transmitted from guest user. I could see someone had mistakenly provided extra permission to guest user, which is not at all required for this environment. Now things are back to normal. Please have a look from your end and let me know you if you have any questions
App.Owner: Great! Thanks for fixing this. Have a good day
Me: You too, have a good day!

In my case issue was with guest user permission. Let’s see the same in demo

-First we will create a new database named ‘TESTDB’. Then will check the status of guest account.
























Note: If guest user is enabled in Model database, the same will get applied to newly created database

-Now we will create a SQL Login ‘UserA’, a new table ‘EPL’ and insert 3 records.



















We have just created a SQL Login it’s not mapped to any of the database. At instance level also no permission is provided.

Now let’s try doing a simple select on table EPL using UserA.














As expected, ‘UserA’ is not able to fetch the data from TESTDB. Now let’s try enabling and providing read and write permission to ‘Guest’ user in TESTDB. You cannot enable/disable Guest user through GUI, it can be done only with T-SQL.

To Enable Guest Account:

USE [DATABASE_NAME]
GO
GRANT CONNECT TO GUEST
GO

To Disable Guest Account:

USE [DATABASE_NAME]
GO
REVOKE CONNECT TO GUEST
GO





















Command completed successfully. Now let’s try running some commands.





















So now UserA is able to read and insert records in TESTDB. The same task can be performed by all users associated with the instance. When Guest user is enabled, it will authorize access for logins which are not mapped to the database.

Things to know about Guest user

  • It’s always recommended to disable guest user for user database, unless you have any specific/special requirement.
  • Guest user cannot be dropped, but can be disabled/enabled.
  • Guest user cannot be disabled for Master and Tempdb database.
  • On msdb database guest user should not be disabled. If you have any plans to do so, please read this.

Do you use Guest user? On what scenarios you use them?
Please share your thoughts via comments below.
 
Cheers,
Naveen