I have posted an topic on getRandomImage performance( may be post lost?), now I have modified the sql to imporved the performance (base on 1.1.5).
The speed of the SQL on my mysql server, the original is around 0.4 sec at normal period, the new 2 sql is (0.14 + 0.02) sec.
Since I have called the random image to display images in my wordpress, I made a short php script to just display 9 random images. The time for 9 image is improved from 4.X~7.X sec to 0.8~1.5 sec (My gallery got 4500 images), it maybe faster if getRandomImages allow pass in number of random images wanted and return an array.
`
function getRandomImages() {
if (zp_loggedin()) {
$albumWhere = '';
$imageWhere = '';
} else {
$albumscheck = query_full_array("SELECT * FROM " . prefix('albums'). " ORDER BY title");
foreach($albumscheck as $albumcheck) {
if(!checkAlbumPassword($albumcheck['folder'], $hint)) {
$albumpasswordcheck= " AND ".prefix('albums').".id != ".$albumcheck['id'];
$passwordcheck = $passwordcheck.$albumpasswordcheck;
}
}
$albumWhere = " AND ".prefix('albums') . ".show=1".$passwordcheck;
$imageWhere = " AND " . prefix('images') . ".show=1";
}
$c = 0;
while ($c < 10) {
$result = query_single_row('SELECT FLOOR(RAND() * COUNT(*)) AS rand_row ' .
' FROM '.prefix('images'). ', '.prefix('albums').
' WHERE ' . prefix('albums') . '.folder!="" AND '.prefix('images').'.albumid = ' . prefix('albums') . '.id ' . $albumWhere . $imageWhere );
$rand_row = $result['rand_row'];
$result = query_single_row('SELECT '.prefix('images').'.filename, '.prefix('albums').'.folder ' .
' FROM '.prefix('images').', '.prefix('albums') .
' WHERE '.prefix('images').'.albumid = '.prefix('albums').'.id ' . $albumWhere . $imageWhere . ' LIMIT ' . $rand_row . ', 1');
$imageName = $result['filename'];
if (is_valid_image($imageName)) {
$image = new Image(new Album(new Gallery(), $result['folder']), $imageName );
return $image;
}
$c++;
}
return NULL;
}
```
ps. sorry I have posted in wrong sub forum, seem that I can't delete my own post.
Comments
An important thing to mention: If you work on core functions like this please always use at least the latest nightly build (or even better the latest svn version), so that you use the latest code available. Since 1.1.6 is out now the 1.1.5 code is not a good choice..:-)
I don't believe that we change much, but just to be sure could you please revisit that with the nightly and then open a ticket for it with the code attached. The forum likes to mangle the posted code (whch I believe it did).
Thanks you for trying to improve it!
Actually, I have found another performance problem about random images, since I always logined with admin , it won't built the where clause for the security. The function go through all the node on the tree and backtrack all the parent. Once I test it without the login, it use extra 1 second for this security checking. My gallery got 200 albums (but not a deep one, just 2 or 3 level)
I don't have a idea to build a single SQL statement to solve it, but I have a idea to do this with the some logic change: When enable a album to password protected, use the same password to auto protect the child album which password is empty. As a reault, it is just a simple query to check weather the album is protected or not.
Is it a good idea? I check the zenphoto code only calling getRandomImages in template-functions.php, and calling 4 times in theme effervescence_plus & stopdesign.