Skip to content

Commit 88d2b18

Browse files
swissspidyclaude
andcommitted
db size: Allow displaying data and index size separately
The reported size has always been the sum of the data and the index size, which hides how much of a database is taken up by indexes alone. Add optional `Data` and `Index` fields that can be selected through the new `--fields` argument, while `Size` keeps reporting the total. For SQLite, the size of a table now covers its indexes as well, which matches how table sizes are reported for MySQL and MariaDB. Human readable sizes now default to two decimals, so that a database of 6.23 GB is no longer reported as 7 GB, and a value that rounds up into the next size format is displayed in that size format. Fixes #348 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WA7GLFtiJMyt4ZwaDq194v
1 parent 19e4a9e commit 88d2b18

4 files changed

Lines changed: 278 additions & 116 deletions

File tree

‎README.md‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,12 +826,17 @@ Defaults to all tables registered to the $wpdb database handler.
826826
Displays the database name and size.
827827

828828
~~~
829-
wp db size [--size_format=<format>] [--tables] [--human-readable] [--format=<format>] [--scope=<scope>] [--network] [--decimals=<decimals>] [--all-tables-with-prefix] [--all-tables] [--order=<order>] [--orderby=<orderby>]
829+
wp db size [--size_format=<format>] [--tables] [--human-readable] [--fields=<fields>] [--format=<format>] [--scope=<scope>] [--network] [--decimals=<decimals>] [--all-tables-with-prefix] [--all-tables] [--order=<order>] [--orderby=<orderby>]
830830
~~~
831831

832832
Display the database name and size for `DB_NAME` specified in wp-config.php.
833833
The size defaults to a human-readable number.
834834

835+
The reported size is the sum of the data and the index size. Both can be
836+
displayed separately by adding them to `--fields`. For SQLite, the database
837+
size is the size of the database file, which also includes overhead such as
838+
free pages.
839+
835840
Available size formats include:
836841
* b (bytes)
837842
* kb (kilobytes)
@@ -876,6 +881,9 @@ Available size formats include:
876881
[--human-readable]
877882
Display database sizes in human readable formats.
878883

884+
[--fields=<fields>]
885+
Get a specific subset of the fields.
886+
879887
[--format=<format>]
880888
Render output in a particular format.
881889
---
@@ -893,7 +901,7 @@ Available size formats include:
893901
List all the tables in a multisite install.
894902

895903
[--decimals=<decimals>]
896-
Number of digits after decimal point. Defaults to 0.
904+
Number of digits after decimal point. Defaults to 0, or 2 when using --human-readable.
897905

898906
[--all-tables-with-prefix]
899907
List all tables that match the table prefix even if not registered on $wpdb. Overrides --network.
@@ -919,6 +927,18 @@ Available size formats include:
919927
- size
920928
---
921929

930+
**AVAILABLE FIELDS**
931+
932+
These fields will be displayed by default:
933+
934+
* Name
935+
* Size
936+
937+
These fields are optionally available:
938+
939+
* Data
940+
* Index
941+
922942
**EXAMPLES**
923943

924944
$ wp db size
@@ -946,6 +966,13 @@ Available size formats include:
946966
| wp_commentmeta | 48 KB |
947967
+-----------------------+-------+
948968

969+
$ wp db size --fields=Name,Data,Index,Size --human-readable
970+
+-------------------+---------+--------+---------+
971+
| Name | Data | Index | Size |
972+
+-------------------+---------+--------+---------+
973+
| wordpress_default | 3.43 GB | 2.8 GB | 6.23 GB |
974+
+-------------------+---------+--------+---------+
975+
949976
$ wp db size --size_format=b
950977
5865472
951978

‎features/db-size.feature‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,41 @@ Feature: Display database size
112112
"""
113113
And STDOUT should be empty
114114

115+
@require-mysql-or-mariadb
116+
Scenario: Display data and index size of a database for a WordPress install
117+
Given a WP install
118+
119+
When I run `wp db size --fields=Name,Data,Index,Size`
120+
Then STDOUT should match /wp_cli_test\s+\d+ B\s+\d+ B\s+\d+ B/
121+
122+
When I run `wp db size --fields=Name,Data,Index,Size --format=csv`
123+
Then STDOUT should contain:
124+
"""
125+
Name,Data,Index,Size
126+
"""
127+
128+
@require-sqlite @skip-windows
129+
Scenario: Display data and index size of a database for a WordPress install
130+
Given a WP install
131+
132+
When I run `wp db size --fields=Name,Data,Index,Size`
133+
Then STDOUT should match /\.ht\.sqlite(\.php)?\s+\d+ B\s+\d+ B\s+\d+ B/
134+
135+
# On CI, SQLite on Windows is missing the dbstat extension.
136+
@skip-windows
137+
Scenario: Display data and index size of each table for a WordPress install
138+
Given a WP install
139+
140+
When I run `wp db size --tables --fields=Name,Data,Index,Size`
141+
Then STDOUT should match /wp_posts\s+\d+ B\s+\d+ B\s+\d+ B/
142+
143+
When I run `wp db size --tables --fields=Name,Index --human-readable`
144+
Then STDOUT should match /wp_posts\s+[\d.]+ (B|KB)/
145+
But STDOUT should not contain:
146+
"""
147+
Size
148+
"""
149+
115150
Scenario: Display only database size in bytes for a WordPress install
116151
Given a WP install
117152

@@ -191,6 +226,17 @@ Feature: Display database size
191226
MB
192227
"""
193228

229+
@require-mysql-or-mariadb
230+
Scenario: Display database size in a human readable format with specific precision for a WordPress install
231+
Given a WP install
232+
233+
When I run `wp db size --human-readable`
234+
Then STDOUT should match /\d+\.\d+ KB/
235+
236+
When I run `wp db size --human-readable --decimals=0`
237+
Then STDOUT should match /\d+ KB/
238+
And STDOUT should not match /\d+\.\d+ KB/
239+
194240
@require-mysql-or-mariadb
195241
Scenario: Display database size in bytes with specific format for a WordPress install
196242
Given a WP install

0 commit comments

Comments
 (0)