Skip to content

Commit fbde425

Browse files
committed
finish the readme
1 parent 73d55e5 commit fbde425

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

README.md

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,79 @@ The package can be installed by adding `ecto_sqlite3_extras` to your list of dep
99
```elixir
1010
def deps do
1111
[
12-
{:ecto_sqlite3_extras, "~> 1.0.0"}
12+
{:ecto_sqlite3_extras, "~> 1.1.5"}
1313
]
1414
end
1515
```
1616

1717
## Integrating with Phoenix Live Dashboard
1818

19-
...
19+
When you have `ecto_sqlite3_extras` in the list of dependencies for your [Phoenix](https://www.phoenixframework.org/) project that uses [ecto_sqlite3](https://github.com/elixir-sqlite/ecto_sqlite3), the [Phoenix Live Dashboard](https://github.com/phoenixframework/phoenix_live_dashboard) will automatically show the tables produced by `ecto_sqlite3_extras` in the "Ecto Stats" tab. Magic!
2020

2121
![Example of live dashboard with ecto_sqlite3_extras](./assets/live-dashboard.png)
2222

2323
## Using from Elixir
2424

25-
...
25+
If you don't have Phoenix Live Dashboard on the environment you want to inspect, you can use `ecto_sqlite3_extras` directly from the [iex](https://hexdocs.pm/iex/1.14/IEx.html) shell.
26+
27+
```elixir
28+
# run the query and print a nice ASCII table into stdout
29+
EctoSQLite3Extras.table_size(MyProject.Repo)
30+
31+
# get the raw output of the query
32+
EctoSQLite3Extras.table_size(MyProject.Repo, format: :raw)
33+
34+
# run the query on a remote node
35+
EctoSQLite3Extras.table_size({MyProject.Repo, self()})
36+
```
2637

2738
![Example of ecto_sqlite3_extras usage with iex](./assets/iex.png)
2839

2940
## Available queries
3041

42+
1. `total_size`. Total size of all tables and indices. It's a summary talbe, it has only 2 columns: `name` and `value`. Rows:
43+
1. `cells`: The number of cells in the DB. Each value stored in the DB is represented as at least one cell. So, the number of cells correlates with the number of records in the DB.
44+
1. `payload_size`: How much space the actual useful payload takes in the DB.
45+
1. `unused_size`: How much space in the DB is reserved, not used yet, and can be used later to store more data. This is a surplus that occurs because SQLite allocates space for data in chunks ("pages").
46+
1. `vacuum_size`: How much space is unused and cannot be used for the future data. You can run [VACUUM](https://www.sqlite.org/lang_vacuum.html) command to reduce it.
47+
1. `page_size`: The total space occupied by all pages. Each page is a single chunk of space allocated by SQLite. This number is the sum of `payload_size`, `unused_size`, and `vacuum_size`.
48+
1. `pages`: The total number of pages.
49+
1. `pages: leaf`: The pages that store the actual data. Read [SQLite Internals: Pages & B-trees](https://fly.io/blog/sqlite-internals-btree/) to learn more.
50+
1. `pages: internal`: The pages that store ranges for leaf pages for a faster lookup. Sometimes also called "interior pages".
51+
1. `pages: overflow`: The pages that store chunks of big data that doesn't fit in a single leaf page.
52+
1. `pages: table`: The pages used for storing data for tables.
53+
1. `pages: index`: The pages used for storing indices.
3154
1. `table_size`. Information about the space used (and unused) by all tables. Based on the [dbstat](https://www.sqlite.org/dbstat.html) virtual table.
32-
1. `name`: the table name.
33-
1. `payload_size`:
34-
1. `unused_size`:
35-
1. `page_size`:
36-
1. `cells`:
37-
1. `pages`:
38-
1. `max_payload_size`:
39-
1. `index_size`.
40-
1. `sequence_number`.
41-
1. `table_name`:
42-
1. `sequence_number`:
43-
1. `settings`. List values of PRAGMAs (settings). Only includes the ones that have an integer or a boolean value. For bravity, the ones with `0` (`false`) value are excluded from the output (based on the observation that this is the default value for most of the PRAGMAs). Check out the SQLite documentation to learn more about what each PRAGMA means: [PRAGMA Statements](https://www.sqlite.org/pragma.html).
55+
1. `name`: The table name.
56+
1. `payload_size`.
57+
1. `unused_size`.
58+
1. `vacuum_size`.
59+
1. `page_size`.
60+
1. `cells`.
61+
1. `pages`.
62+
1. `max_payload_size`: The size of the biggest payload in the table.
63+
1. `index_size`. Size of all indices.
64+
1. `name`: The index name.
65+
1. `table_name`: The table where the index is defined.
66+
1. `column_name`: The name of the column being indexed. This columns is NULL if the column is the rowid or an expression.
67+
1. `payload_size`.
68+
1. `unused_size`.
69+
1. `page_size`.
70+
1. `cells`.
71+
1. `pages`.
72+
1. `max_payload_size`.
73+
1. `sequence_number`. Sequence numbers of autoincrement columns. Generated based on the [sqlite_sequence](https://renenyffenegger.ch/notes/development/databases/SQLite/internals/schema-objects/sqlite_sequence) table. The query will fail if there are no autoincrement columns in the DB yet.
74+
1. `table_name`.
75+
1. `sequence_number`.
76+
1. `pragma`. List values of PRAGMAs (settings). Only includes the ones that have an integer or a boolean value. For bravity, the ones with `0` (`false`) value are excluded from the output (based on the observation that this is the default value for most of the PRAGMAs). Check out the SQLite documentation to learn more about what each PRAGMA means: [PRAGMA Statements](https://www.sqlite.org/pragma.html).
4477
1. `name`: the name of the PRAGMA as listed in the SQLite documentation.
4578
1. `value`: the value of the PRAGMA. The `true` value is converted into `1` (and `false` is simply excluded).
79+
1. `integrity_check`. Run integrity checks on the database. Executes [PRAGMA integrity_check](https://www.sqlite.org/pragma.html#pragma_integrity_check) and returns the resulting messages.
4680

4781
## Acknowledgments
4882

4983
These are the projects that made `ecto_sqlite3_extras` possible:
5084

5185
1. [phoenix_live_dashboard](https://github.com/phoenixframework/phoenix_live_dashboard) is the reason why I made the project. I want my SQLite-powered Phoenix service to have the same nice-looking live dashboard as for PostgreSQL.
52-
1. [exqlite](https://github.com/elixir-sqlite/exqlite) provides SQLite support for Elixir. They enabled just for me the `SQLITE_ENABLE_DBSTAT_VTAB` option required for `ecto_sqlite3_extras` to work, literally making this project possible.
86+
1. [exqlite](https://github.com/elixir-sqlite/exqlite) provides SQLite support for Elixir. They [enabled just for me](https://github.com/elixir-sqlite/exqlite/issues/231) the `SQLITE_ENABLE_DBSTAT_VTAB` option required for `ecto_sqlite3_extras` to work, literally making this project possible.
5387
1. [ecto_psql_extras](https://github.com/pawurb/ecto_psql_extras) is a similar project for PostgreSQL. I shamelessly copied the project structure and tests, so that I can be sure that `ecto_sqlite3_extras` can be a drop-in replacement for `ecto_psql_extras`.

lib/queries/table_size.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule EctoSQLite3Extras.TableSize do
1010
%{name: :name, type: :string},
1111
%{name: :payload_size, type: :bytes},
1212
%{name: :unused_size, type: :bytes},
13+
%{name: :vacuum_size, type: :bytes},
1314
%{name: :page_size, type: :bytes},
1415
%{name: :cells, type: :integer},
1516
%{name: :pages, type: :integer},
@@ -25,6 +26,7 @@ defmodule EctoSQLite3Extras.TableSize do
2526
name,
2627
payload AS payload_size,
2728
unused as unused_size,
29+
pgsize - payload - unused as vacuum_size,
2830
pgsize as page_size,
2931
ncell as cells,
3032
pageno as pages,

lib/queries/total_size.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ defmodule EctoSQLite3Extras.TotalSize do
2020
UNION ALL SELECT 'cells', SUM(ncell) FROM dbstat
2121
UNION ALL SELECT 'payload_size', SUM(payload) FROM dbstat
2222
UNION ALL SELECT 'unused_size', SUM(unused) FROM dbstat
23+
UNION ALL SELECT 'vacuum_size', SUM(pgsize) - SUM(payload) - SUM(unused) FROM dbstat
2324
UNION ALL SELECT 'page_size', SUM(pgsize) FROM dbstat
25+
UNION ALL SELECT 'pages', COUNT(*) FROM dbstat
2426
2527
UNION ALL SELECT 'pages: leaf', COUNT(*)
2628
FROM dbstat WHERE pagetype = 'leaf'
2729
UNION ALL SELECT 'pages: internal', COUNT(*)
2830
FROM dbstat WHERE pagetype = 'internal'
31+
UNION ALL SELECT 'pages: overflow', COUNT(*)
32+
FROM dbstat WHERE pagetype = 'overflow'
2933
UNION ALL SELECT 'pages: table', COUNT(*)
3034
FROM dbstat WHERE name IN (SELECT name FROM sqlite_schema WHERE type='table')
3135
UNION ALL SELECT 'pages: index', COUNT(*)

test/ecto_sqlite3_extras_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ defmodule EctoSQLite3ExtrasTest do
5252
["cells", 49351],
5353
["payload_size", "597.2 KB"],
5454
["unused_size", "82.0 KB"],
55+
["vacuum_size", "184.8 KB"],
5556
["page_size", "864.0 KB"],
57+
["pages", 864],
5658
["pages: leaf", 840],
5759
["pages: internal", 24],
60+
["pages: overflow", 0],
5861
["pages: table", 466],
5962
["pages: index", 389]
6063
]

0 commit comments

Comments
 (0)