Skip to content

Instantly share code, notes, and snippets.

@YohannesTz
Last active June 27, 2023 08:23
Show Gist options
  • Save YohannesTz/a977853e299a20430ed22d58b9d6111e to your computer and use it in GitHub Desktop.
Save YohannesTz/a977853e299a20430ed22d58b9d6111e to your computer and use it in GitHub Desktop.
const mysql = require("mysql2/promise");
const run = async () => {
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'dummy',
});
try {
console.time("database_get");
//const [rows, fields] = await connection.execute('SELECT * FROM MOCK_DATA ORDER BY RAND() LIMIT 500 OFFSET 200'); // use this for randomizing from SQL
const [rows, fields] = await connection.execute('SELECT * FROM MOCK_DATA LIMIT 500 OFFSET 200');
const randomizedRes = shuffleArray(rows);
console.timeEnd("database_get");
} catch (error) {
console.error(error);
} finally {
await connection.end();
}
};
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
run();
{
"name": "db_test",
"version": "1.0.0",
"main": "index.js",
"author": "yohannesTz",
"license": "MIT",
"dependencies": {
"mysql2": "^3.3.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment