1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-17 22:18:01 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
jbranchaud
53931955d2 Add Get ActiveRecord Attribute Directly From Database as a Rails til 2022-03-28 10:44:12 -05:00
jbranchaud
dc8f1a52c9 Add Get The Size On Disk Of An Index as a Postgres til 2022-03-28 09:29:13 -05:00
jbranchaud
31af7235c2 Add Diagnose Problems In A Heroku Postgres Database as a Heroku til 2022-03-07 09:34:19 -06:00
4 changed files with 92 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1187 TILs and counting..._
_1190 TILs and counting..._
---
@@ -339,6 +339,7 @@ _1187 TILs and counting..._
### Heroku
- [Deploy A Review App To A Different Stack](heroku/deploy-a-review-app-to-a-different-stack.md)
- [Diagnose Problems In A Heroku Postgres Database](heroku/diagnose-problems-in-a-heroku-postgres-database.md)
- [Set And Show Heroku Env Variables](heroku/set-and-show-heroku-env-variables.md)
- [SSH Into Heroku Server Hosting App](heroku/ssh-into-heroku-server-hosting-app.md)
@@ -591,6 +592,7 @@ _1187 TILs and counting..._
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
- [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md)
- [Get A Quick Approximate Count Of A Table](postgres/get-a-quick-approximate-count-of-a-table.md)
- [Get The Size On Disk of An Index](postgres/get-the-size-on-disk-of-an-index.md)
- [Get The Size Of A Database](postgres/get-the-size-of-a-database.md)
- [Get The Size Of A Table](postgres/get-the-size-of-a-table.md)
- [Get The Size Of An Index](postgres/get-the-size-of-an-index.md)
@@ -720,6 +722,7 @@ _1187 TILs and counting..._
- [Find Records With Multiple Associated Records](rails/find-records-with-multiple-associated-records.md)
- [Force All Users To Sign Out](rails/force-all-users-to-sign-out.md)
- [Generating And Executing SQL](rails/generating-and-executing-sql.md)
- [Get ActiveRecord Attribute Directly From Database](rails/get-active-record-attribute-directly-from-database.md)
- [Get An Array Of Values From The Database](rails/get-an-array-of-values-from-the-database.md)
- [Get An Empty ActiveRecord Relation](rails/get-an-empty-activerecord-relation.md)
- [Get The Column Names For A Model](rails/get-the-column-names-for-a-model.md)

View File

@@ -0,0 +1,23 @@
# Diagnose Problems In A Heroku Postgres Database
Heroku keeps track of all kinds of diagnostics on the performance of your app's
database. From long running queries and transactions to index cache hit rates
to unused indexes to tables with bloat.
Running the `pg:diagnose` command for your Heroku app will surface all of these
details in the terminal.
```bash
$ heroku pg:diagnose -a APP_NAME
```
The report will start with the biggest problem areas which it color codes in
red. If your app is experiencing degraded performance, the list of red items
would be a good place to start investigating.
The report will then list less urgent problem areas. Those will be color coded
yellow. Though Heroku has deemed these less serious, you may still want to deal
with these.
All the other areas of diagnose will fall into the _green_ bucket. Meaning
Heroku doesn't see any issues in those areas.

View File

@@ -0,0 +1,29 @@
# Get The Size On Disk Of An Index
Indexes, when added to the right columns, can provide massive performance gains
for certain queries. Indexes aren't free though. It is worth noting that they
take up disk space. The amount of disk space they take is generally irrelevant,
but it is at least worth being aware of. Especially if you're deal with a
massive table.
You can check the current size of an index on disk with [PostgreSQL's
`pg_relation_size`
function](https://www.postgresql.org/docs/current/functions-admin.html).
First, you'll want to look up the name of the index you're curious about.
Running `\d table_name` (replacing `table_name` with the name of the table the
index is on) will show you everything about the table including its indexes and
their names.
Then run the following query:
```sql
select pg_size_pretty(pg_relation_size('index_users_on_email'));
pg_size_pretty
----------------
41 MB
(1 row)
```
This one is pretty small. They can get pretty big though. I've seen some that
take up over 1GB on disk.

View File

@@ -0,0 +1,36 @@
# Get ActiveRecord Attribute Directly From Database
In Rails, an ActiveRecord model will automatically get methods named after each
column in the backing database table. This can be called to retrieve those
values from the respective columns in the database.
What if you wanted to override and alter one of those values? For example,
ensure the `email` value you're passing around is always fully downcased.
Something like this won't quite work.
```ruby
def email
email.downcase
end
```
Because the method is named `email`, the `email` reference inside it will call
itself, recursively, until it exceeds the stack.
Instead, you need a way of referencing the email attribute that is stored in
the database.
[`attribute_in_database`](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html#method-i-attribute_in_database)
will do the trick.
```ruby
def email
attribute_in_database('email').downcase
end
```
That will retrieve the value from the `email` column in the database for this
record, downcase it, and return it. Anyone calling `email` won't notice the
difference.
h/t [Dillon Hafer](https://twitter.com/dillonhafer)