Skip to content

Instantly share code, notes, and snippets.

@dudeNumber4
dudeNumber4 / ShardManagement.cs
Last active October 24, 2018 15:31
Azure Shard Management
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement; // nuget package
using System;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
namespace ISharded.Data
{
@dudeNumber4
dudeNumber4 / ShardContext.cs
Created October 24, 2018 15:32
Entity Framework Context Constructor for Azure Shard
/// <summary>
/// Create EF context that will connect to Azure shard.
/// </summary>
/// <param name="operational">True if connecting for operational data (as opposed to historical). My solution only contains 2 shards so I can use bool. This could, for instance, simply be the integer parameter of the needed shard.</param>
public ShardContext(bool operational) : base(ShardManagement.GetRegularConnection(operational), true)
{
}
@dudeNumber4
dudeNumber4 / ShardContextSample.cs
Created October 24, 2018 15:35
Sample Usage of ShardContext
ShardManagement.TrySetShardMapManager();
// true in this sample means first shard (operational).
using (var context = new ShardContext(true))
{
var customer = context.Customers.FirstOrDefault();
if (customer != null)
{
Console.WriteLine($"First operational customer: {customer.CustomerId}");
}
----------------------------------- Create a regular Table for Demo ------------------------------------------------------------------------
-- Adapted from https://www.mssqltips.com/sqlservertip/2888/how-to-partition-an-existing-sql-server-table/
--Table/Index creation. Create table as it would exist before partitioning.
CREATE TABLE [dbo].[TABLE1]
([pkcol] [int] NOT NULL,
[datacol] [int] NULL,
[partitioncol] datetime)
GO
ALTER TABLE dbo.TABLE1 ADD CONSTRAINT PK_TABLE1 PRIMARY KEY CLUSTERED (pkcol)
GO
---------------------------------- Change database name [Sandbox] to whatever you please (or create it) ------------------------------------------
---------------------------------- Change paths to files below to whatever you please. ------------------------------------------
---------------------------------- Cleanup shrinkfile commands at very bottom assume file names as well. ------------------------------------------
---------------------------------- Create Files/File Groups -------------------------------------------------------------------------
-- We will want 2 new filegroups/files four our partitions:
-- PRIMARY: Partition 2; operational data
-- History: Partition 1; historical data
-- Split: Partition N; temp during split/merge of partitions.
ALTER DATABASE [Sandbox] ADD FILEGROUP [History]
@dudeNumber4
dudeNumber4 / GetExecutableStatementFromQueryPlan.sql
Last active March 18, 2019 16:14
Get Executable Statement From TSQL Query Plan
-- Helper function. Drop If Exists depends on v16.
drop function if exists RemoveLeadingTuple;
go
create function RemoveLeadingTuple(@s varchar(max)) returns varchar(max) as
-- If @s contains a leading tuple, e.g., '((xx))', remove the string with that tuple removed
begin
declare @result varchar(max) = @s;
declare @leftParens int = 0;
declare @rightParens int = 0;
if (LEFT(@s, 1) = '(')
$CypressCommand = 'npx cypress run --spec ' + "'" + $('$(Build.SourcesDirectory)').Replace('\', '/') + '/AutomatedTests/Cypress/cypress/integration/ci_test.js' + "'"
Invoke-Expression $CypressCommand
@dudeNumber4
dudeNumber4 / GetPartitionQueryDate.sql
Created April 26, 2019 19:24
A simple function returning a date for partition queries.
create function GetPartitionQueryDate() returns date as
-- Provides the date that we will always query partitioned tables on.
-- Dates within the past 90 days should always be in the operational partition of any partitioned table.
begin
return GETDATE() - 90
end
@dudeNumber4
dudeNumber4 / ShowPartitionRowCounts.sql
Created April 26, 2019 19:25
Script to show table partition row counts.
-- Show partition row counts
SELECT $PARTITION.PartitionTable_PartitionFunc(PartitionedColumn) as Partition_Number, COUNT(1) as Row_Count
FROM PartionedTable
GROUP BY $PARTITION.order_PartitionFunc(PartitionedColumn)
@dudeNumber4
dudeNumber4 / metadata.json
Last active November 13, 2021 00:33
LoanSample
{"valueParameterInfo":[],"slotParameterDescriptions":[],"roleDescriptions":[],"contractType":"Other","contractShortDescription":"","contractName":"","contractLongDescription":"","choiceInfo":[]}