Skip to content

Instantly share code, notes, and snippets.

View all4miller's full-sized avatar

Angus Miller all4miller

View GitHub Profile
def process_past_48_hours
biz_ids = Businesss.where.not(id: 10).where(flag: true).pluck(:id)
biz_ids += Cards.where(state: 1).pluck(:biz_id)
biz_ids = biz_ids.uniq
# 4720 biz_ids recrds
h = {}
st = (DateTime.now - 48.hours).beginning_of_hour
en = ((st + 48.hours) - 1.second)
Businesss.where(id: biz_ids).order(:plan).each do |business|
def process_past_48_hours_fast
st = (DateTime.now - 48.hours).beginning_of_hour
en = ((st + 48.hours) - 1.second)
Business.connection.select_all("
with biz as (
select businesses.id,
coalesce(businesses.cpc_in_dollars, 0.03) cpc_in_dollars,
businesses.fee_in_dollars,
businesses.plan
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
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
# 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'] }
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',
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
"browserify": {
"transform": [
[
"coffeeify",
{
"extension": ".js.coffee"
}
],
[
"babelify",

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)
	),
# BAD each iteration loads invoice model
class Company < ApplicationRecord
has_many :invoices
end
class Invoice < ApplicationRecord
belongs_to :company
end