catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

Rand() optimization #1091

Closed Albanj closed 1 year ago

Albanj commented 1 year ago

Information

Hello folks.

I make a

$var = $database->rand('xxx', [
    'yyy',
    'zzz'
    ],[
    'LIMIT' => 6
    ]);

My hoster is crying because the request need to load a very big amount of data for every page load and overload the temporaries files.

They explain to me how to optimize the SQL request. If I understood how SQL woks, I won't use Medoo. No ? And we passed from (generated by Medoo)

SELECT yyy,zzz
FROM xxx
ORDER BY RAND()
LIMIT 6;

to

SELECT s.yyy, s.zzz
FROM xxx s
JOIN (
    SELECT id
    FROM xxx
    ORDER BY RAND()
    LIMIT 6
) AS z ON z.id = s.id

The time of request is 4s for unoptimized request to 0.15s for the optimized. The idea come from : https://stackoverflow.com/questions/1823306/mysql-alternatives-to-order-by-rand

I clearly don't have the skill to make a pull request, but I think this optimization in Medoo could be a good idea. In reality, I don't have the skill to understand how and why this optimization works.

Regards

catfan commented 1 year ago

Well, optimizing fetching data randomly from the database is a complex case.

Those solutions are based on different construction, different data type, or different database engine. There is no absolute answer.

I just recommend you can use rand() for simple and small data.

But for complex and larger data, it's good to write your query alone with query() based on your actual situation to get the best performance.

Albanj commented 1 year ago

Ok, thank you for your enlightenment.

And yes, I used query() yesterday to implement the solution prescribed by my hoster.