MySQL efficiency

Reply

Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

MySQL efficiency

 
0
  #1
Mar 31st, 2009
Hello,
I'm pretty new to PHP and MySQL. I was wondering what the most efficient way to access a large database would be. All the tutorials I've seen show something along the lines of:

$database = mysql_query("SELECT * FROM table");
But it seems to me that if 'table' was a large database then the time that it would take to copy 'table' into $database would grow fast. Wouldn't it be faster to only load a few rows if we knew their primary key?

And how do you look up a row by its primary key? What I found on Google didn't have much information with it explaining what was going on. Thanks!
Last edited by peter_budo; Mar 31st, 2009 at 3:39 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: MySQL efficiency

 
0
  #2
Mar 31st, 2009
table is the name of the table in the database. $database in your example will contain a result set of rows from the database according to the query passed into the mysql_query function. It should probably be named $result instead of $database to give you a clearer understanding of what is happening.

Try to think of a database being like a filing cabinet. The tables in the database are the files in the filing cabinet and store the actual data.

EDIT: To actually connect to the database in php, check out the mysql_connect function. Once connected you run SQL queries using the mysql_query function and interrogate the result with the mysql_fetch_* functions.
Last edited by darkagn; Mar 31st, 2009 at 1:04 am.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: MySQL efficiency

 
0
  #3
Mar 31st, 2009
Thanks darkagn. I guess a better way to phrase my question would be is it possible to only select one row at a time based on something like the primary key? Something along the lines of:

  1. $result = mysql_query("SELECT primaryKey FROM table");
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,318
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: MySQL efficiency

 
0
  #4
Mar 31st, 2009
  1. $result = mysql_query("SELECT * FROM table where id=%s, $id");
Last edited by almostbob; Mar 31st, 2009 at 7:04 pm.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 33
Reputation: theighost is an unknown quantity at this point 
Solved Threads: 1
theighost theighost is offline Offline
Light Poster

Re: MySQL efficiency

 
0
  #5
Mar 31st, 2009
your question is rather vague, if you're asking how to limit the results by a condition then almostbob asked your question.

If you meant columns by 'rows' then u asked the question yourself by:
$result = mysql_query("SELECT column FROM table");
but If you want efficiency, meaning finding something fast in a database, well, I guess very few people had trouble with this, and I think you don't have to worry about efficiency in this case.
Last edited by peter_budo; Apr 1st, 2009 at 4:39 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC