Skip to content

Instantly share code, notes, and snippets.

View jaylevitt's full-sized avatar

Jay Levitt jaylevitt

View GitHub Profile
SELECT tappable_id, tappable_type, user_id, created_at, c
FROM (
SELECT tappable_id, tappable_type, user_id, created_at,
row_number() over(partition by tappable_id order by created_at),
count(user_id) over(partition by tappable_id order by created_at) as c
FROM taps
) as ss
WHERE row_number = 1 order by c desc;
typedef struct NDBOX
{
int32 vl_len_; /* varlena header (do not touch directly!) */
unsigned int dim;
double x[1];
} NDBOX;
double
similarity(double *a, double *b)
{
@jaylevitt
jaylevitt / gist:1829170
Created February 14, 2012 18:56
update survey_responses mapping
create temp table mapping as
SELECT sr.id as survey_response_id, st.user_id
FROM survey_responses AS sr
JOIN survey_takings AS st
ON st.id = sr.survey_taking_id;
create index on mapping (survey_response_id);
explain update survey_responses as sr
set user_id = mapping.user_id
from mapping
@jaylevitt
jaylevitt / crosstab.sql
Created February 8, 2012 18:31
crosstab syntax
rails_dev=# \d sub_scale_scores
Table "public.sub_scale_scores"
Column | Type | Modifiers
--------------+------------------+-----------
user_id | integer | not null
sub_scale_id | integer | not null
average | double precision |
Indexes:
"sub_scale_scores_pkey" PRIMARY KEY, btree (user_id, sub_scale_id)
"index_sub_scale_scores_on_sub_scale_id" btree (sub_scale_id)
@jaylevitt
jaylevitt / slowcube.sql
Created February 7, 2012 17:35
slow cube tables
\c postgres
drop database if exists slowcube;
create database slowcube;
\c slowcube
\timing
create schema slowcube;
set search_path to slowcube;
create extension cube;
@jaylevitt
jaylevitt / gist:1754803
Created February 6, 2012 20:57
breaks everything else
rails_dev=# \i neighborcube.sql
You are now connected to database "postgres" as user "jay".
DROP DATABASE
CREATE DATABASE
You are now connected to database "jaytest" as user "jay".
CREATE SCHEMA
SET
Timing is on.
CREATE EXTENSION
Time: 17.912 ms
@jaylevitt
jaylevitt / gist:1754527
Created February 6, 2012 20:12
slow create gist index on unlogged table
\c postgres
drop database if exists jaytest;
create database jaytest;
\c jaytest
create schema jaytest;
set search_path to jaytest;
create extension cube;
@jaylevitt
jaylevitt / gwssh.rb
Created December 30, 2011 19:12
Gateway ssh
#!/usr/bin/env ruby
# gwssh: ssh via a gateway host. Just use gwssh instead of ssh,
# and your options, commands, etc. will be preserved.
GATEWAY_HOST = 'gate.example.com'
DOMAIN = 'example.com'
DEBUG = false
OPTIONS_WITH_ARGUMENTS = 'bcDeFiLlmOopRSw'
@jaylevitt
jaylevitt / gist:1540691
Created December 30, 2011 17:26
Parsing arbitrary options to wrap ssh
#!/usr/bin/env ruby
# gwssh: ssh via a gateway host
# We want to find the hostname so we can add tiptap.com. The hostname will be the
# first string in ARGV that isn't a standalone option and that isn't itself the argument
# for an option (e.g. in "-F configfile", configfile is not the hostname).
# This feels very hacky, and I wonder if there's a more elegant way.
@jaylevitt
jaylevitt / gist:1522146
Created December 26, 2011 21:34
ActiveRecord SQL sanity/izer
class ActiveRecord::Base
def self.escape_sql(clause, *rest)
self.send(:sanitize_sql_array, rest.empty? ? clause : ([clause] + rest))
end
def self.execute_sql(conditions, *rest)
self.connection.execute(escape_sql(conditions, *rest))
end
end