How to make your website faster if you have a lot of queries of the same data collection

Just if you think your wixData.queries take up a lot of time, because you have to do the same query on different pages many times:

I have a website that has a lot of velo code and I found a way to speed it up by avoiding the same queries again and again:

When the user logs into my site I run a code that collects all the data that is used on the website. For example I have a collection that stores all the data of “students” and a collection that stores all the data of “teachers”. I have to use these datasets on many of my pages and I was thinking about a way how to speed up the loading times.

So I did the following:
First a “getData” function is called that does all queries of all the data I need to use later on my pages and then I store the collected data sets in memory items.

So in my case I do the following:

import {memory} from ‘wix-storage’;

await wixData.query(“students”)
.eq(“_owner”, myUserId)
.limit(999)
.find()
.then((results) => {
memory.setItem(“students”, JSON.stringify(results.items));
}
});

I do the same with all other datasets that I will need later.

Now on each page where I need a dataset I call the data from the memory item:
studentsString = memory.getItem(“students”);
studentsData = JSON.parse(studentsString );

Now I can parse through studentsData instead of creating a new query each time when I need the students data.

I checked the speed by running the request either by a query or by parsing through the studentsData Array.

I checked the speed like this:

let startQuery = new Date().getTime();
await wixData.query(“students”)
.eq(“_owner”, wixUsers.currentUser.id)
.eq(“active”, true)
.find()
.then( (results) => {
// do something with the data
});
let endQuery = new Date().getTime();
let queryTime = endQuery - startQuery;
console.log("time used with query: "+queryTime);

let startForLoop = endQuery;
for (let i = 0; i < studentsData .length; i++) {
//do something
}
let endForLoop = new Date().getTime();
let loopTime = endForLoop - startForLoop;

console.log("time used with loop: "+loopTime);

In my case I got the following results:
Output:
time used with query: 163
time used with loop: 1

The bad aspect:
{memory} from ‘wix-storage’ can only hold 1mb.
I checked the usage of my local {memory} and at the moment I use approx. 250kb. So it only works for strings and probably not for heavy images and such and I am a bit worried if a user has many “students” and then the {memory} might come to the limit of 1mb.

I hope you are inspired by my approach on how to speed up your site if you query the same database many times on your pages.

And maybe you have an idea how to solve the limit of 1mb issue.
Or maybe you see some other problems that might occur with this approach…

Thanks!

Is there a reason why you are not using the native dataset filter settings for Owner is logged in? Or perhaps also using Owner Id in the/any dynamic page(s) so that it automatically filters the data on the page?

(I do not know if you are or are not using those methods already. I am only assuming you are not simply because of the code example you provided.)

Oh, you are right. I am using the native datafilter. The line
eq(“_owner”, wixUsers.currentUser.id)

is indeed obsolete.

Thanks for the hint!

1 Like