You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
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.
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.
@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.
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.
Activity
wmrowan commentedon May 7, 2013
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
coffeemug commentedon Jul 12, 2013
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.
wmrowan commentedon Jul 12, 2013
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.
jdoliner commentedon Jul 12, 2013
@wmrowan we copy large values.
jdoliner commentedon Aug 12, 2013
Working on this.
coffeemug commentedon Aug 13, 2013
@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):
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.
jdoliner commentedon Aug 13, 2013
So it actually turns out this is a lot easier than we initially thought. The key to this is that we use a
blob_tto store values in the btree. I feel like you (Mr. Mug) know how these work but for those who don't know ablob_tbasically 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
New Algorithm
Update
Old algorithm
New Algorithm
Delete
Delete is just like update without the insertions.
coffeemug commentedon Aug 13, 2013
Ah, that's really, really clever.
jdoliner commentedon Aug 14, 2013
This is in CR 828.
jdoliner commentedon Aug 16, 2013
This is in next as of 7dc3b3c. Ships in 1.9. @AtnNn we should do a point release for this.