I’m using this query to check the username is already exist or not in database using ajax
so till now i hv only few records in DB bt in future username list gonna be huge so i want to improve the performance of query in such a way it will take less time to check username existence.
how can i improve following query ? is indexing on db is feasible solution for this ?
check.php
<?php
if(isset($_POST['user_name']))
{
$user_name=$_POST['user_name'];
include("include/conn.php");
$sql_check = mysql_query("select userid from `vector`.`signup` where userid='".$user_name."'")
or die(mysql_error());
if (mysql_num_rows($sql_check)>0)
{
echo "no";
}
else
{
echo "yes";
}
}
?>
my jquery code
<script language="javascript">
$(document).ready(function()
{
$("#uname").blur(function()
{
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
$.post("check.php",{ user_name:$(this).val() } ,function(data)
{
if(data=='no') //if username not avaiable
{
$("#msgbox").fadeTo(200,0.1,function()
{
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function()
{
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
</script>
Yes, you should probably add an index on the userid field. For mysql, that would look something like this:
Edit: You can use this line in the MySQL console once you’re connected to the database. If you’re using PHPMyAdmin, it also supports adding indexes in their GUI.