mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-10-08 02:17:08 +02:00
table.create_index(..., analyze=True), refs #378
This commit is contained in:
parent
04e319a18d
commit
ced9034383
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.
|
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:
|
.. _python_api_analyze:
|
||||||
|
|
||||||
Optimizing index usage with ANALYZE
|
Optimizing index usage with ANALYZE
|
||||||
|
|
|
||||||
|
|
@ -1554,6 +1554,7 @@ class Table(Queryable):
|
||||||
unique: bool = False,
|
unique: bool = False,
|
||||||
if_not_exists: bool = False,
|
if_not_exists: bool = False,
|
||||||
find_unique_name: bool = False,
|
find_unique_name: bool = False,
|
||||||
|
analyze: bool = False,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Create an index on this table.
|
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.
|
- ``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
|
- ``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.
|
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`.
|
See :ref:`python_api_create_index`.
|
||||||
"""
|
"""
|
||||||
|
|
@ -1581,7 +1583,11 @@ class Table(Queryable):
|
||||||
columns_sql.append(fmt.format(column))
|
columns_sql.append(fmt.format(column))
|
||||||
|
|
||||||
suffix = None
|
suffix = None
|
||||||
|
created_index_name = None
|
||||||
while True:
|
while True:
|
||||||
|
created_index_name = (
|
||||||
|
"{}_{}".format(index_name, suffix) if suffix else index_name
|
||||||
|
)
|
||||||
sql = (
|
sql = (
|
||||||
textwrap.dedent(
|
textwrap.dedent(
|
||||||
"""
|
"""
|
||||||
|
|
@ -1591,9 +1597,7 @@ class Table(Queryable):
|
||||||
)
|
)
|
||||||
.strip()
|
.strip()
|
||||||
.format(
|
.format(
|
||||||
index_name="{}_{}".format(index_name, suffix)
|
index_name=created_index_name,
|
||||||
if suffix
|
|
||||||
else index_name,
|
|
||||||
table_name=self.name,
|
table_name=self.name,
|
||||||
columns=", ".join(columns_sql),
|
columns=", ".join(columns_sql),
|
||||||
unique="UNIQUE " if unique else "",
|
unique="UNIQUE " if unique else "",
|
||||||
|
|
@ -1618,6 +1622,8 @@ class Table(Queryable):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
|
if analyze:
|
||||||
|
self.db.analyze(created_index_name)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def add_column(
|
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"}
|
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(
|
@pytest.mark.parametrize(
|
||||||
"data_structure",
|
"data_structure",
|
||||||
(
|
(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue