Skip to content

Instantly share code, notes, and snippets.

@mikeblas
mikeblas / generate_attaches.sql
Last active September 3, 2024 07:34
get `sp_attach_db` scripts for all databases
-- due to https://www.mssqltips.com/sqlservertip/1786/auto-generate-sql-server-database-attach-and-detach-scripts/
-- modified to use NVARCHAR for names
SET NOCOUNT ON
DECLARE @cmd NVARCHAR(MAX),
@dbname NVARCHAR(200),
@prevdbname NVARCHAR(200)
SELECT @cmd = N'', @dbname = N';', @prevdbname = N''
@mikeblas
mikeblas / AllIndexSizes.sql
Created July 22, 2024 22:20
Sizes of all indexes
-- get the row count and space used stats for each index
-- in this database
SELECT
s.Name as SchemaName, t.Name as TableName, i.name as IndexName,
p.Rows as RowCounts, SUM(AU.Total_pages) * 8.0/1024 AS TotalSpaceMB,
SUM(AU.used_pages) * 8.0/1024 AS UsedSpaceMB,
(SUM(AU.Total_Pages) - SUM(AU.used_pages)) * 8.0/1024 AS UnusedSpaceMB
FROM sys.tables AS T
@mikeblas
mikeblas / gist:da086ba431c94cf7a1329985d851ab48
Created July 15, 2024 15:18
get SQL Server database list with files, and file sizes
-- get files for each database (both log and rows)
-- including the sizes of those files
-- and growth settings
SELECT
db.name AS [Database Name],
mf.name AS [Logical Name],
mf.type_desc AS [File Type],
mf.physical_name AS [Path],
@mikeblas
mikeblas / skill_rank.md
Created November 24, 2023 19:44
exponential skill ranking

This might kind of seem harsh, but I think it takes into account that there's a lot of knowledge to have in most subjects, and most people take very high levels of knowlege for granted. When I recruit, and when I train people on hiring, I propose a very curious rating on a zero to ten scale:

  1. don't know how to spell it
  2. heard of it
  3. wandering around the bookstore to find a book on it
  4. did some tutorials, half a clue
  5. used it a bit
  6. familiar with most stuff about it
  7. did a very substantial project with it
@mikeblas
mikeblas / sqlsills.md
Created December 4, 2022 02:59
SQL Skills

"SQL skills" is a bit of a trigger phrase for me because it's very much overloaded, since SQL work ranges from developing a database (that is, writing the database server itself) through designing a database (using a DBMS someone else wrote to implement a data system), through writing queries and doing DBA work. People tend to lump these things together without carefully understanding what job they're looking for (or what candidate they want to hire) or how to best build a data team or ...

Anyway! Since you asked specifically about querying, I can leave out all the rest:

A beginner:

  • Knows the tool. Can setup and use a command-line tool, a GUI tool, knows a couple of each at least. Can diagnose connection problems. Understands how to save, load, manipulate files.
@mikeblas
mikeblas / MySQLSampleData.py
Created September 15, 2022 18:17
generate some sample data for a Reddit question about MySQL Performance
import random
def build_data(tablename, filename, rows):
with open(filename, 'w') as outfile:
outfile.write(f"INSERT INTO {tablename} (id, type, category_id) VALUES\n")
type_list = ['pet', 'plant', 'book', 'snack', 'tool']
for x in range(rows):
if x > 0:
outfile.write(",\n")
@mikeblas
mikeblas / SampleData.md
Last active December 22, 2024 03:16
FAQ: Where can I get sample data?

It's not so hard to find sample data and data sources to use for interesting side-projects, or just for practicing writing SQL.

In-product sample data

Most DBMSes come with sample databases. You can write lots of interesting queries against them, and usually a tutorial accompanies the database in the documentation.

@mikeblas
mikeblas / in_flight_queries.sql
Created November 27, 2020 15:52
SQL Server: show currently running queries by CPU time
-- in flight queries by CPU time
-- author: [email protected]
-- Date: 2020-11-27
SELECT s.session_id,
DB_NAME(r.database_id) as DBName,
r.status,
r.blocking_session_id 'Blk by',
r.wait_type,