24/10/2023

SARGability in SQL Server

When I heard first about SARGability, it was a funny sound word to me and never could remember the meaning of it.

So I decided to search on it and study a bit and write a note about it, so I can remember it.

In simple terms SARGability means, ability use search predicates (i.e. what is in where clause) to seek through index or a table. High SARGability means, your where clause can be directly use to seek through index or table.

Non-SARGability means, opposite of that, of course.  I.e. these predicate will be hard for SQL server to use in direct seek operation.

Erik Darling, has goo article on this -> https://erikdarling.com/sargability-week-what-is-it-anyway/


23/10/2023

Migrating SSRS Report Subscriptions

 SSRS has nice little feature where you can automate emailing of an report on a pre-defined schedule. Last week, one of our customer was moving their SQL server to brand new one and wanted to move all SQL related feature to it. 

So I have moved everything and was left with few dozens of report subscriptions. I wondering whether I should re-create them manually on new server or is there a tool that I can use.

When asked from AI search tool, it came up with several tools. Out of them following two were my finalists.

1. Report Sync Tool -> https://code.google.com/archive/p/reportsync/downloads

2. ReportingServicesTools written in PowerShell

I decided to go with PowerShell, because of my love with PowerShell.

ReportingServicesTools is a PowerShell cmdlet is a tool that we can use to do various tasks related to SSRS. It is hosted on GitHub -> ReportingServices

Documentation contains how to install it and basic use of it. However, I couldn't find enough documentation regarding how to do a report subscription migration.

There were some information in here, but nothing much.

So I decided to dig little deep into the script and see what is it capable of. I was working with version 0.0.8.0.

There are about 70 odd functions in that cmdlet, but I was interested on functions relate to subscriptions.

  • Get-RsSubscription
  • Set-RsSubscription
  • Copy-RsSubscription
  • Export-RsSubscriptionXml
  • Remove-RsSubscription
  • New-RsSubscription
  • Import-RsSubscriptionXml

In order to fetch subscriptions under a reporting folder I used following:

Get-RsSubscription -ReportServerUri 'http://remote-machine:8080/reportserver_sql16' -RsItem '/path/to/my/report'

In -RsItem parameter you can specify a folder or a report it self. If you specify a folder, it will look for all subscriptions under that folder. If you specify a report, it will look for all subscription belong to that report only.

Above assume current windows user credentials when connecting to report server. But if you want to connect to report server using different credentials use the usual methods to specify credentials for -Credential parameter.

$password = ConvertTo-SecureString "MyPlainTextPassword" -AsPlainText -Force

$Cred = New-Object System.Management.Automation.PSCredential ("username", $password)


Now I got subscriptions, I want to write them to a file, so I can transfer them to the new server (if the new server is accessible from where you are you can directly use the Copy-RsSubscription function).

To export to a file, I have used Export-RsSubscriptionXml function.

Get-RsSubscription <<parameters as per above>> | Export-RsSubscriptionXml C:\Test\MySubscriptions.xml

You can pipe the output of the Get-RsSubscription function to Export function.


Then on the new server, I have copied above xml file to a directory and used Import-RsSubscription to import them to new Reporting server.

Import-RsSubscriptionXml C:\Test\\MySubscriptions.xml -ReportServerUri 'http://remote-machine:8080/reportserver_sql16'

This will just create Powershell objects from the xml file, you need to output this to a Copy-RsSubscription or Set-RsSubscription function.

Different between Copy and Set functions are, Copy create new subscriptions, where set updates already existing subscription.

If you want just to see, what is in a xml file you can use command like below:

            Import-RsSubscriptionXml .\MySubscriptions.xml | 

            Out-GridView -PassThru |

            Copy-RsSubscription -RsItem /Example/Report


You can learn more about these functions, from source code help available on the GitHub -> CatalogItems.


05/10/2023

When I SET STATISTICS IO ON

SET statements in TSQL allows user to get more information or change behavior of the query execution in SSMS.

One of the most common SET statement we use is SET STATISTICS ON/OFF.

This shows statistical information about the query information. This is what we get when we turn on STATISTICS IO ON.

Statistic IO Result:

---------------------------

