# Slow query speeds, related to stable memory?

**URL:** <https://forum.dfinity.org/t/slow-query-speeds-related-to-stable-memory/26716>\
**Category:** Developers\
**Created:** [January 15, 2024, 6:40pm UTC](https://forum.dfinity.org/t/slow-query-speeds-related-to-stable-memory/26716 "2024-01-15T18:40:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristofer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kristofer/32/11135_2.png) [@kristofer](https://forum.dfinity.org/u/kristofer)\
**Post date:** [January 15, 2024, 6:40pm UTC](https://forum.dfinity.org/t/slow-query-speeds-related-to-stable-memory/26716/1 "2024-01-15T18:40:32Z")

</div>

I have created a simple canister for a demo app – a companion app to the [ic-siwe](https://github.com/kristoferlund/ic-siwe) library. All works well but query calls are kind of slow. I know update calls can take two or more seconds but so does my query calls. Is that expected? I am using stable memory.

Try for instance the [list\_profiles()](https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.icp0.io/?id=jvkuy-nqaaa-aaaal-qdbpq-cai) call.

Here are snippets with relevant code:

```Rust
thread_local! {
    static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
        RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));

    static USER_PROFILES: RefCell<StableBTreeMap<String, UserProfile, Memory>> = RefCell::new(
        StableBTreeMap::init(
            MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0))),
        )
    );
}

#[query]
fn list_profiles() -> Result<Vec<(String, UserProfile)>, String> {
    let profiles = USER_PROFILES.with(|p| p.borrow().iter().collect::<Vec<_>>());
    Ok(profiles)
}

```

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 15, 2024, 6:53pm UTC](https://forum.dfinity.org/t/slow-query-speeds-related-to-stable-memory/26716/2 "2024-01-15T18:53:24Z")

</div>

@ielashi knows best but, my understanding is indeed than stable memory is a bit slower than heap memory because every access requires deserializion respectively serializion - i.e. every read requires a deserialization and write a serialization of what’s in/out the memory.

That said, if the sample of data is small and entities are not gigantic, I don’t think it should make that a noticable difference.

Regarding your sample, I generally use [range](https://docs.rs/ic-stable-structures/0.6.1/ic_stable_structures/btreemap/struct.BTreeMap.html#method.range) instead of `iter` but I can in your example it does not make much a difference.

Stupid question but, you are also sure that you deployed with `query` right?

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [January 15, 2024, 7:32pm UTC](https://forum.dfinity.org/t/slow-query-speeds-related-to-stable-memory/26716/3 "2024-01-15T19:32:37Z")

</div>

> [@peterparker](#):
>
> Stupid question but, you are also sure that you deployed with `query` right?

I think that’s indeed the issue. The function seems not to be annotated as a query otherwise Candid UI should render a `Query` button instead of a call `Call` button if I’m not mistaken.

---

<div class="post-metadata">

**Author:** ![kristofer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kristofer/32/11135_2.png) [@kristofer](https://forum.dfinity.org/u/kristofer)\
**Post date:** [January 15, 2024, 7:46pm UTC](https://forum.dfinity.org/t/slow-query-speeds-related-to-stable-memory/26716/4 "2024-01-15T19:46:56Z")

</div>

> [@peterparker](#):
>
> Stupid question but, you are also sure that you deployed with `query` right?

🤦

I moved a bit too fast and forgot marking the call as `query` in the did. Now the calls are much quicker. Thanks!

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [January 15, 2024, 7:55pm UTC](https://forum.dfinity.org/t/slow-query-speeds-related-to-stable-memory/26716/5 "2024-01-15T19:55:28Z")

</div>

Been there, done that.

Cool to hear it works out!
