Skip to content

Instantly share code, notes, and snippets.

View all4miller's full-sized avatar

Angus Miller all4miller

View GitHub Profile
drop table if exists datadz;
create table if not exists datadz (
datestmp date,
user_id integer,
stat double precision
);
insert into datadz
select date(datestmp),
drop table if exists datatz;
create table if not exists datatz (
timestmp timestamp,
user_id integer,
stat double precision
);
insert into datatz
select timestmp,
# BAD each iteration loads invoice model
class Company < ApplicationRecord
has_many :invoices
end
class Invoice < ApplicationRecord
belongs_to :company
end

Given the below PostgreSQL compatible SQL

with calendar
as (
	select t.day::date,
		trunc(random() * 3 + 1) max_version_daily,
		trunc(random() * 3 + 1) max_version_hourly
	from generate_series(timestamp '2010-01-01', timestamp '2022-01-31', interval '1 day') as t(day)
	),
"browserify": {
"transform": [
[
"coffeeify",
{
"extension": ".js.coffee"
}
],
[
"babelify",
drop table if exists shipments;
create table shipments (
id serial primary key not null,
sender_address_id integer not null,
sender_contact_id integer not null,
receiver_address_id integer not null,
receiver_contact_id integer not null,
created_at timestamp not null default current_timestamp,
units integer not null,
zar_value numeric not null
create view dbo.FuckingSlow
as
select 'derivatives' as section,
Rtrim(Accounts.indicator)
+ Rtrim(Departments.indicator)
+ Rtrim(Products.indicator) as 'Account',
Rtrim(Accounts.indicator)
+ Rtrim(Departments.indicator)
+ Rtrim(Products.indicator) + '0000'
+ Rtrim(Derivatives.indicator) as 'FullAccount',
# frozen_string_literal: true
require 'benchmark'
require 'benchmark/ips'
require 'benchmark/memory'
require 'csv'
N_NUMBER_OF = 100_000
ary = Array.new(N_NUMBER_OF) { ['one', 'two', 'three', 'four', 'five', 'six'] }
def average_time_in_hours_to_2_or_more_views
Businesss.where.not(date_achieved_2_views: nil).pluck('sum(abs(extract(epoch from created_at - date_achieved_2_links)/3600)) / count(*)').first
end
def average_time_in_hours_to_2_or_more_view
businesses = Businesss.where.not(date_achieved_2_views: nil)
hours_sum = 0
businesses.map do |business|
hours_sum += TimeDifference.between(business.created_at, business.date_achieved_2_views).in_hours
end
return nil if hours_sum.blank? || businesses.count.blank?
hours_sum / businesses.count
end