mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
table.create_index(..., analyze=True), refs #378
This commit is contained in:
parent
541f64ddb0
commit
0d10402f7b
3 changed files with 19 additions and 3 deletions
|
|
@ -2204,6 +2204,8 @@ You can create a unique index by passing ``unique=True``:
|
|||
|
||||
Use ``if_not_exists=True`` to do nothing if an index with that name already exists.
|
||||
|
||||
Pass ``analyze=True`` to run ``ANALYZE`` against the new index after creating it.
|
||||
|
||||
.. _python_api_analyze:
|
||||
|
||||
Optimizing index usage with ANALYZE
|
||||
|
|
|
|||
|
|
@ -1554,6 +1554,7 @@ class Table(Queryable):
|
|||
unique: bool = False,
|
||||
if_not_exists: bool = False,
|
||||
find_unique_name: bool = False,
|
||||
analyze: bool = False,
|
||||
):
|
||||
"""
|
||||
Create an index on this table.
|
||||
|
|
@ -1565,6 +1566,7 @@ class Table(Queryable):
|
|||
- ``if_not_exists`` - only create the index if one with that name does not already exist.
|
||||
- ``find_unique_name`` - if ``index_name`` is not provided and the automatically derived name
|
||||
already exists, keep incrementing a suffix number to find an available name.
|
||||
- ``analyze`` - run ``ANALYZE`` against this index after creating it.
|
||||
|
||||
See :ref:`python_api_create_index`.
|
||||
"""
|
||||
|
|
@ -1581,7 +1583,11 @@ class Table(Queryable):
|
|||
columns_sql.append(fmt.format(column))
|
||||
|
||||
suffix = None
|
||||
created_index_name = None
|
||||
while True:
|
||||
created_index_name = (
|
||||
"{}_{}".format(index_name, suffix) if suffix else index_name
|
||||
)
|
||||
sql = (
|
||||
textwrap.dedent(
|
||||
"""
|
||||
|
|
@ -1591,9 +1597,7 @@ class Table(Queryable):
|
|||
)
|
||||
.strip()
|
||||
.format(
|
||||
index_name="{}_{}".format(index_name, suffix)
|
||||
if suffix
|
||||
else index_name,
|
||||
index_name=created_index_name,
|
||||
table_name=self.name,
|
||||
columns=", ".join(columns_sql),
|
||||
unique="UNIQUE " if unique else "",
|
||||
|
|
@ -1618,6 +1622,8 @@ class Table(Queryable):
|
|||
continue
|
||||
else:
|
||||
raise e
|
||||
if analyze:
|
||||
self.db.analyze(created_index_name)
|
||||
return self
|
||||
|
||||
def add_column(
|
||||
|
|
|
|||
|
|
@ -774,6 +774,14 @@ def test_create_index_find_unique_name(fresh_db):
|
|||
assert index_names == {"idx_t_id", "idx_t_id_2", "idx_t_id_3"}
|
||||
|
||||
|
||||
def test_create_index_analyze(fresh_db):
|
||||
dogs = fresh_db["dogs"]
|
||||
assert "sqlite_stat1" not in fresh_db.table_names()
|
||||
dogs.insert({"name": "Cleo", "twitter": "cleopaws"})
|
||||
dogs.create_index(["name"], analyze=True)
|
||||
assert "sqlite_stat1" in fresh_db.table_names()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data_structure",
|
||||
(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue