Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way of creating indexes on newly created tables #349

Open
simonw opened this issue Dec 5, 2021 · 3 comments
Open

A way of creating indexes on newly created tables #349

simonw opened this issue Dec 5, 2021 · 3 comments
Labels
enhancement New feature or request python-library

Comments

@simonw
Copy link
Owner

simonw commented Dec 5, 2021

I'm writing code for simonw/git-history#33 that creates a table inside a loop:

item_pk = db[item_table].lookup(
    {"_item_id": item_id},
    item_to_insert,
    column_order=("_id", "_item_id"),
    pk="_id",
)

I need to look things up by _item_id on this table, which means I need an index on that column (the table can get very big).

But there's no mechanism in SQLite utils to detect if the table was created for the first time and add an index to it. And I don't want to run CREATE INDEX IF NOT EXISTS every time through the loop.

This should work like the foreign_keys= mechanism.

@simonw simonw added enhancement New feature or request python-library labels Dec 5, 2021
@simonw
Copy link
Owner Author

simonw commented Dec 6, 2021

(I ended up not needing this here since .lookup() already creates a unique index on _item_id for you. Still could be a useful feature though.)

@simonw
Copy link
Owner Author

simonw commented Dec 7, 2021

I think the syntax design of this looks like:

item_pk = db[item_table].lookup(
    {"_item_id": item_id},
    item_to_insert,
    column_order=("_id", "_item_id"),
    pk="_id",
    indexes=("_version",),
)

So it's a sequence of column names... or a sequence of tuples for creating compound indexes:

db["dogs"].insert(
  {"name": "Cleo", "species": "Mutt", "hobbies": "Raiding picnics"},
  indexes=(("name", "species"), "hobbies"),
)

@simonw
Copy link
Owner Author

simonw commented Dec 7, 2021

In terms of types, I think that means it looks like this:

IndexesType = Iterable[
    Union[str, Iterable[str]]
]

def create(
    self,
    columns: Dict[str, Any],
    pk: Optional[Any] = None,
    ...
    indexes: Optional[IndexesType] = None,
):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python-library
Projects
None yet
Development

No branches or pull requests

1 participant