Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 02:38 (UTC -07:00)
View GitHub Profile
@StevenJL
StevenJL / static-content-nginx.conf
Last active January 15, 2018 03:00
Static Nginx Configuration
# The error_log directive sets the location of the nginx error
# log as well as the security level.
error_log /var/log/nginx/error.log warn;
# Specifies the number of worker processes spawned by the master
# process. It's these worker processes that listen to sockets and
# accepts the incoming connections. Should be set equal to the
# number of cores on the machine.
worker_processes 8;
@StevenJL
StevenJL / nginx.conf
Last active January 6, 2018 20:46
Basic Nginx Conf
error_log /var/log/nginx/error.log warn;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# Suppose we want to select from these n emails those that
# are not already in the `users` table.
emails = [
"[email protected]",
"[email protected]",
"[email protected]",
...
"[email protected]"
]
# When creating a lot of records from an array of hashes
new_users = [
{name: "Teemo", email: "[email protected]"},
{name: "Heim", email: "[email protected]"},
{name: "Annie", email: "[email protected]" },
...
{name: "Volli", email: "[email protected]" },
]
# Do not create each one individually as it results in one query, per record.
# If we care to know a record exists, but actually don't
# need to do anything with the record...
if User.where(email: "[email protected]").present?
puts "There is a user with email address [email protected]"
else
puts "There is no user with email address [email protected]"
end
# then using `present?` is wasteful, as it selects all the column
# and loads the object into memory.
# SELECT * FROM users WHERE email` = '[email protected]';
# Deleting a large amount of records by invoking `destroy` on
# each record results in a single query per record.
deadbeat_users = User.where(payment_status: "deadbeat")
deadbeat_users.each do |deadbeat|
deadbeat.destroy
end
# DELETE FROM users WHERE id = 13;
# DELETE FROM users WHERE id = 42;
# DELETE FROM users WHERE id = 49;
# DELETE FROM users WHERE id = 420;
# Suppose we load all user records, instantiate them as `User` objects
# only to select one attribute. This is wasteful since we select all
# columns from the database and load them all into memory.
user_emails = User.where(status: "active").map(&:email)
# SELECT * FROM users;
# Instead, we can use `pluck` to modify the query to select only the
# column(s) we need. It also returns the attributes as an array of strings
# instead of `User` objects, reducing memory use.
user_emails = User.where(status: "active").pluck(:email)
# Selecting all users
User.all.each do |user|
notify(user)
end
# Generates this query:
# SELECT * FROM users;
# Selecting all users with `find_each` will query by batches of size
# determined by the :batch_size option (defaults to 1000)
# Eager-load all users' posts with `includes`
users = User.includes(:posts)
users.each do |user|
user.posts
end
# This will only run 2 queries.
# SELECT * FROM users;
# SELECT * FROM posts WHERE posts.user_ids IN (1,2,3,4,....,n);
# Assuming User has the following association
class User
has_many :posts
end
# Running this activerecord code
users = User.all
users.each do |user|
user.posts