mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 06:28:53 +00:00
update functions for 1.9.0, finally write prepare and common issues
This commit is contained in:
1 parent
c07dfc085c
commit
40dee21923
10 files changed
+227
-154
No files matched your search
+33
-57
@@ -17,71 +17,48 @@ You must include one of the following lines, adjusted for your connection and da
|
||||
set mysql_connection_string "mysql://root:12345@localhost/es_extended?charset=utf8mb4"
|
||||
set mysql_connection_string "user=root;database=es_extended;password=12345;charset=utf8mb4"
|
||||
```
|
||||
Certain special characters are reserved or blocked and may cause issues when used in your password.
|
||||
You can also add the following convars if you require extra information when testing queries.
|
||||
```
|
||||
set mysql_slow_query_warning 100
|
||||
set mysql_slow_query_warning 150
|
||||
set mysql_debug true
|
||||
```
|
||||
For more optional settings (such as multiple statements) you can reference [pool.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Pool.d.ts#L10) and [connection.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Connection.d.ts#L8).
|
||||
|
||||
|
||||
## Sync vs. async
|
||||
Asychronous behaviour does not _truly_ exist in any of the scripting runtimes utilised, with tasks being performed as part of a queue each tick.
|
||||
Two variants of exports are provided for users, and their names describe their behaviour as part of the "thread" being executed.
|
||||
|
||||
The following export is "asynchronous" and will execute in order, however the result will be delayed until a result is returned rather than pausing execution of following code.
|
||||
```lua
|
||||
print('This print will display first')
|
||||
exports.oxmysql:scalar('SELECT group FROM users WHERE identifier = ?', {playerIdentifier}, function(result)
|
||||
print('Group: '..result)
|
||||
end)
|
||||
print('This print will display second')
|
||||
```
|
||||
|
||||
Conversely, the following export will halt the active thread and wait for it to resolve before continuing.
|
||||
Furthermore, the result will be returned inline (left-hand assignment) rather than being locally scoped to the callback function.
|
||||
```lua
|
||||
print('This print will display first')
|
||||
local result = exports.oxmysql:scalarSync('SELECT group FROM users WHERE identifier = ?', {playerIdentifier})
|
||||
print('Group: '..result)
|
||||
print('This print will display third')
|
||||
```
|
||||
|
||||
|
||||
## Benchmark
|
||||
```
|
||||
Lua: Low: 0.2955ms | High: 16.7566ms | Avg: 0.36956378ms | Total: 3695.6378ms (10000 queries)
|
||||
JS: Low: 0.2831ms | High: 5.1899ms | Avg: 0.33495788ms | Total: 3349.5788ms (10000 queries)
|
||||
Lua: Low: 0.2167ms | Avg: 0.28788843ms | Total: 2878.8843ms (10000 queries)
|
||||
JS: Low: 0.2098ms | Avg: 0.29384436ms | Total: 2938.4436ms (10000 queries)
|
||||
```
|
||||
These benchmark _exports_ rather than actual query speed, resulting in extra delays due to serialisation and overhead. Lua generally falls _slightly_ behind, but the amount is negligible.
|
||||
Furthermore we utilise the _sync_ variants for this, so a query will only execute once the previous one has returned.
|
||||
These benchmarks await the previous response rather than occuring asynchronously, and measure the total time spent executing the export.
|
||||
The `MySQL.Sync.prepare` function calls `exports.oxmysql:execute_async`, as used in the JS sample.
|
||||
|
||||
#### Lua (5.4)
|
||||
```lua
|
||||
local val = 10000
|
||||
RegisterCommand('luasync', function()
|
||||
local queryTimesLocal = {}
|
||||
local result
|
||||
for i=1, val do
|
||||
local time = os.nanotime()
|
||||
local r = exports.oxmysql:scalarSync('SELECT * from users')
|
||||
queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000
|
||||
if i==1 then result = r end
|
||||
end
|
||||
local queryMsLow, queryMsHigh, queryMsSum = 1000, 0, 0
|
||||
for _, v in pairs(queryTimesLocal) do
|
||||
queryMsSum = queryMsSum + v
|
||||
end
|
||||
for _, v in pairs(queryTimesLocal) do
|
||||
if v > queryMsHigh then queryMsHigh = v end
|
||||
end
|
||||
for _, v in pairs(queryTimesLocal) do
|
||||
if v < queryMsLow then queryMsLow = v end
|
||||
end
|
||||
local averageQueryTime = queryMsSum / #queryTimesLocal
|
||||
print(json.encode(result))
|
||||
print('Low: '.. queryMsLow ..'ms | High: '..queryMsHigh..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)')
|
||||
local queryTimesLocal = {}
|
||||
local result
|
||||
local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'})
|
||||
for i=1, val do
|
||||
local time = os.nanotime()
|
||||
local r = MySQL.Sync.prepare('SELECT identifier from users WHERE lastname = ?', {'Linden'})
|
||||
queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000
|
||||
if i==1 then result = r end
|
||||
end
|
||||
local queryMsLow, queryMsSum = 1000, 0
|
||||
for _, v in pairs(queryTimesLocal) do
|
||||
queryMsSum = queryMsSum + v
|
||||
end
|
||||
for _, v in pairs(queryTimesLocal) do
|
||||
if v < queryMsLow then queryMsLow = v end
|
||||
end
|
||||
local averageQueryTime = queryMsSum / #queryTimesLocal
|
||||
print(json.encode(result))
|
||||
print('Low: '.. queryMsLow ..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)')
|
||||
end)
|
||||
|
||||
```
|
||||
|
||||
#### JavaScript
|
||||
@@ -89,18 +66,17 @@ end)
|
||||
const val = 10000
|
||||
RegisterCommand('jssync', async() => {
|
||||
const queryTimesLocal = [];
|
||||
let result
|
||||
let result
|
||||
for(let i=0; i < val; i++) {
|
||||
const startTime = process.hrtime.bigint()
|
||||
const r = await exports.oxmysql.scalarSync('SELECT * from users')
|
||||
queryTimesLocal.push(Number(process.hrtime.bigint() - startTime) / 1000000)
|
||||
if (i === 0) result = 1
|
||||
const startTime = process.hrtime()
|
||||
const r = await exports.oxmysql.execute_async('SELECT identifier from users WHERE lastname = ?', ['Linden'])
|
||||
queryTimesLocal.push(process.hrtime(startTime)[1] / 1000000)
|
||||
if (i === 0) result = r
|
||||
}
|
||||
const queryMsSum = queryTimesLocal.reduce((a, b) => a + b, 0)
|
||||
const queryMsHigh = queryTimesLocal.sort((a, b) => b - a)[0]
|
||||
const queryMsLow = queryTimesLocal.sort((a, b) => a - b)[0]
|
||||
const averageQueryTime = queryMsSum / queryTimesLocal.length
|
||||
console.log(result)
|
||||
console.log('Low: '+ queryMsLow +'ms | High: '+queryMsHigh+'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)')
|
||||
console.log(result)
|
||||
console.log('Low: '+ queryMsLow +'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)')
|
||||
})
|
||||
```
|
||||
Reference in new issue
Block a user