Skip to content

Don't dup documents in secondary indexes #752

Closed

Description

@coffeemug

Currently secondary indexes duplicate documents inline. We should either change them to point to primary documents instead, or support both ways and give users the option.

Activity

  1. wmrowan commented on May 7, 2013

    @wmrowan
    Contributor

    Do we duplicate large values when re-inserting rows into the secondary index b-tree? It's been awhile since I last played with the b-tree code but I think I remember that we only stored the first 256 bytes or so of the row in the b-tree leaf itself. For rows larger than that we stored only a large value reference that pointed to an on-disk list of blocks containing the row.

    I think we'd get close to ideal behavior if we always copied whatever we stored in the leaf of the main b-tree, copying large value references but not the actual large values being referenced. For rows less than 256 bytes the cost of duplication is low and we avoid the extra disk seeks needed to lookup the row in the main b-tree. For rows larger than 256 bytes we'd only be duplicating the large value reference (again only a few bytes) and again avoid unnecessary disk seeks

  2. coffeemug commented on Jul 12, 2013

    @coffeemug
    ContributorAuthor

    Bumping this issue. I was on a call with a user today, and this is a huge problem for them in production. They're using ~20 indexes on a table, and each document is quite big. Their read latencies and throughput are spectacular, but having all these indexes kills write throughput because it saturates disk bandwidth.

  3. wmrowan commented on Jul 12, 2013

    @wmrowan
    Contributor

    We should confirm if we actually do copy large values or just the reference to the large value when inserting into a secondary index. If it's the former behavior this could be pretty easy to fix by piggybacking on our large value behavior.

  4. jdoliner commented on Jul 12, 2013

    @jdoliner
    Contributor

    @wmrowan we copy large values.

  5. jdoliner commented on Aug 12, 2013

    @jdoliner
    Contributor

    Working on this.

  6. coffeemug commented on Aug 13, 2013

    @coffeemug
    ContributorAuthor

    @jdoliner, could you describe how you're doing this and what's involved? Specifically there are three questions currently in my mind (though there are probably more I'm not thinking of):

    • are we just going to store pkeys as the secondary index value (or rather, basically nothing, since it's already stored in the secondary index key) and then just look up documents by pkey?
    • are we going to use snapshotting in the cache to make this work, or some other mechanism?
    • also, there is a question of whether the copy-free implementation should be the only implementation, or one of two possible implementations controlled by a flag (MongoDB offers both an implementation that copies documents and one that requires an extra pkey lookup).

    I think this issue warrants some public discussion as it's a pretty big deal and the implementation tradeoffs will have significant product-y impacts that we should discuss.

  7. jdoliner commented on Aug 13, 2013

    @jdoliner
    Contributor

    So it actually turns out this is a lot easier than we initially thought. The key to this is that we use a blob_t to store values in the btree. I feel like you (Mr. Mug) know how these work but for those who don't know a blob_t basically gives you an abstracted way to store arbitrarily large values in btree nodes. The way it does this is you give it a chunk of space in a node (normally 250 bytes), if the value you're trying to store in it fits within 250 bytes then it just inlines it* otherwise it allocates more blocks to fit the data and stores references to them in the leaf node (I'm going to call the raw data stored in the leaf node the basic value).

    The key insight here is that we can copy the basic value byte for byte into another node and it will act as a reference to the data and ensure that the added overhead from value duplication is capped at 250 bytes per value which we can definitely live with.

    So the answer to all of your questions is no. The first 2 just don't apply and I don't think the 3rd one does either because this method is strictly better than the existing method.

    *We truncate so we're not using 250 bytes to store 10 byte values.


    The implementation of this is actually incredibly simple here's how the basic btree operations change:

    Insert

    Old Algorithm

    find location in primary btree
    convert to blob_t and insert in primary btree
    for each index:
        find location in index btree
        convert to blob_t and insert in index btree
    

    New Algorithm

    find location in primary btree
    convert to blob_t and insert in primary btree
    store basic value of blob_t in B
    for each index:
        find location in index btree
        insert B in index btree
    

    Update

    Old algorithm

    find location in primary btree
    compute new value
    clear blob_t stored in primary btree
    convert new value to blob_t and store in primary btree
    for each index:
        find location in index btree
        clear blob_t stored in index btree
        convert new value to blob_t and store in index btree
    

    New Algorithm

    find location in primary btree
    compute new value
    store basic value of blob_t stored in primary btree in B_delete
    convert new value to blob_t and store in primary btree
    store basic value of new blob_t in B
    for each index:
        find location in index btree
        insert B in index btree
    clear B_delete
    

    Delete

    Delete is just like update without the insertions.

  8. coffeemug commented on Aug 13, 2013

    @coffeemug
    ContributorAuthor

    Ah, that's really, really clever.

  9. jdoliner commented on Aug 14, 2013

    @jdoliner
    Contributor

    This is in CR 828.

  10. jdoliner commented on Aug 16, 2013

    @jdoliner
    Contributor

    This is in next as of 7dc3b3c. Ships in 1.9. @AtnNn we should do a point release for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions