Jump to content

Improve Database Speed In Simple Program?


BorysSokolov

Recommended Posts

I've made a simple jQuery program which retrieves information from a database based on the value of a textbox. The event is triggered on keyup:

 

$('#textbox').on('keyup', function(){
	var category = $(this).val();
	
	$.get('dbRun.php', {category: category}, function(data){
		$('#output').html(data);
	});
});

 

...and here's the file that retrieves the data:

 

<?php
$linkDB = mysqli_connect('localhost', 'user', '', 'ajax_db');

$queryCategory = mysqli_query($linkDB, "SELECT content FROM site_contents WHERE category = '".$_GET['category']."'");
echo mysqli_fetch_row($queryCategory)[0];
?> 

 

Unfortunately, it runs pretty slow; I'm guessing it's because every time the event is triggered the database has to re-connect, right? Then how could I run the database connect once, at the start of the program, and make the connection available to the ajax file?

 

Thanks in advance.

Also if you only want one result limit the query to 1.

 

I've tightened up the code a little, like you suggested, and uploaded the program with free hosting, which seems to have eliminated the delay before the message is posted (I'm guessing their database is faster); but that still leaves me with one minor issue: how do I trigger the event to update the chat window whenever a new message is submitted? Currently I'm using intervals, but it seems really inefficient; is there a batter way?

 

I've thought about fetching all the messages at the start, and then just posting each new message directly to the chat window, so whenever the scroll height of the window would change, I could post the new message into the database right after, but I still haven't attempted it. Would that be a good technique?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.