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

Support Create Index #1199

Merged
merged 16 commits into from
Apr 6, 2025
Merged

Support Create Index #1199

merged 16 commits into from
Apr 6, 2025

Conversation

PThorpe92
Copy link
Contributor

@PThorpe92 PThorpe92 commented Mar 28, 2025

Closes #1193

│limbo> explain create index idxp on products(price);                                                                                                                        
│addr  opcode             p1    p2    p3    p4             p5  comment
│----  -----------------  ----  ----  ----  -------------  --  -------
│0     Init               0     39    0                    0   Start at 39
│1     CreateBtree        0     1     2                    0   r[1]=root iDb=0 flags=2
│2     OpenWriteAsync     0     1     0                    0
│3     OpenWriteAwait     0     0     0                    0
│4     NewRowId           0     2     0                    0
│5     String8            0     3     0     index          0   r[3]='index'
│6     String8            0     4     0     idxp           0   r[4]='idxp'
│7     String8            0     5     0     products       0   r[5]='products'
│8     Copy               1     6     1                    0   r[6]=r[1]
│9     String8            0     7     0     CREATE INDEX idxp ON products (price)  0   r[7]='CREATE INDEX idxp ON products (price)'
│10    MakeRecord         3     5     8                    0   r[8]=mkrec(r[3..7])
│11    InsertAsync        0     8     2                    0
│12    InsertAwait        0     0     0                    0
│13    SorterOpen         3     1     0     k(1,B)         0   cursor=3
│14    OpenPseudo         4     9     2                    0   2 columns in r[9]
│15    OpenReadAsync      2     273   0                    0   table=products, root=273
│16    OpenReadAwait      0     0     0                    0
│17    RewindAsync        2     0     0                    0
│18    RewindAwait        2     25    0                    0   Rewind table products
│19      Column           2     2     10                   0   r[10]=products.price
│20      RowId            2     11    0                    0   r[11]=products.rowid
│21      MakeRecord       10    2     12                   0   r[12]=mkrec(r[10..11])
│22      SorterInsert     3     12    0     0              0   key=r[12]
│23    NextAsync          2     0     0                    0
│24    NextAwait          2     19    0                    0
│25    OpenWriteAsync     1     1     0                    0
│26    OpenWriteAwait     0     0     0                    0
│27    SorterSort         3     33    0                    0
│28      SorterData       3     13    4                    0   r[13]=data
│29      SeekEnd          1     0     0                    0
│30      IdxInsertAsync   1     13    0                    0   key=r[13]
│31      IdxInsertAwait   1     0     0                    0
│32    SorterNext         3     28    0                    0
│33    Close              3     0     0                    0
│34    Close              2     0     0                    0
│35    Close              1     0     0                    0
│36    ParseSchema        0     0     0     name = 'idxp' AND type = 'index'  0   name = 'idxp' AND type = 'index'
│37    Close              0     0     0                    0
│38    Halt               0     0     0                    0
│39    Transaction        0     1     0                    0   write=true
│40    Goto               0     1     0                    0

This will create the initial index btree and insert whatever relevant records that need to be inserted, it doesn't handle the case of inserting new index keys when normal records are created afterwards. That will prob be added in next PR to keep this one concise.

Limbo will properly use the index in a subsequent query:
image

Creating a unique index on a column that has 2 existing identical rows:
image

@PThorpe92 PThorpe92 force-pushed the create-idx branch 2 times, most recently from cc9ed01 to 988116e Compare March 29, 2025 03:39
@PThorpe92 PThorpe92 marked this pull request as ready for review March 30, 2025 23:34
@PThorpe92 PThorpe92 requested a review from jussisaurio March 30, 2025 23:56
@PThorpe92 PThorpe92 force-pushed the create-idx branch 4 times, most recently from 7b26363 to a95f318 Compare April 3, 2025 00:48
Comment on lines +176 to +177
TableRowId((u64, Option<&'a ImmutableRecord>)),
IndexKey(&'a ImmutableRecord),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TableRowId's record is not really a "key" but the record to insert right? Does it make sense to remain separate to the key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this was kind of a pain.. why I included that is because otherwise the arguments to insert would be:

    pub fn insert(
        &mut self,
        key: &BTreeKey,
        record: Option<&ImmutableRecord>,  
        moved_before: bool,
    ) -> Result<CursorResult<()>> {

So making it part of the key felt like ergonomically the best choice.

But indeed, prob at least could use a better name 🤔

@penberg penberg merged commit 2d3fd01 into tursodatabase:main Apr 6, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CREATE INDEX support
4 participants