> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt ## Resource: Build Efficient Queries ## Article: Build Efficient Queries ## Article Link: https://dev.wix.com/docs/develop-websites-sdk/code-your-site/best-practices/data/build-efficient-queries.md ## Article Content: # Build Efficient Queries Database queries are essential for displaying dynamic content on your site. However, inefficient queries can slow down your site's performance by transferring unnecessary data between the server and browser. Common inefficiencies include retrieving unfiltered results, loading complete sets of data at once, and requesting unused fields. The following techniques optimize your database queries and improve site performance by helping you retrieve only the data you need, when you need it. - [Filter your data](#filter-your-data) - [Select specific fields](#select-specific-fields) - [Omit the total counts](#omit-the-total-counts) - [Limit results with pagination](#limit-results-with-pagination) ## Filter your data Use [filter methods](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-filter/introduction.md) to query only the data you need. Instead of retrieving all items from a collection, apply filters to get specific items that match your criteria. Some filter methods include: - `eq()`: Items equal to a specific value - `gt()`: Items greater than a specific value - `lt()`: Items less than a specific value - `contains()`: Items that contain specific text - `startsWith()`: Items that start with specific text View more filter methods in the [WixDataFilter API reference](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-filter/introduction.md). Populate a repeater with only `"active"` items from a collection: ```javascript import { items } from "@wix/data"; let queryResults; $w.onReady(async function () { queryResults = await items .query("myCollection") .eq("status", "active") .find(); $w("#myRepeater").data = queryResults.items; }); ``` > **Note:** You can also filter data using [datasets in the editor](https://support.wix.com/en/article/cms-formerly-content-manager-about-filtering-and-sorting-live-site-content-with-datasets). ## Select specific fields Query only the fields you need rather than entire records to reduce data transfer and improve performance. For example, if your page displays only a few fields from a collection with many fields, retrieve just those specific fields. Use the `fields()` method to retrieve only the `"title"` and `"status"` fields: ```javascript import { items } from "@wix/data"; let queryResults; $w.onReady(async function () { queryResults = await items .query("myCollection") .fields("title", "status") .find(); $w("#myRepeater").data = queryResults.items; }); ``` ## Omit the total counts Speed up query execution by omitting the total item and page counts. By default, the `returnTotalCount` data option in the `find()` method is `false`. This means the query won't count the total number of items that match your query, which makes it run faster. Only set `returnTotalCount` to `true` when you need the total counts. For example, to show page numbers or "X items found". Use `find()` without setting `returnTotalCount`, defaulting it to `false`: ```javascript import { items } from "@wix/data"; let queryResults; $w.onReady(async function () { queryResults = await items .query("myCollection") .find(); // returnTotalCount is false by default $w("#myRepeater").data = queryResults.items; }); ``` Set `returnTotalCount` to `true` to get the total count of items: ```javascript import { items } from "@wix/data"; let queryResults; $w.onReady(async function () { queryResults = await items .query("myCollection") .find({ returnTotalCount: true }); // returnTotalCount is true $w("#myRepeater").data = queryResults.items; $w("#countText").text = `${queryResults.totalCount} items found`; // total count is used }); ``` ## Limit results with pagination Instead of loading all items at once, use the [`limit()`](https://dev.wix.com/docs/sdk/backend-modules/data/items/query.md) method in your query to retrieve data in smaller batches. By default, queries return up to 50 items, but you can adjust this limit based on your needs. This improves initial page load time and allows you to load additional items as needed using paging methods like [`hasNext()`](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-result/has-next.md) and [`next()`](https://dev.wix.com/docs/sdk/backend-modules/data/items/wix-data-result/next.md). Load the first 6 items with a **Load More** button for additional results: ```javascript import { items } from "@wix/data"; let queryResults; $w.onReady(async function () { queryResults = await items .query("myCollection") .limit(6) .find(); $w("#myRepeater").data = queryResults.items; if (queryResults.hasNext()) { $w("#loadMore").expand(); } $w("#loadMore").onClick(async () => { queryResults = await queryResults.next(); let currentData = $w("#myRepeater").data; $w("#myRepeater").data = currentData.concat(queryResults.items); if (!queryResults.hasNext()) { $w("#loadMore").collapse(); } }); }); ``` > **Note:** You can also configure pagination limits for [datasets in the Editor](https://support.wix.com/en/article/cms-changing-the-maximum-items-displayed-in-your-dataset-settings). ## See also - [Data Retrieval Performance Boosts](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/best-practices/data/data-retrieval-performance-boosts.md) - [Minimize Trips to the Server](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/best-practices/data/minimize-trips-to-the-server.md) - [About the Data Collections API](https://dev.wix.com/docs/sdk/backend-modules/data/collections/introduction.md) - [About Datasets](https://support.wix.com/en/article/cms-about-datasets)