Skip to content

Instantly share code, notes, and snippets.

View NoahDragon's full-sized avatar
I may be slow to respond.

Abner Chou NoahDragon

I may be slow to respond.
View GitHub Profile
@NoahDragon
NoahDragon / NonblockingReads.c
Last active February 3, 2016 21:51
Reading all the bytes from file-- from book "Linux System Programming"
/*
Snippet from: Linux System Programming
Purpose: read file to avoid system interuption
Relative: Nonblocking reads
*/
ssize_t ret;
while (len != 0 && (ret = read (fd, buf, len)) != 0 ) {
if(ret == -1){
@NoahDragon
NoahDragon / NullableInteger.vb
Last active February 3, 2016 21:51
VB.NET nullable integer assign nothing through If statement may result nothing=0 issue if another return variable is not nullable.
Imports System
Public Module Module1
Public Sub Main()
Dim a as Integer? = Nothing
Dim b as Integer = 5
Dim c as Integer? = 5
a = If(1=1, nothing, b)
Console.WriteLine(a) ' 0
a = Nothing
@NoahDragon
NoahDragon / CleanCache.sql
Created February 25, 2016 22:14
Clean the SQL Server cache while investigating performance.
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
@NoahDragon
NoahDragon / GetSPLastRunTime.sql
Last active February 25, 2016 22:19
Get SQL Server Stored Procedure last run time.
-- Check last time SP ran.
select b.name, a.last_execution_time
from sys.dm_exec_procedure_stats a
inner join sys.objects b
on a.object_id = b.object_id
where DB_NAME(a.database_ID) = DB_NAME() -- Current database or change it to your database name
@NoahDragon
NoahDragon / ShowSessionCode.sql
Created February 25, 2016 22:17
Show SQL Server Session Running Code
-- sp_who2 60
-- DBCC OPENTRAN() -- check open transactions in DB
SET NOCOUNT ON
DECLARE @SPID SMALLINT
DECLARE @WAIT TINYINT
DECLARE @NoLoop BIT
SET @SPID = 81 -- MODIFY to correct SPID.
@NoahDragon
NoahDragon / RecoveryTime.sql
Created February 25, 2016 22:21
Calculate SQL Server Database Recovery Time
--
-- SQL SERVER 2008
--
DECLARE @DBName VARCHAR(64) = DB_NAME() -- databasename
DECLARE @ErrorLog AS TABLE([LogDate] CHAR(24), [ProcessInfo] VARCHAR(64), [TEXT] VARCHAR(MAX))
INSERT INTO @ErrorLog
EXEC sys.xp_readerrorlog 0, 1, 'Recovery of database', @DBName
@NoahDragon
NoahDragon / Index.sql
Last active March 22, 2016 13:52
SQL Server Index Operations.
--
-- Disabled indexes.
--
select
sys.objects.name,
sys.indexes.name
from sys.indexes
inner join sys.objects on sys.objects.object_id = sys.indexes.object_id
where sys.indexes.is_disabled = 1
order by
@NoahDragon
NoahDragon / AllTableRowCount.sql
Created February 26, 2016 15:00
Get Row Count for All Tables in A Database.
declare @tableName VARCHAR(256), @id INT, @sql NVARCHAR(MAX)
set @id = 0
while(1=1)
begin
SELECT Top 1
@tableName = name,
@id = object_ID
FROM sys.objects
WHERE name LIKE 'Import%' -- Change to the table you would like to get the count.
AND type = 'U'
@NoahDragon
NoahDragon / readlines.js
Created March 18, 2016 15:21
Nodejs read file line by line.
module.exports = {
readlines : function (filename){
var fs = require('fs');
if (fs.existsSync(filename)) {
return fs.readFileSync(filename).toString().split(/\r?\n/);
}
else {
return null;
}
@NoahDragon
NoahDragon / loopDir.js
Last active June 16, 2016 19:35
Loop through a directory.
var fs = require("fs");
// General function
var dive = function (dir, action) {
// Assert that it's a function
if (typeof action !== "function")
action = function (error, file, path) { };
// Read the directory
fs.readdir(dir, function (err, list) {