Search code examples
pythonexchangelib

Speeding up fetching emails using Exchangelib


Currently I fetch emails using an iterator this way:

for item in account.inbox.filter(datetime_received__gt=__LAST_ANALYSYS__+timedelta(0,1)):

I also test if type(item) == Message: as another filter layer.

Issue: it takes 5-10 mins to fetch 400+ messages; I have a macbook pro 3.5 GHz Dual-Core Intel Core i7 and 16 GB 2133 MHz LPDDR3. I have reasonably fast broadband connect.

The app will be migrated to Azure soon. Will the retrieval speed improve?

Or, is there another trick i can employ? I saw a post about using the only(attribs) nethod, but not sure if this helps materially, or how it can be combined with the account.inbox.filter method? Thank you.


Solution

  • Just try out the .only() method and see if it helps your use-case. Usually it does, because without it, you'll be fetching everything; attachments, MIME content, and other heavy fields. If you have a folder where the ratio of Message items to other item types is low, you will also benefit by doing a filter on item_class so you're filtering server-side, not client-side.

    .only() supports chaining, just like Django QuerySets: some_folder.filter(...).only(...). See https://ecederstrand.github.io/exchangelib/#searching