Table '<<Table name>>'. -> Name of the table we read

Scan count 1, -> Number of scans or seeks

logical reads 160,  -> Reads from RAM

physical reads 0, -> Reads from Disk

read-ahead reads 0, -> Number of pages placed into the cache for the query


lob logical reads 0, -> Large objects read from RAM

lob physical reads 0, -> Large objects read from Disk

lob read-ahead reads 0. -> Number of pages for LOB placed into the cache for the query


LOB stands for Large objects, such as ntext, nvarchar, varchar, etc.

LOB data is stored not on the row, but away from the row.

  • If size is < 8000 bytes, stored on row overflow pages
  • If size is > 8000 bytes, stored on LOB data pages



Moving away from old Azure AD Connect ...

One of the client we work for had hybrid Active Directory, which means they have their on premise domain with two on premise domain controllers which synchronised with Azure Active Directory (now called Microsoft Entra Id).

They used Azure AD Connect utility to sychronise users and credentials between on premise domain controllers and Azure AD (Entra Id). This utility was installed on one of the domain controllers.

They had version 1.x. Last week they suddenly got a email from Microsoft saying AD Connect 1.x versions will be stopped working from 1st of October 2023. Company was not ware of this until then. When searched about this on the web, AD Connect 1.x versions was actually retired on 31/08/2022.



So they needed to act fast.

There were two options:

  • Install newest version of Azure AD Connect (which is version 2.x)
or
  • Install new utility Microsoft recommend to synchronise on premise AD with Microsoft Entra Id -> which is called Cloud Sync
Reading about Cloud Sync, it is the future proof utility to syncrhonised identities between on premise and Microsoft Entra Id. Program run primarily on cloud (un-like AD Connect) and have an agent running on the on premise server for synchronisation.


After considering lot, we have decided to install new version of the AD Connect instead of Cloud Sync. This is mainly because, this organisation is schedule to remove all on premise present of network very soon. There fore it was not worthy to install whole new architecture of sync on servers.

When we tried to install, there were another issue. Azure AD Connect was not supporting Windows version below 2016. But they had domain controllers on Windows 2012 R2. There fore we had to upgrade them before starting to install new version.

Fortunately, domain controller upgrade was straight forward, didn't face much issues and we were able to upgrade to Windows 2019 and also increased the Domain Functional level to 2016 (which is the highest available at that time).

Only issue we faced while upgrading was preparing AD (because they were domain controllers)



All you have to do is run adprep. Adprep can be found in the installation DVD of the windows 2019 (or any other version).


In this method, we installed AD Connect new version on the other domain controller and then kept it staging mode (not synching). Then we stopped old connect and un-installed it. Then we have activated the new version.


01/10/2023

Using NOEXPAND hint

 NOEXPAND is a TSQL query hint, which we use with indexed views in certain scenarios.

NOEXPAND

Indexed views (or materialized views) are the ones which store underlying data in the database, where normal views generate data for the underlying query on run time.

However, even though indexed view store actual data for the view, there is a change SQL server still use the underlying query directly to generate the data when view is used in a query.

Under some circumstances, this can cause poor performances. So to improve performances, you can force SQL server to use underlying data on the query by using this query hint.


Why I cannot use alias name in where clause

 


Ever wonder why when alias columns in "SELECT" clause of a TSQL code, you cannot use them in some other clauses but not on others?

Until I know the truth I was also confused and frustrated.

This is caused by how SQL server query parsing engine works. It process key words/clauses in a particular order.

  1. FROM
    1. Cross JOIN
    2. ON
  2. WHERE
  3. GROUP BY
    1. CUBE/ROLLUP
  4. HAVING
  5. SELECT
    1. Expressions, aggregates and alias
    2. DISTINCT
    3. TOP
  6. ORDER BY
    1. OFFSET / FETCH
As you can see from above order, alias is pretty down the order, just before ORDERY BY clause. So you can really use alias in ORDER BY clause only.

Kind of disappointing isn't it?

Introduction to SQL Server Statistics - Tutorial

Wishing you all, my loving readers, Happy New Year 2025! This is the first blog for the year 2025. As a new initiative, I'm going to (tr...