# Optimization

## Client Side

{% hint style="info" %}

### IDLE

{% endhint %}

<figure><img src="/files/LVragXPTgeQCDWWbdFAC" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}

### When Nearby A Room

{% endhint %}

<figure><img src="/files/NRhn9sv4C2iqnIjZXcuW" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}

### When Nearby The Text And Marker

{% endhint %}

<figure><img src="/files/2iSPoGjzgXfOPoeX5CNJ" alt=""><figcaption></figcaption></figure>

## Server Side

{% hint style="info" %}

### All Sql Queries Are Perfectly Optimized

{% endhint %}

### Example Code

```lua
function SaveCustomers()
    if next(PlayersMotels) then
        local selectList = ''
        local formatQuery = ([[
            SELECT '%s' AS owner, %s AS py, '%s' AS mtl, %s AS rmid, '%s' AS unq, '%s' AS reqs, %s as susp, '%s' as sh, %s as st, %s as ed, '%s' as wn
        ]])
        local total = 0
        for k,v in pairs(PlayersMotels) do
            total+= 1
            if selectList == '' then 
                selectList = ([[
                    UPDATE mx_motels u JOIN (%s
                ]]):format(formatQuery:format(k, v.pay, v.motel, v.roomId, v.uniq, json.encode(v.requests), v.suspended, json.encode(v.shared), v.startTime, v.endTime, json.encode(v.warn)))
                goto continue
            end
            selectList = selectList .. ([[
                UNION %s
            ]]):format(formatQuery:format(k, v.pay, v.motel, v.roomId, v.uniq, json.encode(v.requests), v.suspended, json.encode(v.shared), v.startTime, v.endTime, json.encode(v.warn)))
            ::continue::
        end
        if selectList == '' then return end
        selectList = selectList .. [[
            ) AS t ON u.owner = t.owner
           SET pay = py,
            motel = mtl, 
            `roomId` = rmid,
            uniq = unq,
            requests = reqs,
            suspended = susp,
            `shared` = sh,
            `startTime` = st,
            `endTime` = ed,
            `warn` = wn
        ]]
        ExecuteSQL(selectList)
        Debug('Saved ^2'..total..'^1 Customers(s) ^0')
    end
end
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.moxha.dev/documentation/paid-scripts/motels/introduction/optimization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
