Skip to content

Instantly share code, notes, and snippets.

View ronascentes's full-sized avatar

Rodrigo Nascentes ronascentes

View GitHub Profile
@ronascentes
ronascentes / checkStatistics.sql
Created December 4, 2019 19:01
Use this T-SQL script to generate the complete list of tables that need statistics update in a given database
;WITH StatTables AS(
SELECT so.schema_id AS 'schema_id', so.name AS 'TableName', so.object_id AS 'object_id', ISNULL(sp.rows,0) AS 'ApproximateRows', ISNULL(sp.modification_counter,0) AS 'RowModCtr'
FROM sys.objects so (NOLOCK) JOIN sys.stats st (NOLOCK) ON so.object_id=st.object_id
CROSS APPLY sys.dm_db_stats_properties(so.object_id, st.stats_id) AS sp
WHERE so.is_ms_shipped = 0 AND st.stats_id<>0
AND so.object_id NOT IN (SELECT major_id FROM sys.extended_properties (NOLOCK) WHERE name = N'microsoft_database_tools_support')
),
StatTableGrouped AS(
SELECT ROW_NUMBER() OVER(ORDER BY TableName) AS seq1, ROW_NUMBER() OVER(ORDER BY TableName DESC) AS seq2, TableName, cast(Max(ApproximateRows) AS bigint) AS ApproximateRows,
cast(Max(RowModCtr) AS bigint) AS RowModCtr, count(*) AS StatCount, schema_id,object_id
@ronascentes
ronascentes / killAllConnections.sql
Created January 11, 2020 21:42
Kill all the current connections of a SQL instance
DECLARE @session_id INT, @sql NVARCHAR (4000);
DECLARE database_curs CURSOR FAST_FORWARD FOR
SELECT c.session_id
FROM sys.dm_exec_connections AS c
JOIN sys.dm_exec_sessions AS s ON c.session_id = s.session_id
WHERE c.session_id <> @@SPID AND s.is_user_process = 1;
OPEN database_curs;
FETCH NEXT FROM database_curs INTO @session_id;
WHILE (@@fetch_status <> -1)
BEGIN
@ronascentes
ronascentes / if_null.ps1
Created April 20, 2021 19:46
Validating null value in powershell
# If $value is not $null or 0 or $false or an empty string
if ($null -ne $value -and $value -ne 0 -and $value -ne '' -and $value -ne $false ){
:do
}
@ronascentes
ronascentes / digon.json
Created September 24, 2021 18:50
Windows Terminal Digon Color Scheme
{
"background": "#1B1D1E",
"black": "#222D3F",
"blue": "#3167AC",
"brightBlack": "#a4eeb7",
"brightBlue": "#3C7DD2",
"brightCyan": "#35B387",
"brightGreen": "#2D9440",
"brightPurple": "#8230A7",
"brightRed": "#D4312E",
@ronascentes
ronascentes / PSTop.ps1
Created January 7, 2022 17:38
Poor man's "top" for Windows
while($true) { cls ; '' ; ps | sort cpu -Descending | select -first 20 | ft -auto; Start-Sleep -Milliseconds 1000 }
@ronascentes
ronascentes / ConnectionPoolDebuggingLogicForMongoDB.cs
Created February 16, 2022 18:55
How to enable C# connection pool debugging logic for MongoDB
var clientSettings = new MongoClientSettings();
var traceSource = new TraceSource("MongoDB-CMAP-SDAM", SourceLevels.All);
traceSource.Listeners.Clear(); // remove the default listener
var logFileName = "<should be updated on the real path to a log file>";
var listener = new TextWriterTraceListener(new FileStream(logFileName, FileMode.Append));
listener.TraceOutputOptions = TraceOptions.DateTime;
traceSource.Listeners.Add(listener);
var eventSubscriber = new TraceSourceEventSubscriber(traceSource);
Action<ClusterBuilder> clusterConfigurator = builder => builder.Subscribe(eventSubscriber); // if you don't use a MongoClient as singletone, it's better to share the same instance(so object.ReferenceEquals for them should return true) of this configurator, between all mongo clients
clientSettings.ClusterConfigurator = clusterConfigurator;
@ronascentes
ronascentes / Singleton.ps1
Created July 30, 2024 13:51 — forked from dfinke/Singleton.ps1
PowerShell Singleton Pattern: Ensure single instance for shared resource management
# Singleton
class Product {
$Name
Product($name) {
$this.Name = $name
}
}
@ronascentes
ronascentes / ChatMediator.ps1
Created July 30, 2024 13:53 — forked from dfinke/ChatMediator.ps1
Demonstrates the implementation of the Mediator Design Pattern in PowerShell
<#
Define an object that encapsulates how a set of objects interact.
Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
#>
class ChatMediator {
$users
ChatMediator() {
$this.users = New-Object System.Collections.ArrayList
@ronascentes
ronascentes / StartComputer.ps1
Created July 30, 2024 13:53 — forked from dfinke/StartComputer.ps1
PowerShell example demonstrating the Facade Design Pattern implementation
class CPU {
freeze() { "$this freezing" | Out-Host }
jump($position) { "$this jump to $position" | Out-Host }
execute() { "$this execute" | Out-Host }
}
class Memory {
load($position, $data) {
"$this load $position $data" | Out-Host
class Command {
execute() {
"[$(get-date)] Logging execute of command [$this]" | Out-Host # Logs the execution of the command
}
}
class Loony : Command {
execute() {
([Command]$this).execute() # Calls the execute method of the base class (Command)
"You're a loony." | Out-Host # Outputs "You're a loony."