diff --git a/docs/python-api.rst b/docs/python-api.rst index 66e6100..a8fd786 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -638,8 +638,9 @@ The function can accept an iterator or generator of rows and will commit them ac You can skip inserting any records that have a primary key that already exists using ``ignore=True``. This works with both ``.insert({...}, ignore=True)`` and ``.insert_all([...], ignore=True)``. -You can delete all the existing rows in the table before inserting the new -records using ``truncate=True``. This is useful if you want to replace the data in the table. +You can delete all the existing rows in the table before inserting the new records using ``truncate=True``. This is useful if you want to replace the data in the table. + +Pass ``analyze=True`` to run ``ANALYZE`` against the table after inserting the new records. .. _python_api_insert_replace: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 6a68c1b..6d3bcf9 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -2575,10 +2575,13 @@ class Table(Queryable): conversions=DEFAULT, columns=DEFAULT, upsert=False, + analyze=False, ) -> "Table": """ Like ``.insert()`` but takes a list of records and ensures that the table that it creates (if table does not exist) has columns for ALL of that data. + + Use ``analyze=True`` to run ``ANALYZE`` after the insert has completed. """ pk = self.value_or_default("pk", pk) foreign_keys = self.value_or_default("foreign_keys", foreign_keys) @@ -2671,6 +2674,9 @@ class Table(Queryable): ignore, ) + if analyze: + self.analyze() + return self def upsert( @@ -2721,6 +2727,7 @@ class Table(Queryable): extracts=DEFAULT, conversions=DEFAULT, columns=DEFAULT, + analyze=False, ) -> "Table": """ Like ``.upsert()`` but can be applied to a list of records. @@ -2739,6 +2746,7 @@ class Table(Queryable): conversions=conversions, columns=columns, upsert=True, + analyze=analyze, ) def add_missing_columns(self, records: Iterable[Dict[str, Any]]) -> "Table": diff --git a/tests/test_create.py b/tests/test_create.py index 1906f10..82c2c72 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1028,6 +1028,23 @@ def test_insert_all_single_column(fresh_db): assert table.pks == ["name"] +@pytest.mark.parametrize("method_name", ("insert_all", "upsert_all")) +def test_insert_all_analyze(fresh_db, method_name): + table = fresh_db["table"] + table.insert_all([{"id": 1, "name": "Cleo"}], pk="id") + assert "sqlite_stat1" not in fresh_db.table_names() + table.create_index(["name"], analyze=True) + assert list(fresh_db["sqlite_stat1"].rows) == [ + {"tbl": "table", "idx": "idx_table_name", "stat": "1 1"} + ] + method = getattr(table, method_name) + method([{"id": 2, "name": "Suna"}], pk="id", analyze=True) + assert "sqlite_stat1" in fresh_db.table_names() + assert list(fresh_db["sqlite_stat1"].rows) == [ + {"tbl": "table", "idx": "idx_table_name", "stat": "2 1"} + ] + + def test_create_with_a_null_column(fresh_db): record = {"name": "Name", "description": None} fresh_db["t"].insert(record)