Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f532a22
commit 17037b9
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## Using Lazy Parsing in Queries | ||
Lazy parsing allows you to skip the parsing and validation process for documents and instead call it on demand for each field separately. This can be useful for optimizing performance in certain scenarios. | ||
|
||
To use lazy parsing in your queries, you can pass the `lazy_parse=True` parameter to your find method. | ||
|
||
Here's an example of how to use lazy parsing in a find query: | ||
|
||
```python | ||
await Sample.find(Sample.number == 10, lazy_parse=True).to_list() | ||
``` | ||
|
||
By setting lazy_parse=True, the parsing and validation process will be skipped and be called on demand when the respective fields will be used. This can potentially improve the performance of your query by reducing the amount of processing required upfront. However, keep in mind that using lazy parsing may also introduce some additional overhead when accessing the fields later on. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import asyncio | ||
|
||
import motor.motor_asyncio | ||
|
||
from beanie import Document, init_beanie | ||
|
||
|
||
class SampleModel(Document): | ||
s: str = "TEST" | ||
i: int = 10 | ||
|
||
|
||
class SampleModel2(SampleModel): | ||
... | ||
|
||
|
||
class SampleModel3(SampleModel2): | ||
... | ||
|
||
|
||
class TestConcurrency: | ||
async def test_with_init(self, settings): | ||
for i in range(10): | ||
|
||
async def init_insert_find(): | ||
cli = motor.motor_asyncio.AsyncIOMotorClient( | ||
settings.mongodb_dsn | ||
) | ||
cli.get_io_loop = asyncio.get_running_loop | ||
db = cli[settings.mongodb_db_name] | ||
await init_beanie( | ||
db, | ||
document_models=[SampleModel3, SampleModel, SampleModel2], | ||
) | ||
|
||
await SampleModel2().insert() | ||
|
||
docs = await SampleModel2.find(SampleModel.i == 10).to_list() | ||
return docs | ||
|
||
await asyncio.gather(*[init_insert_find() for _ in range(100)]) | ||
await SampleModel2.delete_all() | ||
|
||
async def test_without_init(self, settings): | ||
for i in range(10): | ||
cli = motor.motor_asyncio.AsyncIOMotorClient(settings.mongodb_dsn) | ||
cli.get_io_loop = asyncio.get_running_loop | ||
db = cli[settings.mongodb_db_name] | ||
await init_beanie( | ||
db, document_models=[SampleModel3, SampleModel, SampleModel2] | ||
) | ||
|
||
async def insert_find(): | ||
await SampleModel2().insert() | ||
docs = await SampleModel2.find(SampleModel2.i == 10).to_list() | ||
return docs | ||
|
||
await asyncio.gather(*[insert_find() for _ in range(100)]) | ||
await SampleModel2.delete_all() |