THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | |
in Search

Greg Low (The Bit Bucket: IDisposable)

Ramblings of Greg Low (SQL Server MVP, MCM and Microsoft RD) - SQL Down Under

  • Optimizing Dynamic Search Screens – Fixing the Performance of Common Code Paths

    One of the common challenges that arises in many applications is around how to optimize the SQL Server performance of dynamic search queries. For example, we've all seen applications with screens like this:

    clip_image002

    Often, there will be many, many more fields to be searched. What I often hear from developers is that there is no way to optimize the query because you don't know in advance which options the user will choose. While this is partly true, it assumes that human behaviour is much more random than it really is. Let me explain:

    A few years back, I was doing performance tuning work at a company that books flights. They had a stored procedure that took a single parameter of a BookingNumber, and returned details of a customer's booking. It took around ½ second to execute.

    Eventually, one of the users said "I'd love to be able to look up the booking by LastName". So they changed the procedure to have an optional parameter to allow for that. Later another user asked for EmailAddress. And so on, and so on. When I arrived, they had a 4000 line procedure that allowed 14 optional parameters. Unfortunately, it now took 90 seconds to execute every time.

    What was truly sad, was that in a week of testing, I never saw any value passed except the BookingNumber. So they had created a situation where all the users all day long were experiencing glacial response times, for a situation that almost never occurred.

    I see this sort of scenario played out time and again. When confronted with a screen that allows the users to pick and choose search criteria, developers always seem to assume that people will choose a wide variety of options. Yet that isn't what happens. Generally, there will be a small set of options that they will choose again and again, and only rarely will they make other choices.

    Knowing that, you realise that even though you can't optimize for every possible code path, it's critical that the common paths are optimized. If necessary, provide alternate code paths for the high volume query types.

    Finally, for the atypical choices made by users, what do you do? A Swedish friend of mine (and fellow SQL Server MVP) Erland Sommarskog has some awesome articles at his web site www.sommarskog.se. One in particular is on Dynamic Search Conditions. I recommend reading it if you have never done so.

  • Power BI Designer March Update includes Google Analytics Connector

    The Power BI team have released details of their March update to the standalone Power BI designer.

    You’ll find details of the update here: http://blogs.msdn.com/b/powerbi/archive/2015/03/26/7-new-updates-to-the-power-bi-designer-preview-march-2015.aspx

    The first thing I noticed is just how much faster the tool operates. The blog post mentioned performance enhancements but I really, really noticed them.

    One particular enhancement  that I wanted to call out was the additional of a connector for Google Analytics. I’ve been trying that this morning and have found it really easy to use. All the standard categories appear as available sets of data:

    image

    I’ve picked those that I’m interested in:

    image

    Then added them to a report:

    image

     

    Very easy. And it’s clear which type of device/operating system we need to continue to ensure the best experience on at SQL Down Under.

  • Powershell Invoke-Sqlcmd –Hostname Parameter sets the Application Name instead of the Host Name

    Two of the parameters in SQL Server connections are the Application Name and the Host Name. You can see these in SQL Server if you execute the following command:

    image

    I’ve always been a fan of having applications identify themselves in their connection strings. It makes tasks like tracing much easier. The tools supplied with SQL Server do a reasonable job of that as you can see above. But many other tools don’t do such a good job.

    I was working at a site today where they are using Powershell to execute commands for monitoring. I noticed that the Powershell commands did not set the Application Name in the connection string when using Invoke-Sqlcmd. Note the following example:

    image

    I then tried to work out how to set the Application Name. When I checked the documentation for Invoke-Sqlcmd, it shows that the Hostname is set via the SQLCMD option –H, by using the –Hostname parameter.

    image

    However, note that if you use the –Hostname option, it actually sets the Application Name and does not set the Host Name:

    image

     



  • Suggest Compression Strategies for Tables and Indexes

    I make no secret of the fact that I think that table compression was the best thing added to SQL Server 2008.

    It seems like every week that I’m talking to clients about it. They have Enterprise Edition but they weren’t using it because they were familiar with O/S level disk compression and they think that compression will mean smaller and slower, yet this often couldn’t be further from the truth for customers whose databases are currently I/O bound or who don’t have enough memory in their buffer cache for it to work correctly.

    It’s important though, to make sure the correct form of compression is used for each table and index. In fact, the decision should be taken for each partition of each table and index if the table is partitioned. We often recommend different compression strategies for different partitions of the same table.

    To make it easier to decide which form of compression might suit, I’ve created another version of the script that I use as a first pass in determining compression strategies. The earlier version suggested ROW and/or PAGE compression. While this version can do the same, it also considers the clustered columnstore indexes that are part of the Enterprise Edition of SQL Server 2014. (I’ve included a switch to turn that function on or off in case you don’t have 2014 yet).

    The cutoff points are configurable but my default values are as shown.

    • CCI (Clustered Columnstore Index) will be recommended where the partition is scanned more than 95% of the time, updated less than 10% of the time, seeks and lookups are less than 5% of the time, and where there are at least 800,000 rows. It will also only be recommended if it is supported.
    • PAGE will be recommended where the partition is scanned more than 75% of the time and updated less than 20% of the time.
    • ROW will be recommended in all other cases. We believe that ROW should be the default in SQL Server across the board, instead of NONE.

    It is important that this script only be run after the system has been in use for long enough to have experienced typical usage patterns.

    I hope this helps you to get into using compression where it makes sense for you.

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

    -- Suggest data compression changes for tables and indexes

    --

    -- Dr Greg Low

    -- March 2015

    --

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

    DECLARE @ClusteredColumnstoreScansCutoff int = 95;

    DECLARE @ClusteredColumnstoreUpdatesCutoff int = 10;

    DECLARE @ClusteredColumnstoreSeeksLookupsCutoff int = 5;

    DECLARE @ClusteredColumnstoreTotalRowsCutoff bigint = 800000;

     

    DECLARE @PageCompressionScansCutoff int = 75;

    DECLARE @PageCompressionUpdatesCutoff int = 20;

     

    DECLARE @IsClusteredColumnstoreSupported bit = 1;

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

     

    WITH IndexUsageStats

    AS

    ( SELECT object_id AS ObjectID,

             index_id AS IndexID,

             COALESCE(user_seeks, 0) + COALESCE(system_seeks, 0) AS Seeks,

             COALESCE(user_scans, 0) + COALESCE(system_scans, 0) AS Scans,

             COALESCE(user_lookups, 0) + COALESCE(system_lookups, 0) AS Lookups,

             COALESCE(user_updates, 0) + COALESCE(system_updates, 0) AS Updates,

             COALESCE(user_seeks, 0) + COALESCE(system_seeks, 0)

               + COALESCE(user_scans, 0) + COALESCE(system_scans, 0)

               + COALESCE(user_lookups, 0) + COALESCE(system_lookups, 0)

               + COALESCE(user_updates, 0) + COALESCE(system_updates, 0) AS Operations

      FROM sys.dm_db_index_usage_stats

      WHERE database_id = DB_ID()

    ),

    PartitionUsageDetails

    AS

    ( SELECT SCHEMA_NAME(t.schema_id) AS SchemaName,

             t.name AS TableName,

             i.name AS IndexName,

             i.index_id AS IndexID,

             i.type_desc AS IndexType,

             CASE WHEN COALESCE(Operations, 0) <> 0

                  THEN CAST((COALESCE(Seeks, 0) + COALESCE(Lookups, 0)) * 100.0

                            / COALESCE(Operations, 0) AS int)

                  ELSE 0

             END AS SeekLookupPercentage,

             CASE WHEN COALESCE(Operations, 0) <> 0

                  THEN CAST(COALESCE(Scans, 0) * 100.0 / COALESCE(Operations, 0) AS int)

                  ELSE 0

             END AS ScanPercentage,

             CASE WHEN COALeSCE(Operations, 0) <> 0

                  THEN CAST(COALESCE(Updates, 0) * 100.0 / COALESCE(Operations, 0) AS int)

                  ELSE 0

             END AS UpdatePercentage,

             p.partition_number AS PartitionNumber,

             p.data_compression_desc AS CurrentCompression,

             p.rows AS TotalRows

       FROM sys.tables AS t

       INNER JOIN sys.indexes AS i

       ON t.object_id = i.object_id

       INNER JOIN sys.partitions AS p

       ON i.object_id = p.object_id

       AND i.index_id = p.index_id

       LEFT OUTER JOIN IndexUsageStats AS ius

       ON i.object_id = ius.ObjectID

       AND i.index_id = ius.IndexID

       WHERE i.index_id > 0

       AND t.is_ms_shipped = 0

       AND t.type = N'U'

    ),

    SuggestedPartitionCompressionTypes

    AS

    ( SELECT pud.*,

             CASE WHEN pud.ScanPercentage >= @ClusteredColumnstoreScansCutoff

                  AND pud.UpdatePercentage <= @ClusteredColumnstoreUpdatesCutoff

                  AND pud.SeekLookupPercentage <= @ClusteredColumnstoreSeeksLookupsCutoff

                  AND pud.TotalRows >= @ClusteredColumnstoreTotalRowsCutoff

                  AND @IsClusteredColumnstoreSupported <> 0

                  THEN N'CCI'

                  WHEN pud.ScanPercentage >= @PageCompressionScansCutoff

                  AND pud.UpdatePercentage <= @PageCompressionUpdatesCutoff

                  THEN N'PAGE'

                  ELSE N'ROW'

             END AS SuggestedCompression

      FROM PartitionUsageDetails AS pud

    )

    SELECT spct.SchemaName,

           spct.TableName,

           spct.IndexName,

           spct.PartitionNumber,

           spct.CurrentCompression,

           spct.SuggestedCompression

    FROM SuggestedPartitionCompressionTypes AS spct

    WHERE spct.SuggestedCompression <> spct.CurrentCompression

    ORDER BY spct.SchemaName,

             spct.TableName,

             CASE WHEN spct.IndexID = 1 THEN 0 ELSE 1 END,

             spct.IndexName;

     

  • Rebuild Partitioned Clustered Columnstore Indexes

    A short time back, I provided an updated script that can be used to rebuild clustered columnstore indexes when necessary.

    One thing that wasn’t covered by that script was the ability to only individual index partitions. Below, I’ve provided an updated version of the script that will rebuild only affected partitions if the clustered columnstore index is partitioned.

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

    -- Rebuild clustered columnstore indexes when necessary

    --

    -- Dr Greg Low v2.2

     

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

    -- rebuild when more than supplied % of rows have been deleted

    -- rebuild when any segments contain more than supplied % deleted rows

    -- rebuild if more than supplied number of segments are empty

    -- rebuild when segments are on average less than the supplied % of

    --     maximum capacity

     

    DECLARE @DeletedTotalRowPercentage int = 10; 

    DECLARE @DeletedSegmentsRowPercentage int = 20;

    DECLARE @EmptySegmentsAllowed int = 0;

    DECLARE @SegmentPotentialFullnessPercentage int = 90;

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

     

    DECLARE @IndexesToRebuild TABLE (SchemaName sysname,

                                     TableName sysname,

                                     IndexName sysname,

                                     PartitionNumber int,

                                     IsPartitioned bit);

     

    SET NOCOUNT ON;

     

    WITH ClusteredColumnstoreIndexes

    AS

    ( SELECT t.object_id AS ObjectID,

             SCHEMA_NAME(t.schema_id) AS SchemaName,

             t.name AS TableName,

             i.name AS IndexName

      FROM sys.indexes AS i

      INNER JOIN sys.tables AS t

      ON i.object_id = t.object_id

      WHERE i.type = 5

    ),

    RowGroups

    AS

    ( SELECT csrg.object_id AS ObjectID,

             csrg.partition_number AS PartitionNumber,

             csrg.total_rows AS TotalRows,

             csrg.deleted_rows AS DeletedRows,

             csrg.deleted_rows * 100.0 / csrg.total_rows AS DeletedPercentage,

             CASE WHEN csrg.total_rows = csrg.deleted_rows

                  THEN 1 ELSE 0

             END AS IsEmptySegment

      FROM sys.column_store_row_groups AS csrg

      WHERE csrg.state = 3 -- Compressed (Ignoring: 0 - Hidden, 1 - Open, 2 - Closed, 4 - Tombstone)

    ),

    IndexPartitionStats

    AS

    ( SELECT cci.ObjectID,

             cci.SchemaName,

             cci.TableName,

             cci.IndexName,

             rg.PartitionNumber,

             SUM(CAST(rg.TotalRows AS decimal(18,0))) AS TotalRows,

             SUM(CAST(rg.DeletedRows AS decimal(18,0))) AS DeletedRows,

             SUM(CAST(rg.DeletedRows AS decimal(18,0))) * 100.0

               / SUM(CAST(rg.TotalRows AS decimal(18,0)))

               AS DeletedPercentage,

             SUM(rg.IsEmptySegment) aS EmptySegments,

             COUNT(rg.TotalRows) AS TotalSegments

      FROM ClusteredColumnstoreIndexes AS cci

      INNER JOIN RowGroups AS rg

      ON cci.ObjectID = rg.ObjectID

      GROUP BY cci.ObjectID, cci.SchemaName, cci.TableName, cci.IndexName, rg.PartitionNumber

    )

    INSERT @IndexesToRebuild (SchemaName, TableName, IndexName, PartitionNumber, IsPartitioned)

    SELECT s.SchemaName,

           s.TableName,

           s.IndexName,

           s.PartitionNumber,

           CASE WHEN EXISTS(SELECT 1

                            FROM IndexPartitionStats AS ps

                            WHERE s.SchemaName = ps.SchemaName

                            AND s.TableName = ps.TableName

                            AND s.IndexName = ps.IndexName

                            AND ps.PartitionNumber > 1)

                THEN 1

                ELSE 0

           END

    FROM IndexPartitionStats AS s

    WHERE s.DeletedPercentage > @DeletedTotalRowPercentage

    OR s.EmptySegments > @EmptySegmentsAllowed

    OR EXISTS(SELECT 1 FROM RowGroups AS rg

                       WHERE rg.ObjectID = s.ObjectID

                       AND rg.DeletedPercentage > @DeletedSegmentsRowPercentage)

    OR (s.TotalRows * 100 / (s.TotalSegments * 1048576.0))

                < @SegmentPotentialFullnessPercentage;

     

    DECLARE @SchemaName sysname;

    DECLARE @TableName sysname;

    DECLARE @IndexName sysname;

    DECLARE @PartitionNumber int;

    DECLARE @IsPartitioned bit;

    DECLARE @SqlCommand nvarchar(max);

     

    DECLARE IndexList CURSOR FAST_FORWARD READ_ONLY

    FOR

      SELECT SchemaName, TableName, IndexName, PartitionNumber, IsPartitioned

      FROM @IndexesToRebuild

      ORDER BY SchemaName, TableName, IndexName, PartitionNumber;

     

    OPEN IndexList;

     

    FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName, @PartitionNumber, @IsPartitioned;

     

    WHILE @@FETCH_STATUS = 0

    BEGIN

      SET @SqlCommand = N'ALTER INDEX ' + QUOTENAME(@IndexName)

                      + N' ON ' + QUOTENAME(@SchemaName)

                      + N'.' + QUOTENAME(@TableName)

                      + N' REBUILD'

                      + CASE WHEN @IsPartitioned <> 0

                             THEN N' PARTITION = ' + CAST(@PartitionNumber AS nvarchar(10))

                             ELSE N''

                        END

                      + ';';

      PRINT @SqlCommand;

      EXEC(@SqlCommand);

      FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName, @PartitionNumber, @IsPartitioned;

    END;

     

    CLOSE IndexList;

    DEALLOCATE IndexList;

     

     

     

     

  • Lots of (Mostly Free) eBooks from Microsoft Virtual Academy

    I know that many of my readers appreciate finding out when free eBooks become available.

    Microsoft Virtual Academy has been releasing quite a few lately. You’ll find them here: http://www.microsoftvirtualacademy.com/eBooks?PRID=MVP-AU-7914

    In particular, SQL Server and database folk might want to take a look at the following:

    • Introducing Microsoft SQL Server 2014
    • Introducing Windows Server 2012 R2
    • Introducing Microsoft Azure HDInsight
    • Microsoft Azure Essentials: Fundamentals of Azure
    • Microsoft Azure Essentials: Fundamentals of Azure
    • Introducing Windows Azure for IT Professionals
    • Rethinking Enterprise Storage: A Hybrid Cloud Model
  • Interested in Winning a Trip to the Ignite Conference?

    This is the first year for Microsoft’s new Ignite conference. Many people are keen to go but figure it would break the bank.

    image

    Here’s another option:

    The Microsoft Virtual Academy have their Heroes program running again. You get one point in the draw for just signing up. Even better though, take some courses and both learn something worthwhile and increase your chances of winning the competition:

    image

    Sign up and enter here now: SIGN UP           (NOTE: Australians Only for this one sadly)

  • Update on Options for Learning Chinese: eChineseLearning

    I seem to have quite a crowd of people that asks me how I’m going with learning Chinese whenever they see me. I promised that I would put together some posts on what has/hasn’t worked for me.

    Over the last few years I had spent quite a bit of time trying to get my Mandarin fluency up. I had finished the 5 levels of Rosetta Stone which helped a lot with vocab but I really don’t like what they’ve done with their online “Rosetta Studio” options. They aren’t good value any more and you don’t get enough practice to be worthwhile. What I wanted to do was to get a better vocab and to gain a lot more practice in conversation, as that’s where the real learning happens.

    I’ve tried a wide variety of options. My current top recommendation is:

    eChineseLearning – No idea where I first came across them (probably an online search) but this has been wonderful. This is providing me with a structured 1 on 1 conversational learning environment. Each week, I spend 3 separate sessions of just under an hour, one on one with a teacher from Wuhan (north of Beijing). They have a booking site where available timeslots are shown, both for my primary teacher (Amy) and for alternates if I need to have a class at a time that Amy isn’t available. I was quite wary about signing up for a service like this but I’m so happy I did. Amy is both interesting and interested in my learning, and provides a good structured approach to work towards proficiency.

    I think this is the quickest way to a good level of proficiency. Each lesson, we spend some time in general conversation, then spend a while working on some new words, then spend some time on grammatical issues, and finally spend some time with listening practice. I like the way it is organised and I like the flexibility of the booking. Most other sites I looked at needed you to commit to the same time each week. With my schedule, that wouldn’t work. eChineseLearning allows me to pick times in each week that will work for me. So if I’m travelling, or have family engagements, I can just work around it. You can change it up to 24 hours before each session.

    I could imagine this site being good for anyone that wants to start learning the language (ie: beginner), or who is keen to become more proficient.

    Like all these types of sites, they love referrals. They’d love you to tell them that you heard of them from me. If you do that, both you and I get free extra time. But that’s up to you.

    Either way, if you’ve wondered about learning another language like Mandarin or you have already tried and stalled, you might want to get a feel for it. They have a free trial lesson. You can sign up here: http://www.echineselearning.com/free-trial/index.html 

    Highly recommended!

  • Clustered columnstore index rebuild script–updated

    Recently I published a series of blog posts that provided scripts for rebuilding clustered columnstore indexes.

    Niko Neugebauer sent me a suggested update to the latest version of those scripts and it makes sense. The change will help to filter out row groups that are not compressed and Niko noted that as Microsoft have started to include more objects (like Tombstone values)  into sys.column_store_row_groups in Azure SQLDatabase, he believes that will help to prevent future SQL versions having a problem with the scripts.

    Sounds good so the latest version is shown below. Thanks Niko !

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

    -- Rebuild clustered columnstore indexes when necessary

    -- Dr Greg Low v2.1

     

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

    -- rebuild when more than supplied % of rows have been deleted

    -- rebuild when any segments contain more than supplied % deleted rows

    -- rebuild if more than supplied number of segments are empty

    -- rebuild when segments are on average less than the supplied % of

    --     maximum capacity

     

    DECLARE @DeletedTotalRowPercentage int = 10;  

    DECLARE @DeletedSegmentsRowPercentage int = 20;

    DECLARE @EmptySegmentsAllowed int = 0;

    DECLARE @SegmentPotentialFullnessPercentage int = 90;

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

     

    DECLARE @IndexesToRebuild TABLE (SchemaName sysname,

                                     TableName sysname,

                                     IndexName sysname);

     

    WITH ClusteredColumnstoreIndexes

    AS

    ( SELECT t.object_id AS ObjectID,

             SCHEMA_NAME(t.schema_id) AS SchemaName,

             t.name AS TableName,

             i.name AS IndexName

      FROM sys.indexes AS i

      INNER JOIN sys.tables AS t

      ON i.object_id = t.object_id

      WHERE i.type = 5

    ),

    RowGroups

    AS

    ( SELECT csrg.object_id AS ObjectID,

             csrg.total_rows AS TotalRows,

             csrg.deleted_rows AS DeletedRows,

             csrg.deleted_rows * 100.0 / csrg.total_rows AS DeletedPercentage,

             CASE WHEN csrg.total_rows = csrg.deleted_rows

                  THEN 1 ELSE 0

             END AS IsEmptySegment

      FROM sys.column_store_row_groups AS csrg

      WHERE csrg.state = 3 -- Compressed (Ignoring: 0 - Hidden, 1 - Open, 2 - Closed, 4 - Tombstone)

    ),

    IndexStats

    AS

    ( SELECT cci.ObjectID,

             cci.SchemaName,

             cci.TableName,

             cci.IndexName,

             SUM(CAST(rg.TotalRows AS decimal(18,0))) AS TotalRows,

             SUM(CAST(rg.DeletedRows AS decimal(18,0))) AS DeletedRows,

             SUM(CAST(rg.DeletedRows AS decimal(18,0))) * 100.0

               / SUM(CAST(rg.TotalRows AS decimal(18,0)))

               AS DeletedPercentage,

             SUM(rg.IsEmptySegment) aS EmptySegments,

             COUNT(rg.TotalRows) AS TotalSegments

      FROM ClusteredColumnstoreIndexes AS cci

      INNER JOIN RowGroups AS rg

      ON cci.ObjectID = rg.ObjectID

      GROUP BY cci.ObjectID, cci.SchemaName, cci.TableName, cci.IndexName

    )

    INSERT @IndexesToRebuild (SchemaName, TableName, IndexName)

    SELECT s.SchemaName, s.TableName, s.IndexName

    FROM IndexStats AS s

    WHERE s.DeletedPercentage > @DeletedTotalRowPercentage

    OR s.EmptySegments > @EmptySegmentsAllowed

    OR EXISTS(SELECT 1 FROM RowGroups AS rg

                       WHERE rg.ObjectID = s.ObjectID

                       AND rg.DeletedPercentage > @DeletedSegmentsRowPercentage)

    OR (s.TotalRows * 100 / (s.TotalSegments * 1048576.0))

                < @SegmentPotentialFullnessPercentage;

     

    DECLARE @SchemaName sysname;

    DECLARE @TableName sysname;

    DECLARE @IndexName sysname;

    DECLARE @SqlCommand nvarchar(max);

     

    DECLARE IndexList CURSOR FAST_FORWARD READ_ONLY

    FOR

      SELECT SchemaName, TableName, IndexName

      FROM @IndexesToRebuild

      ORDER BY SchemaName, TableName, IndexName;

     

    OPEN IndexList;

     

    FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName;

     

    WHILE @@FETCH_STATUS = 0

    BEGIN

      SET @SqlCommand = N'ALTER INDEX ' + QUOTENAME(@IndexName)

                      + N' ON ' + QUOTENAME(@SchemaName)

                      + N'.' + QUOTENAME(@TableName)

                      + N' REBUILD PARTITION = ALL;';

      PRINT @SqlCommand;

      EXEC(@SqlCommand);

      FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName;

    END;

     

    CLOSE IndexList;

    DEALLOCATE IndexList;

     

  • PASS BA Analytics Conference for 2015

    I’m always telling people that the thing I love about BI projects is that they tend to appeal to the people who pay the bills. This is a good place to be doing project work.

    When I look at large companies like Amazon, I can imagine that the people who do the IT work that relates to order processing, invoicing, shipping, etc. do a great job. But I’ll bet that their life is full of budget cutbacks, headcounts, increased pressure to improve productivity, etc. By comparison, I’ll bet the projects that fund parts of their site that make recommendations for possible purchases, etc. are funded at an entirely different level. And that’s because these projects can more directly impact the profitability of the organization, and do so in a more visible way.

    When you stop and look at your career, you might want to consider whether you should be trying to move up the value stack within your organization.

    A great place to get ideas on this would be to attend the PASS BA Analytics Conference. This relatively new conference has a very different focus to the “standard” PASS summit. If analytics interests you at all, this would be the place to be in April:

    And if you provide the registration code UKBA05, you’ll get a $150 discount as well.

  • Improved clustered columnstore index rebuild–potential segment fullness

    Earlier, I posted a maintenance script that could be used with clustered columnstore indexes to try to determine when they should be rebuilt. Reorganize is not a very useful operation for these indexes as it basically just forces a close of the existing delta store, ready for compression.

    One of the problems with a clustered columnstore index is that it doesn’t work as well if the segments have been created at less than the maximum potential size of 1048576 rows per segment. Over use of reorganize options could cause this, but more likely it will be to do with loading the index with small batches of data, rather than loading large batches in BULK INSERT operations.

    I’ve updated the maintenance script so that you can now also choose to rebuild if your segments are much less full than their optimal size ie: you could have 1,048,560,000 rows of data in 10,000 segments. So if you say 90 percent as the cutoff and your segments hold less than 90 percent of their potential, that will also trigger a rebuild.

    Hope this helps someone:

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

    -- Rebuild clustered columnstore indexes when necessary

    -- Dr Greg Low v2.0

     

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

    -- rebuild when more than supplied % of rows have been deleted

    -- rebuild when any segments contain more than supplied % deleted rows

    -- rebuild if more than supplied number of segments are empty

    -- rebuild when segments are on average less than the supplied % of

    --     maximum capacity

     

    DECLARE @DeletedTotalRowPercentage int = 10;   

    DECLARE @DeletedSegmentsRowPercentage int = 20;

    DECLARE @EmptySegmentsAllowed int = 0;

    DECLARE @SegmentPotentialFullnessPercentage int = 90;

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

     

    DECLARE @IndexesToRebuild TABLE (SchemaName sysname,

                                     TableName sysname,

                                     IndexName sysname);

     

    WITH ClusteredColumnstoreIndexes

    AS

    ( SELECT t.object_id AS ObjectID,

             SCHEMA_NAME(t.schema_id) AS SchemaName,

             t.name AS TableName,

             i.name AS IndexName

      FROM sys.indexes AS i

      INNER JOIN sys.tables AS t

      ON i.object_id = t.object_id

      WHERE i.type = 5

    ),

    RowGroups

    AS

    ( SELECT csrg.object_id AS ObjectID,

             csrg.total_rows AS TotalRows,

             csrg.deleted_rows AS DeletedRows,

             csrg.deleted_rows * 100.0 / csrg.total_rows AS DeletedPercentage,

             CASE WHEN csrg.total_rows = csrg.deleted_rows

                  THEN 1 ELSE 0

             END AS IsEmptySegment

      FROM sys.column_store_row_groups AS csrg

    ),

    IndexStats

    AS

    ( SELECT cci.ObjectID,

             cci.SchemaName,

             cci.TableName,

             cci.IndexName,

             SUM(CAST(rg.TotalRows AS decimal(18,0))) AS TotalRows,

             SUM(CAST(rg.DeletedRows AS decimal(18,0))) AS DeletedRows,

             SUM(CAST(rg.DeletedRows AS decimal(18,0))) * 100.0

               / SUM(CAST(rg.TotalRows AS decimal(18,0)))
               AS DeletedPercentage
    ,

             SUM(rg.IsEmptySegment) aS EmptySegments,

             COUNT(rg.TotalRows) AS TotalSegments

      FROM ClusteredColumnstoreIndexes AS cci

      INNER JOIN RowGroups AS rg

      ON cci.ObjectID = rg.ObjectID

      GROUP BY cci.ObjectID, cci.SchemaName, cci.TableName, cci.IndexName

    )

    INSERT @IndexesToRebuild (SchemaName, TableName, IndexName)

    SELECT s.SchemaName, s.TableName, s.IndexName

    FROM IndexStats AS s

    WHERE s.DeletedPercentage > @DeletedTotalRowPercentage

    OR s.EmptySegments > @EmptySegmentsAllowed

    OR EXISTS(SELECT 1 FROM RowGroups AS rg

                       WHERE rg.ObjectID = s.ObjectID

                       AND rg.DeletedPercentage > @DeletedSegmentsRowPercentage)

    OR (s.TotalRows * 100 / (s.TotalSegments * 1048576.0))
                < @SegmentPotentialFullnessPercentage
    ;

     

    DECLARE @SchemaName sysname;

    DECLARE @TableName sysname;

    DECLARE @IndexName sysname;

    DECLARE @SqlCommand nvarchar(max);

     

    DECLARE IndexList CURSOR FAST_FORWARD READ_ONLY

    FOR

      SELECT SchemaName, TableName, IndexName

      FROM @IndexesToRebuild

      ORDER BY SchemaName, TableName, IndexName;

     

    OPEN IndexList;

     

    FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName;

     

    WHILE @@FETCH_STATUS = 0

    BEGIN

      SET @SqlCommand = N'ALTER INDEX ' + QUOTENAME(@IndexName)

                      + N' ON ' + QUOTENAME(@SchemaName)

                      + N'.' + QUOTENAME(@TableName)

                      + N' REBUILD PARTITION = ALL;';

      PRINT @SqlCommand;

      EXEC(@SqlCommand); 

      FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName;

    END;

     

    CLOSE IndexList;

    DEALLOCATE IndexList;

      

     

  • Demos must work–simple concept? So why is it so rarely applied?

    When I attend events like TechEd, like many people I usually find the networking time more valuable than the session time. There is a pretty tight limit on the number of sessions you can attend, no matter how hard you try. So I often watch the sessions later when I can. At one of the earliest TechEd Australia events that I attended, they gave us CDs of the TechEd USA sessions. That was great because that TechEd had around 250 sessions, and there were over 150 that I would have loved to attend. Clearly that wasn’t possible.

    I was doing a lot of driving to/from client sites at that time, and so I had a lot of available listening time. I dragged the audio out of all the TechEd session videos and just listened to the audio. If a session really interested me, I’d go back and watch the video and demos.

    That year, I listened to/watched around 150 sessions. While it was intense and interesting, there was something that I wasn’t expecting, that completely stunned me. I heard the presenters apologising for demo failures in close to 100 of those sessions. I found that really, really hard to believe. I was determined that when I was presenting any sort of session at events like these, that I wasn’t going to be one of those people, or I’d do my very best to avoid it.

    Having your session work as planned already puts you in the top third of all the sessions.

    So if you want to be on the right side of this equation, what can you do?

    1. Have realistic goals for the amount of content.

    I normally aim to tell people three things in a session. They certainly won’t remember more than that and it’s the stories that they’ll remember anyway, so make sure that each demo has a good story associated with it. And if showing any one of these three things takes more than about 15 to 20 minutes, try again. Blaise Pascal said “I would have written a shorter letter, but I did not have the time”. Plan the content and tell the story succinctly. Plan your timing. It’s hard work to get just the right message in just the right amount of time.

    I have lost count of how many sessions I’ve been to that run out of time or that failed to make one of the key points. Don’t be one of them.

    You’ll be especially sorry if your session description includes content that you didn’t end up covering. Someone might have come just for that content.

    2. Aim for repeatable achievable outcomes.

    I’ve seen so many demos that would probably only ever work with the moon in the correct position and the presenter holding his/her head the right way. Don’t do this.

    3. Have a clear structure.

    There’s perceived wisdom that sessions should be all demos. I don’t buy it as the only rule. I’ve been to brilliant sessions with none, and I’ve been to horrid sessions from amazing people where they have no structure to what they are trying to show.

    4. Practice both the session and the demos.

    And I mean multiple times. The bigger the event, the more the whole session needs to be second nature. Try to deliver the session at smaller venues first. Local user groups, virtual sessions, etc. are good options for this.

    5. Find another presenter as a critical friend.

    I have friends that are talented presenters and I love having one of them in the room for trial runs, with a view of being critical. Someone that says “yeah that was great” is nice. Someone that says “you lost me in the second part of the demo” or “I think the third demo would work better if you…” is what you need. Be prepared to do the same for them.

    6. Record the demos.

    When presenting at large events, I have a series of screen shots saved on a USB key, and I also have a full video walkthrough of each of the demos. I’m determined that the audience will get to see the demos.

    As a simple example of when this has saved me, two years ago for TechEd Australia, I was presenting some Azure-related sessions. The Azure folk had decided to do maintenance and take things offline right in the middle of the event.

    I told the audience, switched across to the videos of each demo, which I did a live voice-over for, and I suspect that many of them would have quickly forgotten that they were watching a video. By comparison, I attended several other Azure-related sessions at that same event and watched presenter after presenter stumbling when things would not work. You need a fall back plan.

    Hint: Don’t just play the video with voice, etc. as well though – make it still pretty much a live thing. I’ve seen sessions where people just play a video with sound and it often looks like they could never have actually done the demo, particularly if it’s someone else’s voice.

    7. Don’t try to debug live.

    Unless it’s an obvious and trivial issue, you will do far more damage by trying to debug it. Attendees hate watching you stuff around trying to fix issues. You might feel great *if* you ever get it solved but you will have messed up your timing and possibly looked really, really bad in the meantime. And if you can’t solve it, you will have really messed up. Instead, move on and use your backup plan.

    Isn’t this what everyone does?

    It seems pretty basic to do these things but time and again, I see the opposite happening even at major events. I watched AzureConf the other day, and even the keynote had these issues. Having been involved in event keynotes and knowing what level of rehearsal normally goes into them, I can just imagine the discussions that went on later. They wouldn’t have been pretty.

    You can avoid being one of these statistics with just a bit of planning, and you’ll already be ahead of the pack.

  • Rebuild clustered columnstore indexes when they require maintenance

    For general index maintenance, our friend Old Hallengren has an awesome solution for most people: https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html

    We’ve started to have customers using clustered columnstore indexes in SQL Server 2014, and they need to be treated differently. I checked Ola’s latest scripts today to see what happens with columnstore indexes. It appears that the code ignores nonclustered columnstore indexes (ie: index type of 6), which makes sense as we need to rebuild them whenever the data changes, and in the meantime, the table is read-only. So that makes lots of sense.

    For other indexes (including clustered columnstore indexes which are updatable), the code uses the same avg_fragmentation_in_percent values from sys.dm_db_index_physical_stats. Unfortunately, that view doesn’t return any data for columnstore indexes of either type, so that’s not actually helpful.

    Niko has an awesome series of blog posts about columnstore indexes that I encourage you to read: http://www.nikoport.com/columnstore/ In part 36, he discusses what he thinks is needed for maintenance. The metrics that I want to use overlap the ones that he’s suggested in that article.

    With a clustered columnstore index, when a row is deleted, a flag in a bitmap is updated to indicate the deletion. The row is still present in the columnstore row group. When an update occurs, the same mechanism flags the row as deleted, then inserts the new version into the delta store.

    The main problems that will occur are when many rows have been deleted.

    In the code below, I’ve provided an implementation that will rebuild clustered columnstore indexes in a database when one of three criteria is met:

    • The overall percentage of deleted rows has exceeded a cutoff (I’ve suggested a 10% default)
    • The percentage of rows in any individual segment exceeds a cutoff (I’ve suggested a 20% default)
    • The number of segments that are completely empty (all rows have been deleted) has exceeded a cutoff (I’ve suggested none)

    This would work for both COLUMNSTORE and COLUMNSTORE_ARCHIVE compression.

    Hope it helps someone until Ola does his magic:

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

    -- Rebuild clustered columnstore indexes when necessary

    -- Dr Greg Low v1.0

     

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

    -- rebuild when more than supplied % of rows have been deleted

    -- rebuild when any segments contain more than supplied % deleted rows

    -- rebuild if more than supplied number of segments are empty

     

    DECLARE @DeletedTotalRowPercentage int = 10;   

    DECLARE @DeletedSegmentsRowPercentage int = 20;

    DECLARE @EmptySegmentsAllowed int = 0;         

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

     

    DECLARE @IndexesToRebuild TABLE (SchemaName sysname,

                                     TableName sysname,

                                     IndexName sysname);

     

    WITH ClusteredColumnstoreIndexes

    AS

    ( SELECT t.object_id AS ObjectID,

             SCHEMA_NAME(t.schema_id) AS SchemaName,

             t.name AS TableName,

             i.name AS IndexName

      FROM sys.indexes AS i

      INNER JOIN sys.tables AS t

      ON i.object_id = t.object_id

      WHERE i.type = 5

    ),

    RowGroups

    AS

    ( SELECT csrg.object_id AS ObjectID,

             csrg.total_rows AS TotalRows,

             csrg.deleted_rows AS DeletedRows,

             csrg.deleted_rows * 100.0 / csrg.total_rows AS DeletedPercentage,

             CASE WHEN csrg.total_rows = csrg.deleted_rows

                  THEN 1 ELSE 0

             END AS IsEmptySegment

      FROM sys.column_store_row_groups AS csrg

    ),

    IndexStats

    AS

    ( SELECT cci.ObjectID,

             cci.SchemaName,

             cci.TableName,

             cci.IndexName,

             SUM(rg.TotalRows) AS TotalRows,

             SUM(rg.DeletedRows) AS DeletedRows,

             SUM(rg.DeletedRows) * 100.0 / SUM(rg.TotalRows) AS DeletedPercentage,

             SUM(rg.IsEmptySegment) aS EmptySegments

      FROM ClusteredColumnstoreIndexes AS cci

      INNER JOIN RowGroups AS rg

      ON cci.ObjectID = rg.ObjectID

      GROUP BY cci.ObjectID, cci.SchemaName, cci.TableName, cci.IndexName

    )

    INSERT @IndexesToRebuild (SchemaName, TableName, IndexName)

    SELECT s.SchemaName, s.TableName, s.IndexName

    FROM IndexStats AS s

    WHERE s.DeletedPercentage > @DeletedTotalRowPercentage

    OR s.EmptySegments > @EmptySegmentsAllowed

    OR EXISTS(SELECT 1 FROM RowGroups AS rg

                       WHERE rg.ObjectID = s.ObjectID

                       AND rg.DeletedPercentage > @DeletedSegmentsRowPercentage);

     

    DECLARE @SchemaName sysname;

    DECLARE @TableName sysname;

    DECLARE @IndexName sysname;

    DECLARE @SqlCommand nvarchar(max);

     

    DECLARE IndexList CURSOR FAST_FORWARD READ_ONLY

    FOR

      SELECT SchemaName, TableName, IndexName

      FROM @IndexesToRebuild

      ORDER BY SchemaName, TableName, IndexName;

     

    OPEN IndexList;

     

    FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName;

     

    WHILE @@FETCH_STATUS = 0

    BEGIN

      SET @SqlCommand = N'ALTER INDEX ' + QUOTENAME(@IndexName)

                      + N' ON ' + QUOTENAME(@SchemaName)

                      + N'.' + QUOTENAME(@TableName)

                      + N' REBUILD PARTITION = ALL;';

      PRINT @SqlCommand;

      EXEC(@SqlCommand); 

      FETCH NEXT FROM IndexList INTO @SchemaName, @TableName, @IndexName;

    END;

     

    CLOSE IndexList;

    DEALLOCATE IndexList;

     

  • Invalid Quorum Configuration Warnings when failing over SQL Server Availability Group

    At a client site today and they asked me about a warning that they got every time they manually failed over their SQL Server availability group.

    It said: “The current WSFC cluster quorum vote configuration is not recommended for the availability group.” They were puzzled by this as they had a valid quorum configuration. In their case, they had a two node cluster using MNS (majority node set) and a fileshare witness.

    The problem with that message is that it is returned when the node voting weight is not visible.

    Windows Server 2008 failover clustering introduced node-based voting but later an option was provided to adjust the voting weight for each node. If the cluster is based on Windows Server 2008 or Windows Server 2008 R2, and KB2494036 has not been applied, even though each node has a vote, the utilities that check voting weight are not supplied a weight value. You can see this by querying:

    SELECT * FROM sys.dm_hadr_cluster_members;

    This will return a row for each cluster member but will have a missing vote weight.

    Applying the KB hotfix will make this DMV return the correct values, and will make this invalid warning disappear.

  • Online certification exams are now available in Australia

    I’ve been hoping this would happen for a while and now it’s here (in beta).

    Whenever I take a certification exam, I find it removes my ability to work for most of a day, so I tend to schedule myself for two or three exams in a day, to avoid the overhead. It also means that I tend to limit the number of exams that I would take.

    Online proctoring of exams changes all that for me. If I can just schedule an exam for lunch time or night, or weekend from my own office, I’ll be much more inclined to take more certification exams.

    There are some rules:

    • You'll be recorded—both video and audio—for the duration of the exam.
    • You can't take notes during the exam.
    • You can't eat, drink, or chew gum while you take the exam.
    • You can't take a break—for any reason.

    They seem reasonable to me and if you don’t like them, you can always attend an in-person exam. Better make sure you get to the rest room beforehand though Smile It’s not available in all countries yet but fortunately Australia is one of the countries in the list. I’m not sure how the countries are chosen because I notice that our Kiwi buddies aren’t in the list yet. I’m sure that will change over time.

    Regardless, this is a really good initiative. Well done Microsoft Learning.

    You’ll find more information here: https://www.microsoft.com/learning/en-us/online-proctored-exams.aspx

More Posts Next page »

This Blog

Syndication

Tags

No tags have been created or used yet.
Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement