Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Optimization: Fetching from database

Tags:

html

database

php

I have some PHP code where once the next arrow is pressed it basically just gets the next image in the data base. However On mywebsite I sometimes can have over 600 users online all clicking next. Is there any way I can opimtize this php code to execute faster?

Thanks!

$nxtImage = mysql_query ("SELECT * FROM images WHERE active=1 and id>$id and section=$section ORDER BY id ASC LIMIT 1") or die (mysql_error());
$nxtrow = mysql_fetch_array($nxtImage);
$nxtnum = mysql_num_rows($nxtImage);
$nid = $nxtrow['id'];
$npicTitle = $nxtrow['img_title'];
$nimgLink = preg_replace("![^a-z0-9]+!i", "-", $npicTitle);
$nimgLink = strtolower($nimgLink); 
like image 804
user1301430 Avatar asked May 21 '26 23:05

user1301430


1 Answers

The first thing I would do is change the query to this : SELECT `id`, `img_title` FROM images ..., to ensure that you're only grabbing what you need. The next thing I'd do is build a covered index on id, img_title, section, active so that it doesn't have to read off the data page.

Past that, get more hardware, :D

like image 168
Mike Perrenoud Avatar answered May 23 '26 11:05

Mike Perrenoud