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

Partition key from option #229

Merged
merged 1 commit into from
Nov 20, 2019
Merged

Partition key from option #229

merged 1 commit into from
Nov 20, 2019

Conversation

jannyHou
Copy link
Contributor

@jannyHou jannyHou commented Nov 18, 2019

Description

Prerequisite PR: loopbackio/loopback-connector-couchdb2#71

connect to #220

To trigger the partitonedFind, you can include the partitionKey field in options like:

Product.find(
  {where: {tag: 'food'}},
  // specify the partition key in the options
  {partitionKey: 'toronto'},
  cb
)

Related issues

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

package.json Outdated
@@ -29,9 +29,10 @@
"fs-extra": "^8.1.0",
"lodash": "^4.17.11",
"loopback-connector": "^4.0.0",
"loopback-connector-couchdb2": "^1.5.1",
"loopback-connector-couchdb2": "github:strongloop/loopback-connector-couchdb2#add-option-for-search",

Choose a reason for hiding this comment

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

Don't forget to switch back to npm released version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 sure

@jannyHou jannyHou force-pushed the partition-key-from-option branch from 57762cb to eef9b60 Compare November 19, 2019 22:16
@jannyHou jannyHou force-pushed the partition-key-from-option branch from 7770c30 to dffebe8 Compare November 19, 2019 22:39
db.automigrate('Product', () => {
Product.create(SEED_DATA, function(err) {
if (err) return done(err);
Product.find({where: {tag: 'food'}}, {partitionKey: 'toronto'},
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curious, I guess the performance of partition + where(#L196) is better than doing where on find() normally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@agnes512 Exactly, if you are interested to learn more about this feature, it's explained in https://docs.couchdb.org/en/master/partitioned-dbs/index.html.

An example: you have 200 documents in partition US and 300 documents in partition CA, when you do a global search, you find among 500 documents, but if you specify partitionKey as US and perform a partitioned search, you find among those 200 documents.

{id: `london: ${uuid()}`, name: 'beer', tag: 'drink'},
{id: `london: ${uuid()}`, name: 'salad', tag: 'food'},
{id: `london: ${uuid()}`, name: 'salad', tag: 'food'},
];
Copy link
Contributor

Choose a reason for hiding this comment

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

What if I have a 7th record {id: toronto: ${uuid()}, name: 'dessert', tag: 'food'},, would it be included to the result of the above test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, because its partitionKey is toronto(matches your partition key) and is tagged as food(match your query).

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, the name partitionKey always makes me think of operations based on index.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep they are related :)

The benefit of partitioned databases is that secondary indices can be significantly more efficient when locating matching documents since their entries are contained within their partition. This means a given secondary index read will only scan a single partition range instead of having to read from a copy of every shard.

My understanding is index is a secondary optimization for search.

@jannyHou jannyHou merged commit 4ee685e into master Nov 20, 2019
@delete-merged-branch delete-merged-branch bot deleted the partition-key-from-option branch November 20, 2019 15:04
dhmlau added a commit that referenced this pull request Jan 27, 2020
 * chore: update copyright year (Agnes Lin)
 * replace couchdb3 docker img with a stable version (Agnes Lin)
 * docs: add partition document (#232) (Janny)
 * fixup!: fix the dependency version (#233) (Janny)
 * feat: query with partition field (#230) (Janny)
 * chore: improve issue and PR templates (Nora)
 * feat: add partitioned find (#229) (Janny)
 * add docker setup script (#227) (Janny)
 * feat: partitioned index (#225) (Janny)
 * fix CODEOWNERS file (Diana Lau)
 * update docker image (#224) (Janny)
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.

3 participants