Monday, August 17, 2020

Rotating numbers for base 2

using System;
					
public class Program
{
	public static void Main()
	{
		ShowCircle(32);
		
		ShowCircle(512);	
	}
	
	static void ShowCircle(int n) {
		int masker = n - 1;
		for (int i = -(n * 2); i <= n * 2; ++i) {
			Console.WriteLine("{0,6}: {1,6} {2,6} {3,6}", n, i, i & masker, (i % n + n) % n);
		}
	}
}

https://dotnetfiddle.net/DedpF2

Friday, March 13, 2020

Parameter Sniffing

Parameter sniffing with repro steps:
https://www.red-gate.com/simple-talk/sql/t-sql-programming/parameter-sniffing/


Other good articles explaining parameter sniffing:

https://www.brentozar.com/archive/2013/06/the-elephant-and-the-mouse-or-parameter-sniffing-in-sql-server/

https://hackernoon.com/why-parameter-sniffing-hurts-your-sql-query-performance-d73c0da71fbc


I encountered that problem some time in 2014, if I remember correctly I was able to solve that problem using this:

DROP PROC [dbo].[DisplayBillingInfo]
GO
CREATE PROC [dbo].[DisplayBillingInfo]
  @BeginDate DATETIME,
  @EndDate DATETIME
WITH RECOMPILE
AS 
DECLARE @StartDate DATETIME;
DECLARE @StopDate DATETIME;
SET @StartDate = @BeginDate;
SET @StopDate = @EndDate;
SELECT BillingDate, BillingAmt
  FROM BillingInfo
  WHERE BillingDate between @StartDate AND @StopDate; 



Basically, it prevents SQL Server from sniffing the parameter since it can't identify the parameter values used in the SELECT statement as the WHERE clause is not directly using the parameter variable that passed to it, thereby making SQL Server create a different execution plan every time the stored procedure is executed instead of reusing old execution plan that could otherwise be non-optimal for a given set of parameter values.