This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error_log /var/log/nginx/error.log warn; | |
events { | |
use epoll; | |
worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]" | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |