Skip to content

Commit 1b84c17

Browse files
committed
--analyze option for create-index, insert, update commands, closes #379, closes #365
1 parent e0ef928 commit 1b84c17

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

docs/cli.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ You can delete all the existing rows in the table before inserting the new recor
761761

762762
$ sqlite-utils insert dogs.db dogs dogs.json --truncate
763763

764+
You can add the ``--analyze`` option to run ``ANALYZE`` against the table after the rows have been inserted.
765+
764766
.. _cli_inserting_data_binary:
765767

766768
Inserting binary data
@@ -1697,6 +1699,8 @@ This will create an index on that table on ``(col1, col2 desc, col3)``.
16971699

16981700
If your column names are already prefixed with a hyphen you'll need to manually execute a ``CREATE INDEX`` SQL statement to add indexes to them rather than using this tool.
16991701

1702+
Add the ``--analyze`` option to run ``ANALYZE`` against the index after it has been created.
1703+
17001704
.. _cli_fts:
17011705

17021706
Configuring full-text search
@@ -1817,6 +1821,8 @@ You can run it against specific tables, or against specific named indexes, by pa
18171821

18181822
$ sqlite-utils analyze mydb.db mytable idx_mytable_name
18191823

1824+
You can also run ``ANALYZE`` as part of another command using the ``--analyze`` option. This is supported by the ``create-index``, ``insert`` and ``upsert`` commands.
1825+
18201826
.. _cli_vacuum:
18211827

18221828
Vacuum

sqlite_utils/cli.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,15 @@ def index_foreign_keys(path, load_extension):
490490
default=False,
491491
is_flag=True,
492492
)
493+
@click.option(
494+
"--analyze",
495+
help="Run ANALYZE after creating the index",
496+
is_flag=True,
497+
)
493498
@load_extension_option
494-
def create_index(path, table, column, name, unique, if_not_exists, load_extension):
499+
def create_index(
500+
path, table, column, name, unique, if_not_exists, analyze, load_extension
501+
):
495502
"""
496503
Add an index to the specified table covering the specified columns.
497504
Use "sqlite-utils create-index mydb -- -column" to specify descending
@@ -506,7 +513,11 @@ def create_index(path, table, column, name, unique, if_not_exists, load_extensio
506513
col = DescIndex(col[1:])
507514
columns.append(col)
508515
db[table].create_index(
509-
columns, index_name=name, unique=unique, if_not_exists=if_not_exists
516+
columns,
517+
index_name=name,
518+
unique=unique,
519+
if_not_exists=if_not_exists,
520+
analyze=analyze,
510521
)
511522

512523

@@ -726,6 +737,11 @@ def insert_upsert_options(fn):
726737
envvar="SQLITE_UTILS_DETECT_TYPES",
727738
help="Detect types for columns in CSV/TSV data",
728739
),
740+
click.option(
741+
"--analyze",
742+
is_flag=True,
743+
help="Run ANALYZE at the end of this operation",
744+
),
729745
load_extension_option,
730746
click.option("--silent", is_flag=True, help="Do not show progress bar"),
731747
)
@@ -761,6 +777,7 @@ def insert_upsert_implementation(
761777
default=None,
762778
encoding=None,
763779
detect_types=None,
780+
analyze=False,
764781
load_extension=None,
765782
silent=False,
766783
):
@@ -853,7 +870,12 @@ def insert_upsert_implementation(
853870
else:
854871
docs = (fn(doc) or doc for doc in docs)
855872

856-
extra_kwargs = {"ignore": ignore, "replace": replace, "truncate": truncate}
873+
extra_kwargs = {
874+
"ignore": ignore,
875+
"replace": replace,
876+
"truncate": truncate,
877+
"analyze": analyze,
878+
}
857879
if not_null:
858880
extra_kwargs["not_null"] = set(not_null)
859881
if default:
@@ -950,6 +972,7 @@ def insert(
950972
alter,
951973
encoding,
952974
detect_types,
975+
analyze,
953976
load_extension,
954977
silent,
955978
ignore,
@@ -1005,6 +1028,7 @@ def insert(
10051028
truncate=truncate,
10061029
encoding=encoding,
10071030
detect_types=detect_types,
1031+
analyze=analyze,
10081032
load_extension=load_extension,
10091033
silent=silent,
10101034
not_null=not_null,
@@ -1039,6 +1063,7 @@ def upsert(
10391063
default,
10401064
encoding,
10411065
detect_types,
1066+
analyze,
10421067
load_extension,
10431068
silent,
10441069
):
@@ -1072,6 +1097,7 @@ def upsert(
10721097
default=default,
10731098
encoding=encoding,
10741099
detect_types=detect_types,
1100+
analyze=analyze,
10751101
load_extension=load_extension,
10761102
silent=silent,
10771103
)

tests/test_cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ def test_create_index(db_path):
224224
)
225225

226226

227+
def test_create_index_analyze(db_path):
228+
db = Database(db_path)
229+
assert "sqlite_stat1" not in db.table_names()
230+
assert [] == db["Gosh"].indexes
231+
result = CliRunner().invoke(
232+
cli.cli, ["create-index", db_path, "Gosh", "c1", "--analyze"]
233+
)
234+
assert result.exit_code == 0
235+
assert "sqlite_stat1" in db.table_names()
236+
237+
227238
def test_create_index_desc(db_path):
228239
db = Database(db_path)
229240
assert [] == db["Gosh"].indexes
@@ -889,6 +900,20 @@ def test_upsert(db_path, tmpdir):
889900
]
890901

891902

903+
def test_upsert_analyze(db_path, tmpdir):
904+
db = Database(db_path)
905+
db["rows"].insert({"id": 1, "foo": "x", "n": 3}, pk="id")
906+
db["rows"].create_index(["n"])
907+
assert "sqlite_stat1" not in db.table_names()
908+
result = CliRunner().invoke(
909+
cli.cli,
910+
["upsert", db_path, "rows", "-", "--nl", "--analyze", "--pk", "id"],
911+
input='{"id": 2, "foo": "bar", "n": 1}',
912+
)
913+
assert 0 == result.exit_code, result.output
914+
assert "sqlite_stat1" in db.table_names()
915+
916+
892917
def test_upsert_flatten(tmpdir):
893918
db_path = str(tmpdir / "flat.db")
894919
db = Database(db_path)

tests/test_cli_insert.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ def test_insert_alter(db_path, tmpdir):
327327
] == list(db.query("select foo, n, baz from from_json_nl"))
328328

329329

330+
def test_insert_analyze(db_path):
331+
db = Database(db_path)
332+
db["rows"].insert({"foo": "x", "n": 3})
333+
db["rows"].create_index(["n"])
334+
assert "sqlite_stat1" not in db.table_names()
335+
result = CliRunner().invoke(
336+
cli.cli,
337+
["insert", db_path, "rows", "-", "--nl", "--analyze"],
338+
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
339+
)
340+
assert 0 == result.exit_code, result.output
341+
assert "sqlite_stat1" in db.table_names()
342+
343+
330344
def test_insert_lines(db_path):
331345
result = CliRunner().invoke(
332346
cli.cli,

0 commit comments

Comments
 (0)