Posted in php function
477
2:46 am, April 13, 2023
 

function causing slow load time

<p>i had this function matching a uid using a binary select query. The sites were loading in about 8 full seconds according to my load timer, sometimes up to 12 seconds!</p>
<p>So this was running on old site code, really just a big mess.&nbsp;</p>
<p>But i copied it to another site and tested each function and the sql that ran on them to see which one was causing the lag time.&nbsp;</p>
<p>This query took about 0.03 seconds to return when tested.&nbsp;</p>
<p><code>SELECT $fieldname FROM $tablename where binary $uidfieldname = '$uid' LIMIT 1</code></p>
<p>I think I added the binary option as it was not selecting the uid correctly, but removing the binary seems to not make any difference to the results, but reduces this query time a lot.&nbsp;</p>
<p>Test site with 8+k pages was loading in:</p>
<p>Page generated in 8.3164 seconds.</p>
<p>Then after removing the binary select query and just using a normal query, the same page is now.&nbsp;</p>
<p>Page generated in 0.2958 seconds.</p>
<p>So makes a large difference, and more depending on the page or record number i think.&nbsp;</p>

PHP

function get_single_by_uid($field_name,$table_name,$uid,$uid_field_name = "uid") {
	global $db;
	$x = "";
	$uid = mysqli_real_escape_string($db, $uid);
	$field_name = mysqli_real_escape_string($db, $field_name);
	$table_name = mysqli_real_escape_string($db, $table_name);
	//$sql = "SELECT $field_name FROM $table_name where binary $uid_field_name = '$uid' LIMIT 1";
	$sql = "SELECT $field_name FROM $table_name where $uid_field_name = '$uid' LIMIT 1";
	$result = $db->query($sql);
	if(!$result) { return false; }
	$x = mysqli_fetch_assoc($result);	
	if(isset($x[$field_name])) {
		return(politeify($x[$field_name]));
	} 
	return false;
}

View Statistics
This Week
206
This Month
769
This Year
587

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in php function
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
therock Somewhere along this crazy road I learned (often times the hard way) the most important things I can do is be authentic, trust my gut, be the hardest worker in the room, celebrate the mistakes, be a grateful man and always remember that it's nice to be important, but it's more important to be nice.
The Rock
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.